PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/js/common.js

https://gitlab.com/Gashler/dp
JavaScript | 433 lines | 326 code | 76 blank | 31 comment | 75 complexity | 03eaffeef39119f0523a49c7f9767f3b MD5 | raw file
  1. var showNotice, adminMenu, columns, validateForm, screenMeta;
  2. (function($){
  3. // Removed in 3.3.
  4. // (perhaps) needed for back-compat
  5. adminMenu = {
  6. init : function() {},
  7. fold : function() {},
  8. restoreMenuState : function() {},
  9. toggle : function() {},
  10. favorites : function() {}
  11. };
  12. // show/hide/save table columns
  13. columns = {
  14. init : function() {
  15. var that = this;
  16. $('.hide-column-tog', '#adv-settings').click( function() {
  17. var $t = $(this), column = $t.val();
  18. if ( $t.prop('checked') )
  19. that.checked(column);
  20. else
  21. that.unchecked(column);
  22. columns.saveManageColumnsState();
  23. });
  24. },
  25. saveManageColumnsState : function() {
  26. var hidden = this.hidden();
  27. $.post(ajaxurl, {
  28. action: 'hidden-columns',
  29. hidden: hidden,
  30. screenoptionnonce: $('#screenoptionnonce').val(),
  31. page: pagenow
  32. });
  33. },
  34. checked : function(column) {
  35. $('.column-' + column).show();
  36. this.colSpanChange(+1);
  37. },
  38. unchecked : function(column) {
  39. $('.column-' + column).hide();
  40. this.colSpanChange(-1);
  41. },
  42. hidden : function() {
  43. return $('.manage-column').filter(':hidden').map(function() { return this.id; }).get().join(',');
  44. },
  45. useCheckboxesForHidden : function() {
  46. this.hidden = function(){
  47. return $('.hide-column-tog').not(':checked').map(function() {
  48. var id = this.id;
  49. return id.substring( id, id.length - 5 );
  50. }).get().join(',');
  51. };
  52. },
  53. colSpanChange : function(diff) {
  54. var $t = $('table').find('.colspanchange'), n;
  55. if ( !$t.length )
  56. return;
  57. n = parseInt( $t.attr('colspan'), 10 ) + diff;
  58. $t.attr('colspan', n.toString());
  59. }
  60. }
  61. $(document).ready(function(){columns.init();});
  62. validateForm = function( form ) {
  63. return !$( form ).find('.form-required').filter( function() { return $('input:visible', this).val() == ''; } ).addClass( 'form-invalid' ).find('input:visible').change( function() { $(this).closest('.form-invalid').removeClass( 'form-invalid' ); } ).size();
  64. }
  65. // stub for doing better warnings
  66. showNotice = {
  67. warn : function() {
  68. var msg = commonL10n.warnDelete || '';
  69. if ( confirm(msg) ) {
  70. return true;
  71. }
  72. return false;
  73. },
  74. note : function(text) {
  75. alert(text);
  76. }
  77. };
  78. screenMeta = {
  79. element: null, // #screen-meta
  80. toggles: null, // .screen-meta-toggle
  81. page: null, // #wpcontent
  82. init: function() {
  83. this.element = $('#screen-meta');
  84. this.toggles = $('.screen-meta-toggle a');
  85. this.page = $('#wpcontent');
  86. this.toggles.click( this.toggleEvent );
  87. },
  88. toggleEvent: function( e ) {
  89. var panel = $( this.href.replace(/.+#/, '#') );
  90. e.preventDefault();
  91. if ( !panel.length )
  92. return;
  93. if ( panel.is(':visible') )
  94. screenMeta.close( panel, $(this) );
  95. else
  96. screenMeta.open( panel, $(this) );
  97. },
  98. open: function( panel, link ) {
  99. $('.screen-meta-toggle').not( link.parent() ).css('visibility', 'hidden');
  100. panel.parent().show();
  101. panel.slideDown( 'fast', function() {
  102. panel.focus();
  103. link.addClass('screen-meta-active').attr('aria-expanded', true);
  104. });
  105. },
  106. close: function( panel, link ) {
  107. panel.slideUp( 'fast', function() {
  108. link.removeClass('screen-meta-active').attr('aria-expanded', false);
  109. $('.screen-meta-toggle').css('visibility', '');
  110. panel.parent().hide();
  111. });
  112. }
  113. };
  114. /**
  115. * Help tabs.
  116. */
  117. $('.contextual-help-tabs').delegate('a', 'click focus', function(e) {
  118. var link = $(this),
  119. panel;
  120. e.preventDefault();
  121. // Don't do anything if the click is for the tab already showing.
  122. if ( link.is('.active a') )
  123. return false;
  124. // Links
  125. $('.contextual-help-tabs .active').removeClass('active');
  126. link.parent('li').addClass('active');
  127. panel = $( link.attr('href') );
  128. // Panels
  129. $('.help-tab-content').not( panel ).removeClass('active').hide();
  130. panel.addClass('active').show();
  131. });
  132. $(document).ready( function() {
  133. var lastClicked = false, checks, first, last, checked, menu = $('#adminmenu'), mobileEvent,
  134. pageInput = $('input.current-page'), currentPage = pageInput.val();
  135. // when the menu is folded, make the fly-out submenu header clickable
  136. menu.on('click.wp-submenu-head', '.wp-submenu-head', function(e){
  137. $(e.target).parent().siblings('a').get(0).click();
  138. });
  139. $('#collapse-menu').on('click.collapse-menu', function(e){
  140. var body = $( document.body ), respWidth;
  141. // reset any compensation for submenus near the bottom of the screen
  142. $('#adminmenu div.wp-submenu').css('margin-top', '');
  143. // WebKit excludes the width of the vertical scrollbar when applying the CSS "@media screen and (max-width: ...)"
  144. // and matches $(window).width().
  145. // Firefox and IE > 8 include the scrollbar width, so after the jQuery normalization
  146. // $(window).width() is 884px but window.innerWidth is 900px.
  147. // (using window.innerWidth also excludes IE < 9)
  148. respWidth = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $(window).width() : window.innerWidth;
  149. if ( respWidth && respWidth < 900 ) {
  150. if ( body.hasClass('auto-fold') ) {
  151. body.removeClass('auto-fold').removeClass('folded');
  152. setUserSetting('unfold', 1);
  153. setUserSetting('mfold', 'o');
  154. } else {
  155. body.addClass('auto-fold');
  156. setUserSetting('unfold', 0);
  157. }
  158. } else {
  159. if ( body.hasClass('folded') ) {
  160. body.removeClass('folded');
  161. setUserSetting('mfold', 'o');
  162. } else {
  163. body.addClass('folded');
  164. setUserSetting('mfold', 'f');
  165. }
  166. }
  167. });
  168. if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device
  169. // iOS Safari works with touchstart, the rest work with click
  170. mobileEvent = /Mobile\/.+Safari/.test(navigator.userAgent) ? 'touchstart' : 'click';
  171. // close any open submenus when touch/click is not on the menu
  172. $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
  173. if ( !$(e.target).closest('#adminmenu').length )
  174. menu.find('li.wp-has-submenu.opensub').removeClass('opensub');
  175. });
  176. menu.find('a.wp-has-submenu').on( mobileEvent+'.wp-mobile-hover', function(e) {
  177. var el = $(this), parent = el.parent();
  178. // Show the sub instead of following the link if:
  179. // - the submenu is not open
  180. // - the submenu is not shown inline or the menu is not folded
  181. if ( !parent.hasClass('opensub') && ( !parent.hasClass('wp-menu-open') || parent.width() < 40 ) ) {
  182. e.preventDefault();
  183. menu.find('li.opensub').removeClass('opensub');
  184. parent.addClass('opensub');
  185. }
  186. });
  187. }
  188. menu.find('li.wp-has-submenu').hoverIntent({
  189. over: function(e){
  190. var b, h, o, f, m = $(this).find('.wp-submenu'), menutop, wintop, maxtop, top = parseInt( m.css('top'), 10 );
  191. if ( isNaN(top) || top > -5 ) // meaning the submenu is visible
  192. return;
  193. menutop = $(this).offset().top;
  194. wintop = $(window).scrollTop();
  195. maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
  196. b = menutop + m.height() + 1; // Bottom offset of the menu
  197. h = $('#wpwrap').height(); // Height of the entire page
  198. o = 60 + b - h;
  199. f = $(window).height() + wintop - 15; // The fold
  200. if ( f < (b - o) )
  201. o = b - f;
  202. if ( o > maxtop )
  203. o = maxtop;
  204. if ( o > 1 )
  205. m.css('margin-top', '-'+o+'px');
  206. else
  207. m.css('margin-top', '');
  208. menu.find('li.menu-top').removeClass('opensub');
  209. $(this).addClass('opensub');
  210. },
  211. out: function(){
  212. $(this).removeClass('opensub').find('.wp-submenu').css('margin-top', '');
  213. },
  214. timeout: 200,
  215. sensitivity: 7,
  216. interval: 90
  217. });
  218. menu.on('focus.adminmenu', '.wp-submenu a', function(e){
  219. $(e.target).closest('li.menu-top').addClass('opensub');
  220. }).on('blur.adminmenu', '.wp-submenu a', function(e){
  221. $(e.target).closest('li.menu-top').removeClass('opensub');
  222. });
  223. // Move .updated and .error alert boxes. Don't move boxes designed to be inline.
  224. $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
  225. $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
  226. // Init screen meta
  227. screenMeta.init();
  228. // check all checkboxes
  229. $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
  230. if ( 'undefined' == e.shiftKey ) { return true; }
  231. if ( e.shiftKey ) {
  232. if ( !lastClicked ) { return true; }
  233. checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
  234. first = checks.index( lastClicked );
  235. last = checks.index( this );
  236. checked = $(this).prop('checked');
  237. if ( 0 < first && 0 < last && first != last ) {
  238. checks.slice( first, last ).prop( 'checked', function(){
  239. if ( $(this).closest('tr').is(':visible') )
  240. return checked;
  241. return false;
  242. });
  243. }
  244. }
  245. lastClicked = this;
  246. // toggle "check all" checkboxes
  247. var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible').not(':checked');
  248. $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
  249. return ( 0 == unchecked.length );
  250. });
  251. return true;
  252. });
  253. $('thead, tfoot').find('.check-column :checkbox').click( function(e) {
  254. var c = $(this).prop('checked'),
  255. kbtoggle = 'undefined' == typeof toggleWithKeyboard ? false : toggleWithKeyboard,
  256. toggle = e.shiftKey || kbtoggle;
  257. $(this).closest( 'table' ).children( 'tbody' ).filter(':visible')
  258. .children().children('.check-column').find(':checkbox')
  259. .prop('checked', function() {
  260. if ( $(this).is(':hidden') )
  261. return false;
  262. if ( toggle )
  263. return $(this).prop( 'checked' );
  264. else if (c)
  265. return true;
  266. return false;
  267. });
  268. $(this).closest('table').children('thead, tfoot').filter(':visible')
  269. .children().children('.check-column').find(':checkbox')
  270. .prop('checked', function() {
  271. if ( toggle )
  272. return false;
  273. else if (c)
  274. return true;
  275. return false;
  276. });
  277. });
  278. $('#default-password-nag-no').click( function() {
  279. setUserSetting('default_password_nag', 'hide');
  280. $('div.default-password-nag').hide();
  281. return false;
  282. });
  283. // tab in textareas
  284. $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
  285. var el = e.target, selStart, selEnd, val, scroll, sel;
  286. if ( e.keyCode == 27 ) { // escape key
  287. $(el).data('tab-out', true);
  288. return;
  289. }
  290. if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
  291. return;
  292. if ( $(el).data('tab-out') ) {
  293. $(el).data('tab-out', false);
  294. return;
  295. }
  296. selStart = el.selectionStart;
  297. selEnd = el.selectionEnd;
  298. val = el.value;
  299. try {
  300. this.lastKey = 9; // not a standard DOM property, lastKey is to help stop Opera tab event. See blur handler below.
  301. } catch(err) {}
  302. if ( document.selection ) {
  303. el.focus();
  304. sel = document.selection.createRange();
  305. sel.text = '\t';
  306. } else if ( selStart >= 0 ) {
  307. scroll = this.scrollTop;
  308. el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
  309. el.selectionStart = el.selectionEnd = selStart + 1;
  310. this.scrollTop = scroll;
  311. }
  312. if ( e.stopPropagation )
  313. e.stopPropagation();
  314. if ( e.preventDefault )
  315. e.preventDefault();
  316. });
  317. $('#newcontent').bind('blur.wpevent_InsertTab', function(e) {
  318. if ( this.lastKey && 9 == this.lastKey )
  319. this.focus();
  320. });
  321. if ( pageInput.length ) {
  322. pageInput.closest('form').submit( function(e){
  323. // Reset paging var for new filters/searches but not for bulk actions. See #17685.
  324. if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
  325. pageInput.val('1');
  326. });
  327. }
  328. // Scroll into view when focused
  329. $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
  330. if ( e.target.scrollIntoView )
  331. e.target.scrollIntoView(false);
  332. });
  333. // Disable upload buttons until files are selected
  334. (function(){
  335. var button, input, form = $('form.wp-upload-form');
  336. if ( ! form.length )
  337. return;
  338. button = form.find('input[type="submit"]');
  339. input = form.find('input[type="file"]');
  340. function toggleUploadButton() {
  341. button.prop('disabled', '' === input.map( function() {
  342. return $(this).val();
  343. }).get().join(''));
  344. }
  345. toggleUploadButton();
  346. input.on('change', toggleUploadButton);
  347. })();
  348. });
  349. // internal use
  350. $(document).bind( 'wp_CloseOnEscape', function( e, data ) {
  351. if ( typeof(data.cb) != 'function' )
  352. return;
  353. if ( typeof(data.condition) != 'function' || data.condition() )
  354. data.cb();
  355. return true;
  356. });
  357. })(jQuery);