PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/httpkientrucshome.com/wp-content/themes/genesis/lib/js/admin.js

https://gitlab.com/hop23typhu/list-theme
JavaScript | 338 lines | 102 code | 50 blank | 186 comment | 10 complexity | 1a7de2d1e4e4dab683940455abee57a8 MD5 | raw file
  1. /**
  2. * This file controls the behaviours within the Genesis Framework.
  3. *
  4. * Note that while this version of the file include 'use strict'; at the function level,
  5. * the Closure Compiler version strips that away. This is fine, as the compiler may
  6. * well be doing things that are not use strict compatible.
  7. *
  8. * @author StudioPress
  9. */
  10. // ==ClosureCompiler==
  11. // @compilation_level ADVANCED_OPTIMIZATIONS
  12. // @output_file_name admin.min.js
  13. // @externs_url http://closure-compiler.googlecode.com/svn/trunk/contrib/externs/jquery-1.8.js
  14. // ==/ClosureCompiler==
  15. // http://closure-compiler.appspot.com/home
  16. /* global genesis, genesisL10n, genesis_toggles, confirm */
  17. /**
  18. * Holds Genesis values in an object to avoid polluting global namespace.
  19. *
  20. * @since 1.8.0
  21. *
  22. * @constructor
  23. */
  24. window[ 'genesis' ] = {
  25. settingsChanged: false,
  26. /**
  27. * Inserts a category checklist toggle button and binds the behaviour.
  28. *
  29. * @since 1.8.0
  30. *
  31. * @function
  32. */
  33. categoryChecklistToggleInit: function() {
  34. 'use strict';
  35. // Insert toggle button into DOM wherever there is a category checklist
  36. jQuery( '<p><span id="genesis-category-checklist-toggle" class="button">' + genesisL10n.categoryChecklistToggle + '</span></p>' )
  37. .insertBefore( 'ul.categorychecklist' );
  38. // Bind the behaviour to click
  39. jQuery( document ).on( 'click.genesis.genesis_category_checklist_toggle', '#genesis-category-checklist-toggle', genesis.categoryChecklistToggle );
  40. },
  41. /**
  42. * Provides the behaviour for the category checklist toggle button.
  43. *
  44. * On the first click, it checks all checkboxes, and on subsequent clicks it
  45. * toggles the checked status of the checkboxes.
  46. *
  47. * @since 1.8.0
  48. *
  49. * @function
  50. *
  51. * @param {jQuery.event} event
  52. */
  53. categoryChecklistToggle: function( event ) {
  54. 'use strict';
  55. // Cache the selectors
  56. var $this = jQuery( event.target ),
  57. checkboxes = $this.parent().next().find( ':checkbox' );
  58. // If the button has already been clicked once, clear the checkboxes and remove the flag
  59. if ( $this.data( 'clicked' ) ) {
  60. checkboxes.removeAttr( 'checked' );
  61. $this.data( 'clicked', false );
  62. } else { // Mark the checkboxes and add a flag
  63. checkboxes.attr( 'checked', 'checked' );
  64. $this.data( 'clicked', true );
  65. }
  66. },
  67. /**
  68. * Grabs the array of toggle settings and loops through them to hook in
  69. * the behaviour.
  70. *
  71. * The genesis_toggles array is filterable in load-scripts.php before being
  72. * passed over to JS via wp_localize_script().
  73. *
  74. * @since 1.8.0
  75. *
  76. * @function
  77. */
  78. toggleSettingsInit: function() {
  79. 'use strict';
  80. jQuery.each( genesis_toggles, function( k, v ) {
  81. // Prepare data
  82. var data = { selector: v[ 0 ], showSelector: v[ 1 ], checkValue: v[ 2 ] };
  83. // Setup toggle binding
  84. jQuery( 'div.genesis-metaboxes' )
  85. .on( 'change.genesis.genesis_toggle', v[ 0 ], data, genesis.toggleSettings );
  86. // Trigger the check when page loads too.
  87. // Can't use triggerHandler here, as that doesn't bubble the event up to div.genesis-metaboxes.
  88. // We namespace it, so that it doesn't conflict with any other change event attached that
  89. // we don't want triggered on document ready.
  90. jQuery( v[ 0 ]).trigger( 'change.genesis_toggle', data );
  91. });
  92. },
  93. /**
  94. * Provides the behaviour for the change event for certain settings.
  95. *
  96. * Three bits of event data is passed - the jQuery selector which has the
  97. * behaviour attached, the jQuery selector which to toggle, and the value to
  98. * check against.
  99. *
  100. * The checkValue can be a single string or an array (for checking against
  101. * multiple values in a dropdown) or a null value (when checking if a checkbox
  102. * has been marked).
  103. *
  104. * @since 1.8.0
  105. *
  106. * @function
  107. *
  108. * @param {jQuery.event} event
  109. */
  110. toggleSettings: function( event ) {
  111. 'use strict';
  112. // Cache selectors
  113. var $selector = jQuery( event.data.selector ),
  114. $showSelector = jQuery( event.data.showSelector ),
  115. checkValue = event.data.checkValue;
  116. // Compare if a checkValue is an array, and one of them matches the value of the selected option
  117. // OR the checkValue is _unchecked, but the checkbox is not marked
  118. // OR the checkValue is _checked, but the checkbox is marked
  119. // OR it's a string, and that matches the value of the selected option.
  120. if (
  121. ( jQuery.isArray( checkValue ) && jQuery.inArray( $selector.val(), checkValue ) > -1) ||
  122. ( '_unchecked' === checkValue && $selector.is( ':not(:checked)' ) ) ||
  123. ( '_checked' === checkValue && $selector.is( ':checked' ) ) ||
  124. ( '_unchecked' !== checkValue && '_checked' !== checkValue && $selector.val() === checkValue )
  125. ) {
  126. jQuery( $showSelector ).slideDown( 'fast' );
  127. } else {
  128. jQuery( $showSelector ).slideUp( 'fast' );
  129. }
  130. },
  131. /**
  132. * When a input or textarea field field is updated, update the character counter.
  133. *
  134. * For now, we can assume that the counter has the same ID as the field, with a _chars
  135. * suffix. In the future, when the counter is added to the DOM with JS, we can add
  136. * a data( 'counter', 'counter_id_here' ) property to the field element at the same time.
  137. *
  138. * @since 1.8.0
  139. *
  140. * @function
  141. *
  142. * @param {jQuery.event} event
  143. */
  144. updateCharacterCount: function( event ) {
  145. 'use strict';
  146. jQuery( '#' + event.target.id + '_chars' ).html( jQuery( event.target ).val().length.toString() );
  147. },
  148. /**
  149. * Provides the behaviour for the layout selector.
  150. *
  151. * When a layout is selected, the all layout labels get the selected class
  152. * removed, and then it is added to the label that was selected.
  153. *
  154. * @since 1.8.0
  155. *
  156. * @function
  157. *
  158. * @param {jQuery.event} event
  159. */
  160. layoutHighlighter: function( event ) {
  161. 'use strict';
  162. // Cache class name
  163. var selectedClass = 'selected';
  164. // Remove class from all labels
  165. jQuery('input[name="' + jQuery(event.target).attr('name') + '"]').parent('label').removeClass(selectedClass);
  166. // Add class to selected layout
  167. jQuery(event.currentTarget).addClass(selectedClass);
  168. },
  169. /**
  170. * Helper function for confirming a user action.
  171. *
  172. * @since 1.8.0
  173. *
  174. * @function
  175. *
  176. * @param {String} text The text to display.
  177. * @returns {Boolean}
  178. */
  179. confirm: function( text ) {
  180. 'use strict';
  181. return confirm( text );
  182. },
  183. /**
  184. * Have all form fields in Genesis metaboxes set a dirty flag when changed.
  185. *
  186. * @since 2.0.0
  187. *
  188. * @function
  189. */
  190. attachUnsavedChangesListener: function() {
  191. 'use strict';
  192. jQuery( 'div.genesis-metaboxes :input' ).change( function() {
  193. genesis.registerChange();
  194. });
  195. window.onbeforeunload = function(){
  196. if ( genesis.settingsChanged ) {
  197. return genesisL10n.saveAlert;
  198. }
  199. };
  200. jQuery( 'div.genesis-metaboxes input[type="submit"]' ).click( function() {
  201. window.onbeforeunload = null;
  202. });
  203. },
  204. /**
  205. * Set a flag, to indicate form fields have changed.
  206. *
  207. * @since 2.0.0
  208. *
  209. * @function
  210. */
  211. registerChange: function() {
  212. 'use strict';
  213. genesis.settingsChanged = true;
  214. },
  215. /**
  216. * Ask user to confirm that a new version of Genesis should now be installed.
  217. *
  218. * @since 2.0.0
  219. *
  220. * @function
  221. *
  222. * @return {Boolean} True if upgrade should occur, false if not.
  223. */
  224. confirmUpgrade: function() {
  225. 'use strict';
  226. return confirm( genesisL10n.confirmUpgrade );
  227. },
  228. /**
  229. * Ask user to confirm that settings should now be reset.
  230. *
  231. * @since 2.0.0
  232. *
  233. * @function
  234. *
  235. * @return {Boolean} True if reset should occur, false if not.
  236. */
  237. confirmReset: function() {
  238. 'use strict';
  239. return confirm( genesisL10n.confirmReset );
  240. },
  241. /**
  242. * Initialises all aspects of the scripts.
  243. *
  244. * Generally ordered with stuff that inserts new elements into the DOM first,
  245. * then stuff that triggers an event on existing DOM elements when ready,
  246. * followed by stuff that triggers an event only on user interaction. This
  247. * keeps any screen jumping from occuring later on.
  248. *
  249. * @since 1.8.0
  250. *
  251. * @function
  252. */
  253. ready: function() {
  254. 'use strict';
  255. // Move all messages below our floated buttons
  256. jQuery( 'h2' ).nextAll( 'div.updated, div.error' ).insertAfter( 'p.top-buttons' );
  257. // Initialise category checklist toggle button
  258. genesis.categoryChecklistToggleInit();
  259. // Initialise settings that can toggle the display of other settings
  260. genesis.toggleSettingsInit();
  261. // Initialise form field changing flag.
  262. genesis.attachUnsavedChangesListener();
  263. // Bind character counters
  264. jQuery( '#genesis_title, #genesis_description' ).on( 'keyup.genesis.genesis_character_count', genesis.updateCharacterCount );
  265. // Bind layout highlighter behaviour
  266. jQuery('.genesis-layout-selector').on('change.genesis.genesis_layout_selector', 'label', genesis.layoutHighlighter);
  267. // Bind upgrade confirmation
  268. jQuery( '.genesis-js-confirm-upgrade' ).on( 'click.genesis.genesis_confirm_upgrade', genesis.confirmUpgrade );
  269. // Bind reset confirmation
  270. jQuery( '.genesis-js-confirm-reset' ).on( 'click.genesis.genesis_confirm_reset', genesis.confirmReset );
  271. }
  272. };
  273. jQuery( genesis.ready );
  274. /* jshint ignore:start */
  275. /**
  276. * Helper function for confirming a user action.
  277. *
  278. * This function is deprecated in favour of genesis.confirm( text ) which provides
  279. * the same functionality.
  280. *
  281. * @since 1.0.0
  282. * @deprecated 1.8.0
  283. */
  284. function genesis_confirm( text ) {
  285. 'use strict';
  286. return genesis.confirm( text );
  287. }
  288. /* jshint ignore:end */