PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-includes/js/wp-lists.dev.js

https://github.com/enoex/artisanchairs
JavaScript | 357 lines | 302 code | 54 blank | 1 comment | 144 complexity | 3307a4b7fce5e39e3c13f0e3478cbb5d MD5 | raw file
  1. (function($) {
  2. var fs = {add:'ajaxAdd',del:'ajaxDel',dim:'ajaxDim',process:'process',recolor:'recolor'}, wpList;
  3. wpList = {
  4. settings: {
  5. url: wpListL10n.url, type: 'POST',
  6. response: 'ajax-response',
  7. what: '',
  8. alt: 'alternate', altOffset: 0,
  9. addColor: null, delColor: null, dimAddColor: null, dimDelColor: null,
  10. confirm: null,
  11. addBefore: null, addAfter: null,
  12. delBefore: null, delAfter: null,
  13. dimBefore: null, dimAfter: null
  14. },
  15. nonce: function(e,s) {
  16. var url = wpAjax.unserialize(e.attr('href'));
  17. return s.nonce || url._ajax_nonce || $('#' + s.element + ' input[name=_ajax_nonce]').val() || url._wpnonce || $('#' + s.element + ' input[name=_wpnonce]').val() || 0;
  18. },
  19. parseClass: function(e,t) {
  20. var c = [], cl;
  21. try {
  22. cl = $(e).attr('class') || '';
  23. cl = cl.match(new RegExp(t+':[\\S]+'));
  24. if ( cl ) { c = cl[0].split(':'); }
  25. } catch(r) {}
  26. return c;
  27. },
  28. pre: function(e,s,a) {
  29. var bg, r;
  30. s = $.extend( {}, this.wpList.settings, {
  31. element: null,
  32. nonce: 0,
  33. target: e.get(0)
  34. }, s || {} );
  35. if ( $.isFunction( s.confirm ) ) {
  36. if ( 'add' != a ) {
  37. bg = $('#' + s.element).css('backgroundColor');
  38. $('#' + s.element).css('backgroundColor', '#FF9966');
  39. }
  40. r = s.confirm.call(this,e,s,a,bg);
  41. if ( 'add' != a ) { $('#' + s.element).css('backgroundColor', bg ); }
  42. if ( !r ) { return false; }
  43. }
  44. return s;
  45. },
  46. ajaxAdd: function( e, s ) {
  47. e = $(e);
  48. s = s || {};
  49. var list = this, cls = wpList.parseClass(e,'add'), es, valid, formData;
  50. s = wpList.pre.call( list, e, s, 'add' );
  51. s.element = cls[2] || e.attr( 'id' ) || s.element || null;
  52. if ( cls[3] ) { s.addColor = '#' + cls[3]; }
  53. else { s.addColor = s.addColor || '#FFFF33'; }
  54. if ( !s ) { return false; }
  55. if ( !e.is("[class^=add:" + list.id + ":]") ) { return !wpList.add.call( list, e, s ); }
  56. if ( !s.element ) { return true; }
  57. s.action = 'add-' + s.what;
  58. s.nonce = wpList.nonce(e,s);
  59. es = $('#' + s.element + ' :input').not('[name=_ajax_nonce], [name=_wpnonce], [name=action]');
  60. valid = wpAjax.validateForm( '#' + s.element );
  61. if ( !valid ) { return false; }
  62. s.data = $.param( $.extend( { _ajax_nonce: s.nonce, action: s.action }, wpAjax.unserialize( cls[4] || '' ) ) );
  63. formData = $.isFunction(es.fieldSerialize) ? es.fieldSerialize() : es.serialize();
  64. if ( formData ) { s.data += '&' + formData; }
  65. if ( $.isFunction(s.addBefore) ) {
  66. s = s.addBefore( s );
  67. if ( !s ) { return true; }
  68. }
  69. if ( !s.data.match(/_ajax_nonce=[a-f0-9]+/) ) { return true; }
  70. s.success = function(r) {
  71. var res = wpAjax.parseAjaxResponse(r, s.response, s.element), o;
  72. if ( !res || res.errors ) { return false; }
  73. if ( true === res ) { return true; }
  74. jQuery.each( res.responses, function() {
  75. wpList.add.call( list, this.data, $.extend( {}, s, { // this.firstChild.nodevalue
  76. pos: this.position || 0,
  77. id: this.id || 0,
  78. oldId: this.oldId || null
  79. } ) );
  80. } );
  81. if ( $.isFunction(s.addAfter) ) {
  82. o = this.complete;
  83. this.complete = function(x,st) {
  84. var _s = $.extend( { xml: x, status: st, parsed: res }, s );
  85. s.addAfter( r, _s );
  86. if ( $.isFunction(o) ) { o(x,st); }
  87. };
  88. }
  89. list.wpList.recolor();
  90. wpList.clear.call(list,'#' + s.element);
  91. };
  92. $.ajax( s );
  93. return false;
  94. },
  95. ajaxDel: function( e, s ) {
  96. e = $(e); s = s || {};
  97. var list = this, cls = wpList.parseClass(e,'delete'), element, anim;
  98. s = wpList.pre.call( list, e, s, 'delete' );
  99. s.element = cls[2] || s.element || null;
  100. if ( cls[3] ) { s.delColor = '#' + cls[3]; }
  101. else { s.delColor = s.delColor || '#FF3333'; }
  102. if ( !s || !s.element ) { return false; }
  103. s.action = 'delete-' + s.what;
  104. s.nonce = wpList.nonce(e,s);
  105. s.data = $.extend(
  106. { action: s.action, id: s.element.split('-').pop(), _ajax_nonce: s.nonce },
  107. wpAjax.unserialize( cls[4] || '' )
  108. );
  109. if ( $.isFunction(s.delBefore) ) {
  110. s = s.delBefore( s );
  111. if ( !s ) { return true; }
  112. }
  113. if ( !s.data._ajax_nonce ) { return true; }
  114. element = $('#' + s.element);
  115. if ( 'none' != s.delColor ) {
  116. anim = 'slideUp';
  117. if ( element.css( 'display' ).match(/table/) )
  118. anim = 'fadeOut'; // Can't slideup table rows and other table elements. Known jQuery bug
  119. element
  120. .animate( { backgroundColor: s.delColor }, 'fast' )[anim]( 'fast' )
  121. .queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
  122. } else {
  123. list.wpList.recolor();
  124. }
  125. s.success = function(r) {
  126. var res = wpAjax.parseAjaxResponse(r, s.response, s.element), o;
  127. if ( !res || res.errors ) {
  128. element.stop().stop().css( 'backgroundColor', '#FF3333' ).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
  129. return false;
  130. }
  131. if ( $.isFunction(s.delAfter) ) {
  132. o = this.complete;
  133. this.complete = function(x,st) {
  134. element.queue( function() {
  135. var _s = $.extend( { xml: x, status: st, parsed: res }, s );
  136. s.delAfter( r, _s );
  137. if ( $.isFunction(o) ) { o(x,st); }
  138. } ).dequeue();
  139. };
  140. }
  141. };
  142. $.ajax( s );
  143. return false;
  144. },
  145. ajaxDim: function( e, s ) {
  146. if ( $(e).parent().css('display') == 'none' ) // Prevent hidden links from being clicked by hotkeys
  147. return false;
  148. e = $(e); s = s || {};
  149. var list = this, cls = wpList.parseClass(e,'dim'), element, isClass, color, dimColor;
  150. s = wpList.pre.call( list, e, s, 'dim' );
  151. s.element = cls[2] || s.element || null;
  152. s.dimClass = cls[3] || s.dimClass || null;
  153. if ( cls[4] ) { s.dimAddColor = '#' + cls[4]; }
  154. else { s.dimAddColor = s.dimAddColor || '#FFFF33'; }
  155. if ( cls[5] ) { s.dimDelColor = '#' + cls[5]; }
  156. else { s.dimDelColor = s.dimDelColor || '#FF3333'; }
  157. if ( !s || !s.element || !s.dimClass ) { return true; }
  158. s.action = 'dim-' + s.what;
  159. s.nonce = wpList.nonce(e,s);
  160. s.data = $.extend(
  161. { action: s.action, id: s.element.split('-').pop(), dimClass: s.dimClass, _ajax_nonce : s.nonce },
  162. wpAjax.unserialize( cls[6] || '' )
  163. );
  164. if ( $.isFunction(s.dimBefore) ) {
  165. s = s.dimBefore( s );
  166. if ( !s ) { return true; }
  167. }
  168. element = $('#' + s.element);
  169. isClass = element.toggleClass(s.dimClass).is('.' + s.dimClass);
  170. color = wpList.getColor( element );
  171. element.toggleClass( s.dimClass )
  172. dimColor = isClass ? s.dimAddColor : s.dimDelColor;
  173. if ( 'none' != dimColor ) {
  174. element
  175. .animate( { backgroundColor: dimColor }, 'fast' )
  176. .queue( function() { element.toggleClass(s.dimClass); $(this).dequeue(); } )
  177. .animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
  178. }
  179. if ( !s.data._ajax_nonce ) { return true; }
  180. s.success = function(r) {
  181. var res = wpAjax.parseAjaxResponse(r, s.response, s.element), o;
  182. if ( !res || res.errors ) {
  183. element.stop().stop().css( 'backgroundColor', '#FF3333' )[isClass?'removeClass':'addClass'](s.dimClass).show().queue( function() { list.wpList.recolor(); $(this).dequeue(); } );
  184. return false;
  185. }
  186. if ( $.isFunction(s.dimAfter) ) {
  187. o = this.complete;
  188. this.complete = function(x,st) {
  189. element.queue( function() {
  190. var _s = $.extend( { xml: x, status: st, parsed: res }, s );
  191. s.dimAfter( r, _s );
  192. if ( $.isFunction(o) ) { o(x,st); }
  193. } ).dequeue();
  194. };
  195. }
  196. };
  197. $.ajax( s );
  198. return false;
  199. },
  200. // From jquery.color.js: jQuery Color Animation by John Resig
  201. getColor: function( el ) {
  202. if ( el.constructor == Object )
  203. el = el.get(0);
  204. var elem = el, color, rgbaTrans = new RegExp( "rgba\\(\\s*0,\\s*0,\\s*0,\\s*0\\s*\\)", "i" );
  205. do {
  206. color = jQuery.curCSS(elem, 'backgroundColor');
  207. if ( color != '' && color != 'transparent' && !color.match(rgbaTrans) || jQuery.nodeName(elem, "body") )
  208. break;
  209. } while ( elem = elem.parentNode );
  210. return color || '#ffffff';
  211. },
  212. add: function( e, s ) {
  213. e = $(e);
  214. var list = $(this), old = false, _s = { pos: 0, id: 0, oldId: null }, ba, ref, color;
  215. if ( 'string' == typeof s ) { s = { what: s }; }
  216. s = $.extend(_s, this.wpList.settings, s);
  217. if ( !e.size() || !s.what ) { return false; }
  218. if ( s.oldId ) { old = $('#' + s.what + '-' + s.oldId); }
  219. if ( s.id && ( s.id != s.oldId || !old || !old.size() ) ) { $('#' + s.what + '-' + s.id).remove(); }
  220. if ( old && old.size() ) {
  221. old.before(e);
  222. old.remove();
  223. } else if ( isNaN(s.pos) ) {
  224. ba = 'after';
  225. if ( '-' == s.pos.substr(0,1) ) {
  226. s.pos = s.pos.substr(1);
  227. ba = 'before';
  228. }
  229. ref = list.find( '#' + s.pos );
  230. if ( 1 === ref.size() ) { ref[ba](e); }
  231. else { list.append(e); }
  232. } else if ( s.pos < 0 ) {
  233. list.prepend(e);
  234. } else {
  235. list.append(e);
  236. }
  237. if ( s.alt ) {
  238. if ( ( list.children(':visible').index( e[0] ) + s.altOffset ) % 2 ) { e.removeClass( s.alt ); }
  239. else { e.addClass( s.alt ); }
  240. }
  241. if ( 'none' != s.addColor ) {
  242. color = wpList.getColor( e );
  243. e.css( 'backgroundColor', s.addColor ).animate( { backgroundColor: color }, { complete: function() { $(this).css( 'backgroundColor', '' ); } } );
  244. }
  245. list.each( function() { this.wpList.process( e ); } );
  246. return e;
  247. },
  248. clear: function(e) {
  249. var list = this, t, tag;
  250. e = $(e);
  251. if ( list.wpList && e.parents( '#' + list.id ).size() ) { return; }
  252. e.find(':input').each( function() {
  253. if ( $(this).parents('.form-no-clear').size() )
  254. return;
  255. t = this.type.toLowerCase();
  256. tag = this.tagName.toLowerCase();
  257. if ( 'text' == t || 'password' == t || 'textarea' == tag ) { this.value = ''; }
  258. else if ( 'checkbox' == t || 'radio' == t ) { this.checked = false; }
  259. else if ( 'select' == tag ) { this.selectedIndex = null; }
  260. });
  261. },
  262. process: function(el) {
  263. var list = this;
  264. $("[class^=add:" + list.id + ":]", el || null)
  265. .filter('form').submit( function() { return list.wpList.add(this); } ).end()
  266. .not('form').click( function() { return list.wpList.add(this); } );
  267. $("[class^=delete:" + list.id + ":]", el || null).click( function() { return list.wpList.del(this); } );
  268. $("[class^=dim:" + list.id + ":]", el || null).click( function() { return list.wpList.dim(this); } );
  269. },
  270. recolor: function() {
  271. var list = this, items, eo;
  272. if ( !list.wpList.settings.alt ) { return; }
  273. items = $('.list-item:visible', list);
  274. if ( !items.size() ) { items = $(list).children(':visible'); }
  275. eo = [':even',':odd'];
  276. if ( list.wpList.settings.altOffset % 2 ) { eo.reverse(); }
  277. items.filter(eo[0]).addClass(list.wpList.settings.alt).end().filter(eo[1]).removeClass(list.wpList.settings.alt);
  278. },
  279. init: function() {
  280. var lists = this;
  281. lists.wpList.process = function(a) {
  282. lists.each( function() {
  283. this.wpList.process(a);
  284. } );
  285. };
  286. lists.wpList.recolor = function() {
  287. lists.each( function() {
  288. this.wpList.recolor();
  289. } );
  290. };
  291. }
  292. };
  293. $.fn.wpList = function( settings ) {
  294. this.each( function() {
  295. var _this = this;
  296. this.wpList = { settings: $.extend( {}, wpList.settings, { what: wpList.parseClass(this,'list')[1] || '' }, settings ) };
  297. $.each( fs, function(i,f) { _this.wpList[i] = function( e, s ) { return wpList[f].call( _this, e, s ); }; } );
  298. } );
  299. wpList.init.call(this);
  300. this.wpList.process();
  301. return this;
  302. };
  303. })(jQuery);