gallery.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @output wp-admin/js/gallery.js
  3. */
  4. /* global unescape, getUserSetting, setUserSetting, wpgallery, tinymce */
  5. jQuery( function($) {
  6. var gallerySortable, gallerySortableInit, sortIt, clearAll, w, desc = false;
  7. gallerySortableInit = function() {
  8. gallerySortable = $('#media-items').sortable( {
  9. items: 'div.media-item',
  10. placeholder: 'sorthelper',
  11. axis: 'y',
  12. distance: 2,
  13. handle: 'div.filename',
  14. stop: function() {
  15. // When an update has occurred, adjust the order for each item.
  16. var all = $('#media-items').sortable('toArray'), len = all.length;
  17. $.each(all, function(i, id) {
  18. var order = desc ? (len - i) : (1 + i);
  19. $('#' + id + ' .menu_order input').val(order);
  20. });
  21. }
  22. } );
  23. };
  24. sortIt = function() {
  25. var all = $('.menu_order_input'), len = all.length;
  26. all.each(function(i){
  27. var order = desc ? (len - i) : (1 + i);
  28. $(this).val(order);
  29. });
  30. };
  31. clearAll = function(c) {
  32. c = c || 0;
  33. $('.menu_order_input').each( function() {
  34. if ( this.value === '0' || c ) {
  35. this.value = '';
  36. }
  37. });
  38. };
  39. $('#asc').on( 'click', function( e ) {
  40. e.preventDefault();
  41. desc = false;
  42. sortIt();
  43. });
  44. $('#desc').on( 'click', function( e ) {
  45. e.preventDefault();
  46. desc = true;
  47. sortIt();
  48. });
  49. $('#clear').on( 'click', function( e ) {
  50. e.preventDefault();
  51. clearAll(1);
  52. });
  53. $('#showall').on( 'click', function( e ) {
  54. e.preventDefault();
  55. $('#sort-buttons span a').toggle();
  56. $('a.describe-toggle-on').hide();
  57. $('a.describe-toggle-off, table.slidetoggle').show();
  58. $('img.pinkynail').toggle(false);
  59. });
  60. $('#hideall').on( 'click', function( e ) {
  61. e.preventDefault();
  62. $('#sort-buttons span a').toggle();
  63. $('a.describe-toggle-on').show();
  64. $('a.describe-toggle-off, table.slidetoggle').hide();
  65. $('img.pinkynail').toggle(true);
  66. });
  67. // Initialize sortable.
  68. gallerySortableInit();
  69. clearAll();
  70. if ( $('#media-items>*').length > 1 ) {
  71. w = wpgallery.getWin();
  72. $('#save-all, #gallery-settings').show();
  73. if ( typeof w.tinyMCE !== 'undefined' && w.tinyMCE.activeEditor && ! w.tinyMCE.activeEditor.isHidden() ) {
  74. wpgallery.mcemode = true;
  75. wpgallery.init();
  76. } else {
  77. $('#insert-gallery').show();
  78. }
  79. }
  80. });
  81. jQuery(window).on( 'unload', function () { window.tinymce = window.tinyMCE = window.wpgallery = null; } ); // Cleanup.
  82. /* gallery settings */
  83. window.tinymce = null;
  84. window.wpgallery = {
  85. mcemode : false,
  86. editor : {},
  87. dom : {},
  88. is_update : false,
  89. el : {},
  90. I : function(e) {
  91. return document.getElementById(e);
  92. },
  93. init: function() {
  94. var t = this, li, q, i, it, w = t.getWin();
  95. if ( ! t.mcemode ) {
  96. return;
  97. }
  98. li = ('' + document.location.search).replace(/^\?/, '').split('&');
  99. q = {};
  100. for (i=0; i<li.length; i++) {
  101. it = li[i].split('=');
  102. q[unescape(it[0])] = unescape(it[1]);
  103. }
  104. if ( q.mce_rdomain ) {
  105. document.domain = q.mce_rdomain;
  106. }
  107. // Find window & API.
  108. window.tinymce = w.tinymce;
  109. window.tinyMCE = w.tinyMCE;
  110. t.editor = tinymce.EditorManager.activeEditor;
  111. t.setup();
  112. },
  113. getWin : function() {
  114. return window.dialogArguments || opener || parent || top;
  115. },
  116. setup : function() {
  117. var t = this, a, ed = t.editor, g, columns, link, order, orderby;
  118. if ( ! t.mcemode ) {
  119. return;
  120. }
  121. t.el = ed.selection.getNode();
  122. if ( t.el.nodeName !== 'IMG' || ! ed.dom.hasClass(t.el, 'wpGallery') ) {
  123. if ( ( g = ed.dom.select('img.wpGallery') ) && g[0] ) {
  124. t.el = g[0];
  125. } else {
  126. if ( getUserSetting('galfile') === '1' ) {
  127. t.I('linkto-file').checked = 'checked';
  128. }
  129. if ( getUserSetting('galdesc') === '1' ) {
  130. t.I('order-desc').checked = 'checked';
  131. }
  132. if ( getUserSetting('galcols') ) {
  133. t.I('columns').value = getUserSetting('galcols');
  134. }
  135. if ( getUserSetting('galord') ) {
  136. t.I('orderby').value = getUserSetting('galord');
  137. }
  138. jQuery('#insert-gallery').show();
  139. return;
  140. }
  141. }
  142. a = ed.dom.getAttrib(t.el, 'title');
  143. a = ed.dom.decode(a);
  144. if ( a ) {
  145. jQuery('#update-gallery').show();
  146. t.is_update = true;
  147. columns = a.match(/columns=['"]([0-9]+)['"]/);
  148. link = a.match(/link=['"]([^'"]+)['"]/i);
  149. order = a.match(/order=['"]([^'"]+)['"]/i);
  150. orderby = a.match(/orderby=['"]([^'"]+)['"]/i);
  151. if ( link && link[1] ) {
  152. t.I('linkto-file').checked = 'checked';
  153. }
  154. if ( order && order[1] ) {
  155. t.I('order-desc').checked = 'checked';
  156. }
  157. if ( columns && columns[1] ) {
  158. t.I('columns').value = '' + columns[1];
  159. }
  160. if ( orderby && orderby[1] ) {
  161. t.I('orderby').value = orderby[1];
  162. }
  163. } else {
  164. jQuery('#insert-gallery').show();
  165. }
  166. },
  167. update : function() {
  168. var t = this, ed = t.editor, all = '', s;
  169. if ( ! t.mcemode || ! t.is_update ) {
  170. s = '[gallery' + t.getSettings() + ']';
  171. t.getWin().send_to_editor(s);
  172. return;
  173. }
  174. if ( t.el.nodeName !== 'IMG' ) {
  175. return;
  176. }
  177. all = ed.dom.decode( ed.dom.getAttrib( t.el, 'title' ) );
  178. all = all.replace(/\s*(order|link|columns|orderby)=['"]([^'"]+)['"]/gi, '');
  179. all += t.getSettings();
  180. ed.dom.setAttrib(t.el, 'title', all);
  181. t.getWin().tb_remove();
  182. },
  183. getSettings : function() {
  184. var I = this.I, s = '';
  185. if ( I('linkto-file').checked ) {
  186. s += ' link="file"';
  187. setUserSetting('galfile', '1');
  188. }
  189. if ( I('order-desc').checked ) {
  190. s += ' order="DESC"';
  191. setUserSetting('galdesc', '1');
  192. }
  193. if ( I('columns').value !== 3 ) {
  194. s += ' columns="' + I('columns').value + '"';
  195. setUserSetting('galcols', I('columns').value);
  196. }
  197. if ( I('orderby').value !== 'menu_order' ) {
  198. s += ' orderby="' + I('orderby').value + '"';
  199. setUserSetting('galord', I('orderby').value);
  200. }
  201. return s;
  202. }
  203. };