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