fl-icon-selector.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. (function($){
  2. /**
  3. * Helper class for the icon selector lightbox.
  4. *
  5. * @class FLIconSelector
  6. * @since 1.0
  7. */
  8. FLIconSelector = {
  9. /**
  10. * A reference to the lightbox HTML content that is
  11. * loaded via AJAX.
  12. *
  13. * @since 1.0
  14. * @access private
  15. * @property {String} _content
  16. */
  17. _content : null,
  18. /**
  19. * A reference to a FLLightbox object.
  20. *
  21. * @since 1.0
  22. * @access private
  23. * @property {FLLightbox} _lightbox
  24. */
  25. _lightbox : null,
  26. /**
  27. * A flag for whether the content has already
  28. * been rendered or not.
  29. *
  30. * @since 1.0
  31. * @access private
  32. * @property {Boolean} _rendered
  33. */
  34. _rendered : false,
  35. /**
  36. * The text that is used to filter the selection
  37. * of visible icons.
  38. *
  39. * @since 1.0
  40. * @access private
  41. * @property {String} _filterText
  42. */
  43. _filterText : '',
  44. /**
  45. * Opens the icon selector lightbox.
  46. *
  47. * @since 1.0
  48. * @method open
  49. * @param {Function} callback A callback that fires when an icon is selected.
  50. */
  51. open: function(callback)
  52. {
  53. if(!FLIconSelector._rendered) {
  54. FLIconSelector._render();
  55. }
  56. if(FLIconSelector._content === null) {
  57. FLIconSelector._lightbox.open('<div class="fl-builder-lightbox-loading"></div>');
  58. FLBuilder.ajax({
  59. action: 'render_icon_selector'
  60. }, FLIconSelector._getContentComplete);
  61. }
  62. else {
  63. FLIconSelector._lightbox.open();
  64. }
  65. FLIconSelector._lightbox.on('icon-selected', function(event, icon){
  66. FLIconSelector._lightbox.off('icon-selected');
  67. FLIconSelector._lightbox.close();
  68. callback(icon);
  69. });
  70. },
  71. /**
  72. * Renders a new instance of FLLightbox.
  73. *
  74. * @since 1.0
  75. * @access private
  76. * @method _render
  77. */
  78. _render: function()
  79. {
  80. FLIconSelector._lightbox = new FLLightbox({
  81. className: 'fl-icon-selector'
  82. });
  83. FLIconSelector._rendered = true;
  84. FLBuilder.addHook( 'endEditingSession', function() {
  85. FLIconSelector._lightbox.close()
  86. } );
  87. },
  88. /**
  89. * Callback for when the lightbox content
  90. * has been returned via AJAX.
  91. *
  92. * @since 1.0
  93. * @access private
  94. * @method _getContentComplete
  95. * @param {String} response The JSON with the HTML lightbox content.
  96. */
  97. _getContentComplete: function(response)
  98. {
  99. var data = JSON.parse(response);
  100. FLIconSelector._content = data.html;
  101. FLIconSelector._lightbox.setContent(data.html);
  102. $('.fl-icons-filter-select').on('change', FLIconSelector._filter);
  103. $('.fl-icons-filter-text').on('keyup', FLIconSelector._filter);
  104. $('.fl-icons-list i').on('click', FLIconSelector._select);
  105. $('.fl-icon-selector-cancel').on('click', $.proxy(FLIconSelector._lightbox.close, FLIconSelector._lightbox));
  106. },
  107. /**
  108. * Filters the selection of visible icons based on
  109. * the library select and search input text.
  110. *
  111. * @since 1.0
  112. * @access private
  113. * @method _filter
  114. */
  115. _filter: function()
  116. {
  117. var section = $( '.fl-icons-filter-select' ).val(),
  118. text = $( '.fl-icons-filter-text' ).val();
  119. // Filter sections.
  120. if ( 'all' == section ) {
  121. $( '.fl-icons-section' ).show();
  122. }
  123. else {
  124. $( '.fl-icons-section' ).hide();
  125. $( '.fl-' + section ).show();
  126. }
  127. // Filter icons.
  128. FLIconSelector._filterText = text;
  129. if ( '' !== text ) {
  130. $( '.fl-icons-list i' ).each( FLIconSelector._filterIcon );
  131. }
  132. else {
  133. $( '.fl-icons-list i' ).show();
  134. }
  135. },
  136. /**
  137. * Shows or hides an icon based on the filter text.
  138. *
  139. * @since 1.0
  140. * @access private
  141. * @method _filterIcon
  142. */
  143. _filterIcon: function()
  144. {
  145. var icon = $( this );
  146. if ( -1 == icon.attr( 'class' ).indexOf( FLIconSelector._filterText ) ) {
  147. icon.hide();
  148. }
  149. else {
  150. icon.show();
  151. }
  152. },
  153. /**
  154. * Called when an icon is selected and fires the
  155. * icon-selected event on the lightbox.
  156. *
  157. * @since 1.0
  158. * @access private
  159. * @method _select
  160. */
  161. _select: function()
  162. {
  163. var icon = $(this).attr('class');
  164. FLIconSelector._lightbox.trigger('icon-selected', icon);
  165. }
  166. };
  167. })(jQuery);