wp-lists.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /**
  2. * @output wp-includes/js/wp-lists.js
  3. */
  4. /* global ajaxurl, wpAjax */
  5. /**
  6. * @param {jQuery} $ jQuery object.
  7. */
  8. ( function( $ ) {
  9. var functions = {
  10. add: 'ajaxAdd',
  11. del: 'ajaxDel',
  12. dim: 'ajaxDim',
  13. process: 'process',
  14. recolor: 'recolor'
  15. }, wpList;
  16. /**
  17. * @namespace
  18. */
  19. wpList = {
  20. /**
  21. * @member {object}
  22. */
  23. settings: {
  24. /**
  25. * URL for Ajax requests.
  26. *
  27. * @member {string}
  28. */
  29. url: ajaxurl,
  30. /**
  31. * The HTTP method to use for Ajax requests.
  32. *
  33. * @member {string}
  34. */
  35. type: 'POST',
  36. /**
  37. * ID of the element the parsed Ajax response will be stored in.
  38. *
  39. * @member {string}
  40. */
  41. response: 'ajax-response',
  42. /**
  43. * The type of list.
  44. *
  45. * @member {string}
  46. */
  47. what: '',
  48. /**
  49. * CSS class name for alternate styling.
  50. *
  51. * @member {string}
  52. */
  53. alt: 'alternate',
  54. /**
  55. * Offset to start alternate styling from.
  56. *
  57. * @member {number}
  58. */
  59. altOffset: 0,
  60. /**
  61. * Color used in animation when adding an element.
  62. *
  63. * Can be 'none' to disable the animation.
  64. *
  65. * @member {string}
  66. */
  67. addColor: '#ffff33',
  68. /**
  69. * Color used in animation when deleting an element.
  70. *
  71. * Can be 'none' to disable the animation.
  72. *
  73. * @member {string}
  74. */
  75. delColor: '#faafaa',
  76. /**
  77. * Color used in dim add animation.
  78. *
  79. * Can be 'none' to disable the animation.
  80. *
  81. * @member {string}
  82. */
  83. dimAddColor: '#ffff33',
  84. /**
  85. * Color used in dim delete animation.
  86. *
  87. * Can be 'none' to disable the animation.
  88. *
  89. * @member {string}
  90. */
  91. dimDelColor: '#ff3333',
  92. /**
  93. * Callback that's run before a request is made.
  94. *
  95. * @callback wpList~confirm
  96. * @param {object} this
  97. * @param {HTMLElement} list The list DOM element.
  98. * @param {object} settings Settings for the current list.
  99. * @param {string} action The type of action to perform: 'add', 'delete', or 'dim'.
  100. * @param {string} backgroundColor Background color of the list's DOM element.
  101. * @return {boolean} Whether to proceed with the action or not.
  102. */
  103. confirm: null,
  104. /**
  105. * Callback that's run before an item gets added to the list.
  106. *
  107. * Allows to cancel the request.
  108. *
  109. * @callback wpList~addBefore
  110. * @param {object} settings Settings for the Ajax request.
  111. * @return {object|boolean} Settings for the Ajax request or false to abort.
  112. */
  113. addBefore: null,
  114. /**
  115. * Callback that's run after an item got added to the list.
  116. *
  117. * @callback wpList~addAfter
  118. * @param {XML} returnedResponse Raw response returned from the server.
  119. * @param {object} settings Settings for the Ajax request.
  120. * @param {jqXHR} settings.xml jQuery XMLHttpRequest object.
  121. * @param {string} settings.status Status of the request: 'success', 'notmodified', 'nocontent', 'error',
  122. * 'timeout', 'abort', or 'parsererror'.
  123. * @param {object} settings.parsed Parsed response object.
  124. */
  125. addAfter: null,
  126. /**
  127. * Callback that's run before an item gets deleted from the list.
  128. *
  129. * Allows to cancel the request.
  130. *
  131. * @callback wpList~delBefore
  132. * @param {object} settings Settings for the Ajax request.
  133. * @param {HTMLElement} list The list DOM element.
  134. * @return {object|boolean} Settings for the Ajax request or false to abort.
  135. */
  136. delBefore: null,
  137. /**
  138. * Callback that's run after an item got deleted from the list.
  139. *
  140. * @callback wpList~delAfter
  141. * @param {XML} returnedResponse Raw response returned from the server.
  142. * @param {object} settings Settings for the Ajax request.
  143. * @param {jqXHR} settings.xml jQuery XMLHttpRequest object.
  144. * @param {string} settings.status Status of the request: 'success', 'notmodified', 'nocontent', 'error',
  145. * 'timeout', 'abort', or 'parsererror'.
  146. * @param {object} settings.parsed Parsed response object.
  147. */
  148. delAfter: null,
  149. /**
  150. * Callback that's run before an item gets dim'd.
  151. *
  152. * Allows to cancel the request.
  153. *
  154. * @callback wpList~dimBefore
  155. * @param {object} settings Settings for the Ajax request.
  156. * @return {object|boolean} Settings for the Ajax request or false to abort.
  157. */
  158. dimBefore: null,
  159. /**
  160. * Callback that's run after an item got dim'd.
  161. *
  162. * @callback wpList~dimAfter
  163. * @param {XML} returnedResponse Raw response returned from the server.
  164. * @param {object} settings Settings for the Ajax request.
  165. * @param {jqXHR} settings.xml jQuery XMLHttpRequest object.
  166. * @param {string} settings.status Status of the request: 'success', 'notmodified', 'nocontent', 'error',
  167. * 'timeout', 'abort', or 'parsererror'.
  168. * @param {object} settings.parsed Parsed response object.
  169. */
  170. dimAfter: null
  171. },
  172. /**
  173. * Finds a nonce.
  174. *
  175. * 1. Nonce in settings.
  176. * 2. `_ajax_nonce` value in element's href attribute.
  177. * 3. `_ajax_nonce` input field that is a descendant of element.
  178. * 4. `_wpnonce` value in element's href attribute.
  179. * 5. `_wpnonce` input field that is a descendant of element.
  180. * 6. 0 if none can be found.
  181. *
  182. * @param {jQuery} element Element that triggered the request.
  183. * @param {Object} settings Settings for the Ajax request.
  184. * @return {string|number} Nonce
  185. */
  186. nonce: function( element, settings ) {
  187. var url = wpAjax.unserialize( element.attr( 'href' ) ),
  188. $element = $( '#' + settings.element );
  189. return settings.nonce || url._ajax_nonce || $element.find( 'input[name="_ajax_nonce"]' ).val() || url._wpnonce || $element.find( 'input[name="_wpnonce"]' ).val() || 0;
  190. },
  191. /**
  192. * Extract list item data from a DOM element.
  193. *
  194. * Example 1: data-wp-lists="delete:the-comment-list:comment-{comment_ID}:66cc66:unspam=1"
  195. * Example 2: data-wp-lists="dim:the-comment-list:comment-{comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved"
  196. *
  197. * Returns an unassociative array with the following data:
  198. * data[0] - Data identifier: 'list', 'add', 'delete', or 'dim'.
  199. * data[1] - ID of the corresponding list. If data[0] is 'list', the type of list ('comment', 'category', etc).
  200. * data[2] - ID of the parent element of all inputs necessary for the request.
  201. * data[3] - Hex color to be used in this request. If data[0] is 'dim', dim class.
  202. * data[4] - Additional arguments in query syntax that are added to the request. Example: 'post_id=1234'.
  203. * If data[0] is 'dim', dim add color.
  204. * data[5] - Only available if data[0] is 'dim', dim delete color.
  205. * data[6] - Only available if data[0] is 'dim', additional arguments in query syntax that are added to the request.
  206. *
  207. * Result for Example 1:
  208. * data[0] - delete
  209. * data[1] - the-comment-list
  210. * data[2] - comment-{comment_ID}
  211. * data[3] - 66cc66
  212. * data[4] - unspam=1
  213. *
  214. * @param {HTMLElement} element The DOM element.
  215. * @param {string} type The type of data to look for: 'list', 'add', 'delete', or 'dim'.
  216. * @return {Array} Extracted list item data.
  217. */
  218. parseData: function( element, type ) {
  219. var data = [], wpListsData;
  220. try {
  221. wpListsData = $( element ).data( 'wp-lists' ) || '';
  222. wpListsData = wpListsData.match( new RegExp( type + ':[\\S]+' ) );
  223. if ( wpListsData ) {
  224. data = wpListsData[0].split( ':' );
  225. }
  226. } catch ( error ) {}
  227. return data;
  228. },
  229. /**
  230. * Calls a confirm callback to verify the action that is about to be performed.
  231. *
  232. * @param {HTMLElement} list The DOM element.
  233. * @param {Object} settings Settings for this list.
  234. * @param {string} action The type of action to perform: 'add', 'delete', or 'dim'.
  235. * @return {Object|boolean} Settings if confirmed, false if not.
  236. */
  237. pre: function( list, settings, action ) {
  238. var $element, backgroundColor, confirmed;
  239. settings = $.extend( {}, this.wpList.settings, {
  240. element: null,
  241. nonce: 0,
  242. target: list.get( 0 )
  243. }, settings || {} );
  244. if ( typeof settings.confirm === 'function' ) {
  245. $element = $( '#' + settings.element );
  246. if ( 'add' !== action ) {
  247. backgroundColor = $element.css( 'backgroundColor' );
  248. $element.css( 'backgroundColor', '#ff9966' );
  249. }
  250. confirmed = settings.confirm.call( this, list, settings, action, backgroundColor );
  251. if ( 'add' !== action ) {
  252. $element.css( 'backgroundColor', backgroundColor );
  253. }
  254. if ( ! confirmed ) {
  255. return false;
  256. }
  257. }
  258. return settings;
  259. },
  260. /**
  261. * Adds an item to the list via Ajax.
  262. *
  263. * @param {HTMLElement} element The DOM element.
  264. * @param {Object} settings Settings for this list.
  265. * @return {boolean} Whether the item was added.
  266. */
  267. ajaxAdd: function( element, settings ) {
  268. var list = this,
  269. $element = $( element ),
  270. data = wpList.parseData( $element, 'add' ),
  271. formValues, formData, parsedResponse, returnedResponse;
  272. settings = settings || {};
  273. settings = wpList.pre.call( list, $element, settings, 'add' );
  274. settings.element = data[2] || $element.prop( 'id' ) || settings.element || null;
  275. settings.addColor = data[3] ? '#' + data[3] : settings.addColor;
  276. if ( ! settings ) {
  277. return false;
  278. }
  279. if ( ! $element.is( '[id="' + settings.element + '-submit"]' ) ) {
  280. return ! wpList.add.call( list, $element, settings );
  281. }
  282. if ( ! settings.element ) {
  283. return true;
  284. }
  285. settings.action = 'add-' + settings.what;
  286. settings.nonce = wpList.nonce( $element, settings );
  287. if ( ! wpAjax.validateForm( '#' + settings.element ) ) {
  288. return false;
  289. }
  290. settings.data = $.param( $.extend( {
  291. _ajax_nonce: settings.nonce,
  292. action: settings.action
  293. }, wpAjax.unserialize( data[4] || '' ) ) );
  294. formValues = $( '#' + settings.element + ' :input' ).not( '[name="_ajax_nonce"], [name="_wpnonce"], [name="action"]' );
  295. formData = typeof formValues.fieldSerialize === 'function' ? formValues.fieldSerialize() : formValues.serialize();
  296. if ( formData ) {
  297. settings.data += '&' + formData;
  298. }
  299. if ( typeof settings.addBefore === 'function' ) {
  300. settings = settings.addBefore( settings );
  301. if ( ! settings ) {
  302. return true;
  303. }
  304. }
  305. if ( ! settings.data.match( /_ajax_nonce=[a-f0-9]+/ ) ) {
  306. return true;
  307. }
  308. settings.success = function( response ) {
  309. parsedResponse = wpAjax.parseAjaxResponse( response, settings.response, settings.element );
  310. returnedResponse = response;
  311. if ( ! parsedResponse || parsedResponse.errors ) {
  312. return false;
  313. }
  314. if ( true === parsedResponse ) {
  315. return true;
  316. }
  317. $.each( parsedResponse.responses, function() {
  318. wpList.add.call( list, this.data, $.extend( {}, settings, { // this.firstChild.nodevalue
  319. position: this.position || 0,
  320. id: this.id || 0,
  321. oldId: this.oldId || null
  322. } ) );
  323. } );
  324. list.wpList.recolor();
  325. $( list ).trigger( 'wpListAddEnd', [ settings, list.wpList ] );
  326. wpList.clear.call( list, '#' + settings.element );
  327. };
  328. settings.complete = function( jqXHR, status ) {
  329. if ( typeof settings.addAfter === 'function' ) {
  330. settings.addAfter( returnedResponse, $.extend( {
  331. xml: jqXHR,
  332. status: status,
  333. parsed: parsedResponse
  334. }, settings ) );
  335. }
  336. };
  337. $.ajax( settings );
  338. return false;
  339. },
  340. /**
  341. * Delete an item in the list via Ajax.
  342. *
  343. * @param {HTMLElement} element A DOM element containing item data.
  344. * @param {Object} settings Settings for this list.
  345. * @return {boolean} Whether the item was deleted.
  346. */
  347. ajaxDel: function( element, settings ) {
  348. var list = this,
  349. $element = $( element ),
  350. data = wpList.parseData( $element, 'delete' ),
  351. $eventTarget, parsedResponse, returnedResponse;
  352. settings = settings || {};
  353. settings = wpList.pre.call( list, $element, settings, 'delete' );
  354. settings.element = data[2] || settings.element || null;
  355. settings.delColor = data[3] ? '#' + data[3] : settings.delColor;
  356. if ( ! settings || ! settings.element ) {
  357. return false;
  358. }
  359. settings.action = 'delete-' + settings.what;
  360. settings.nonce = wpList.nonce( $element, settings );
  361. settings.data = $.extend( {
  362. _ajax_nonce: settings.nonce,
  363. action: settings.action,
  364. id: settings.element.split( '-' ).pop()
  365. }, wpAjax.unserialize( data[4] || '' ) );
  366. if ( typeof settings.delBefore === 'function' ) {
  367. settings = settings.delBefore( settings, list );
  368. if ( ! settings ) {
  369. return true;
  370. }
  371. }
  372. if ( ! settings.data._ajax_nonce ) {
  373. return true;
  374. }
  375. $eventTarget = $( '#' + settings.element );
  376. if ( 'none' !== settings.delColor ) {
  377. $eventTarget.css( 'backgroundColor', settings.delColor ).fadeOut( 350, function() {
  378. list.wpList.recolor();
  379. $( list ).trigger( 'wpListDelEnd', [ settings, list.wpList ] );
  380. } );
  381. } else {
  382. list.wpList.recolor();
  383. $( list ).trigger( 'wpListDelEnd', [ settings, list.wpList ] );
  384. }
  385. settings.success = function( response ) {
  386. parsedResponse = wpAjax.parseAjaxResponse( response, settings.response, settings.element );
  387. returnedResponse = response;
  388. if ( ! parsedResponse || parsedResponse.errors ) {
  389. $eventTarget.stop().stop().css( 'backgroundColor', '#faa' ).show().queue( function() {
  390. list.wpList.recolor();
  391. $( this ).dequeue();
  392. } );
  393. return false;
  394. }
  395. };
  396. settings.complete = function( jqXHR, status ) {
  397. if ( typeof settings.delAfter === 'function' ) {
  398. $eventTarget.queue( function() {
  399. settings.delAfter( returnedResponse, $.extend( {
  400. xml: jqXHR,
  401. status: status,
  402. parsed: parsedResponse
  403. }, settings ) );
  404. } ).dequeue();
  405. }
  406. };
  407. $.ajax( settings );
  408. return false;
  409. },
  410. /**
  411. * Dim an item in the list via Ajax.
  412. *
  413. * @param {HTMLElement} element A DOM element containing item data.
  414. * @param {Object} settings Settings for this list.
  415. * @return {boolean} Whether the item was dim'ed.
  416. */
  417. ajaxDim: function( element, settings ) {
  418. var list = this,
  419. $element = $( element ),
  420. data = wpList.parseData( $element, 'dim' ),
  421. $eventTarget, isClass, color, dimColor, parsedResponse, returnedResponse;
  422. // Prevent hidden links from being clicked by hotkeys.
  423. if ( 'none' === $element.parent().css( 'display' ) ) {
  424. return false;
  425. }
  426. settings = settings || {};
  427. settings = wpList.pre.call( list, $element, settings, 'dim' );
  428. settings.element = data[2] || settings.element || null;
  429. settings.dimClass = data[3] || settings.dimClass || null;
  430. settings.dimAddColor = data[4] ? '#' + data[4] : settings.dimAddColor;
  431. settings.dimDelColor = data[5] ? '#' + data[5] : settings.dimDelColor;
  432. if ( ! settings || ! settings.element || ! settings.dimClass ) {
  433. return true;
  434. }
  435. settings.action = 'dim-' + settings.what;
  436. settings.nonce = wpList.nonce( $element, settings );
  437. settings.data = $.extend( {
  438. _ajax_nonce: settings.nonce,
  439. action: settings.action,
  440. id: settings.element.split( '-' ).pop(),
  441. dimClass: settings.dimClass
  442. }, wpAjax.unserialize( data[6] || '' ) );
  443. if ( typeof settings.dimBefore === 'function' ) {
  444. settings = settings.dimBefore( settings );
  445. if ( ! settings ) {
  446. return true;
  447. }
  448. }
  449. $eventTarget = $( '#' + settings.element );
  450. isClass = $eventTarget.toggleClass( settings.dimClass ).is( '.' + settings.dimClass );
  451. color = wpList.getColor( $eventTarget );
  452. dimColor = isClass ? settings.dimAddColor : settings.dimDelColor;
  453. $eventTarget.toggleClass( settings.dimClass );
  454. if ( 'none' !== dimColor ) {
  455. $eventTarget
  456. .animate( { backgroundColor: dimColor }, 'fast' )
  457. .queue( function() {
  458. $eventTarget.toggleClass( settings.dimClass );
  459. $( this ).dequeue();
  460. } )
  461. .animate( { backgroundColor: color }, {
  462. complete: function() {
  463. $( this ).css( 'backgroundColor', '' );
  464. $( list ).trigger( 'wpListDimEnd', [ settings, list.wpList ] );
  465. }
  466. } );
  467. } else {
  468. $( list ).trigger( 'wpListDimEnd', [ settings, list.wpList ] );
  469. }
  470. if ( ! settings.data._ajax_nonce ) {
  471. return true;
  472. }
  473. settings.success = function( response ) {
  474. parsedResponse = wpAjax.parseAjaxResponse( response, settings.response, settings.element );
  475. returnedResponse = response;
  476. if ( true === parsedResponse ) {
  477. return true;
  478. }
  479. if ( ! parsedResponse || parsedResponse.errors ) {
  480. $eventTarget.stop().stop().css( 'backgroundColor', '#ff3333' )[isClass ? 'removeClass' : 'addClass']( settings.dimClass ).show().queue( function() {
  481. list.wpList.recolor();
  482. $( this ).dequeue();
  483. } );
  484. return false;
  485. }
  486. /** @property {string} comment_link Link of the comment to be dimmed. */
  487. if ( 'undefined' !== typeof parsedResponse.responses[0].supplemental.comment_link ) {
  488. var $submittedOn = $element.find( '.submitted-on' ),
  489. $commentLink = $submittedOn.find( 'a' );
  490. // Comment is approved; link the date field.
  491. if ( '' !== parsedResponse.responses[0].supplemental.comment_link ) {
  492. $submittedOn.html( $('<a></a>').text( $submittedOn.text() ).prop( 'href', parsedResponse.responses[0].supplemental.comment_link ) );
  493. // Comment is not approved; unlink the date field.
  494. } else if ( $commentLink.length ) {
  495. $submittedOn.text( $commentLink.text() );
  496. }
  497. }
  498. };
  499. settings.complete = function( jqXHR, status ) {
  500. if ( typeof settings.dimAfter === 'function' ) {
  501. $eventTarget.queue( function() {
  502. settings.dimAfter( returnedResponse, $.extend( {
  503. xml: jqXHR,
  504. status: status,
  505. parsed: parsedResponse
  506. }, settings ) );
  507. } ).dequeue();
  508. }
  509. };
  510. $.ajax( settings );
  511. return false;
  512. },
  513. /**
  514. * Returns the background color of the passed element.
  515. *
  516. * @param {jQuery|string} element Element to check.
  517. * @return {string} Background color value in HEX. Default: '#ffffff'.
  518. */
  519. getColor: function( element ) {
  520. return $( element ).css( 'backgroundColor' ) || '#ffffff';
  521. },
  522. /**
  523. * Adds something.
  524. *
  525. * @param {HTMLElement} element A DOM element containing item data.
  526. * @param {Object} settings Settings for this list.
  527. * @return {boolean} Whether the item was added.
  528. */
  529. add: function( element, settings ) {
  530. var $list = $( this ),
  531. $element = $( element ),
  532. old = false,
  533. position, reference;
  534. if ( 'string' === typeof settings ) {
  535. settings = { what: settings };
  536. }
  537. settings = $.extend( { position: 0, id: 0, oldId: null }, this.wpList.settings, settings );
  538. if ( ! $element.length || ! settings.what ) {
  539. return false;
  540. }
  541. if ( settings.oldId ) {
  542. old = $( '#' + settings.what + '-' + settings.oldId );
  543. }
  544. if ( settings.id && ( settings.id !== settings.oldId || ! old || ! old.length ) ) {
  545. $( '#' + settings.what + '-' + settings.id ).remove();
  546. }
  547. if ( old && old.length ) {
  548. old.before( $element );
  549. old.remove();
  550. } else if ( isNaN( settings.position ) ) {
  551. position = 'after';
  552. if ( '-' === settings.position.substr( 0, 1 ) ) {
  553. settings.position = settings.position.substr( 1 );
  554. position = 'before';
  555. }
  556. reference = $list.find( '#' + settings.position );
  557. if ( 1 === reference.length ) {
  558. reference[position]( $element );
  559. } else {
  560. $list.append( $element );
  561. }
  562. } else if ( 'comment' !== settings.what || 0 === $( '#' + settings.element ).length ) {
  563. if ( settings.position < 0 ) {
  564. $list.prepend( $element );
  565. } else {
  566. $list.append( $element );
  567. }
  568. }
  569. if ( settings.alt ) {
  570. $element.toggleClass( settings.alt, ( $list.children( ':visible' ).index( $element[0] ) + settings.altOffset ) % 2 );
  571. }
  572. if ( 'none' !== settings.addColor ) {
  573. $element.css( 'backgroundColor', settings.addColor ).animate( { backgroundColor: wpList.getColor( $element ) }, {
  574. complete: function() {
  575. $( this ).css( 'backgroundColor', '' );
  576. }
  577. } );
  578. }
  579. // Add event handlers.
  580. $list.each( function( index, list ) {
  581. list.wpList.process( $element );
  582. } );
  583. return $element;
  584. },
  585. /**
  586. * Clears all input fields within the element passed.
  587. *
  588. * @param {string} elementId ID of the element to check, including leading #.
  589. */
  590. clear: function( elementId ) {
  591. var list = this,
  592. $element = $( elementId ),
  593. type, tagName;
  594. // Bail if we're within the list.
  595. if ( list.wpList && $element.parents( '#' + list.id ).length ) {
  596. return;
  597. }
  598. // Check each input field.
  599. $element.find( ':input' ).each( function( index, input ) {
  600. // Bail if the form was marked to not to be cleared.
  601. if ( $( input ).parents( '.form-no-clear' ).length ) {
  602. return;
  603. }
  604. type = input.type.toLowerCase();
  605. tagName = input.tagName.toLowerCase();
  606. if ( 'text' === type || 'password' === type || 'textarea' === tagName ) {
  607. input.value = '';
  608. } else if ( 'checkbox' === type || 'radio' === type ) {
  609. input.checked = false;
  610. } else if ( 'select' === tagName ) {
  611. input.selectedIndex = null;
  612. }
  613. } );
  614. },
  615. /**
  616. * Registers event handlers to add, delete, and dim items.
  617. *
  618. * @param {string} elementId
  619. */
  620. process: function( elementId ) {
  621. var list = this,
  622. $element = $( elementId || document );
  623. $element.on( 'submit', 'form[data-wp-lists^="add:' + list.id + ':"]', function() {
  624. return list.wpList.add( this );
  625. } );
  626. $element.on( 'click', 'a[data-wp-lists^="add:' + list.id + ':"], input[data-wp-lists^="add:' + list.id + ':"]', function() {
  627. return list.wpList.add( this );
  628. } );
  629. $element.on( 'click', '[data-wp-lists^="delete:' + list.id + ':"]', function() {
  630. return list.wpList.del( this );
  631. } );
  632. $element.on( 'click', '[data-wp-lists^="dim:' + list.id + ':"]', function() {
  633. return list.wpList.dim( this );
  634. } );
  635. },
  636. /**
  637. * Updates list item background colors.
  638. */
  639. recolor: function() {
  640. var list = this,
  641. evenOdd = [':even', ':odd'],
  642. items;
  643. // Bail if there is no alternate class name specified.
  644. if ( ! list.wpList.settings.alt ) {
  645. return;
  646. }
  647. items = $( '.list-item:visible', list );
  648. if ( ! items.length ) {
  649. items = $( list ).children( ':visible' );
  650. }
  651. if ( list.wpList.settings.altOffset % 2 ) {
  652. evenOdd.reverse();
  653. }
  654. items.filter( evenOdd[0] ).addClass( list.wpList.settings.alt ).end();
  655. items.filter( evenOdd[1] ).removeClass( list.wpList.settings.alt );
  656. },
  657. /**
  658. * Sets up `process()` and `recolor()` functions.
  659. */
  660. init: function() {
  661. var $list = this;
  662. $list.wpList.process = function( element ) {
  663. $list.each( function() {
  664. this.wpList.process( element );
  665. } );
  666. };
  667. $list.wpList.recolor = function() {
  668. $list.each( function() {
  669. this.wpList.recolor();
  670. } );
  671. };
  672. }
  673. };
  674. /**
  675. * Initializes wpList object.
  676. *
  677. * @param {Object} settings
  678. * @param {string} settings.url URL for ajax calls. Default: ajaxurl.
  679. * @param {string} settings.type The HTTP method to use for Ajax requests. Default: 'POST'.
  680. * @param {string} settings.response ID of the element the parsed ajax response will be stored in.
  681. * Default: 'ajax-response'.
  682. *
  683. * @param {string} settings.what Default: ''.
  684. * @param {string} settings.alt CSS class name for alternate styling. Default: 'alternate'.
  685. * @param {number} settings.altOffset Offset to start alternate styling from. Default: 0.
  686. * @param {string} settings.addColor Hex code or 'none' to disable animation. Default: '#ffff33'.
  687. * @param {string} settings.delColor Hex code or 'none' to disable animation. Default: '#faafaa'.
  688. * @param {string} settings.dimAddColor Hex code or 'none' to disable animation. Default: '#ffff33'.
  689. * @param {string} settings.dimDelColor Hex code or 'none' to disable animation. Default: '#ff3333'.
  690. *
  691. * @param {wpList~confirm} settings.confirm Callback that's run before a request is made. Default: null.
  692. * @param {wpList~addBefore} settings.addBefore Callback that's run before an item gets added to the list.
  693. * Default: null.
  694. * @param {wpList~addAfter} settings.addAfter Callback that's run after an item got added to the list.
  695. * Default: null.
  696. * @param {wpList~delBefore} settings.delBefore Callback that's run before an item gets deleted from the list.
  697. * Default: null.
  698. * @param {wpList~delAfter} settings.delAfter Callback that's run after an item got deleted from the list.
  699. * Default: null.
  700. * @param {wpList~dimBefore} settings.dimBefore Callback that's run before an item gets dim'd. Default: null.
  701. * @param {wpList~dimAfter} settings.dimAfter Callback that's run after an item got dim'd. Default: null.
  702. * @return {$.fn} wpList API function.
  703. */
  704. $.fn.wpList = function( settings ) {
  705. this.each( function( index, list ) {
  706. list.wpList = {
  707. settings: $.extend( {}, wpList.settings, { what: wpList.parseData( list, 'list' )[1] || '' }, settings )
  708. };
  709. $.each( functions, function( func, callback ) {
  710. list.wpList[func] = function( element, setting ) {
  711. return wpList[callback].call( list, element, setting );
  712. };
  713. } );
  714. } );
  715. wpList.init.call( this );
  716. this.wpList.process();
  717. return this;
  718. };
  719. } ) ( jQuery );