]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/upload.js
Wordpress 2.3.3
[autoinstalls/wordpress.git] / wp-admin / js / upload.js
1 addLoadEvent( function() {
2         theFileList = {
3                 currentImage: {ID: 0},
4                 nonce: '',
5                 tab: '',
6                 postID: 0,
7
8                 toQueryParams: function(qryStrOrig) {
9                         var params = new Object();
10                         var qryStr = qryStrOrig;
11                         var i = 0;
12                         do {
13                                 params[qryStr.split("=")[0].replace(/&/, "")] = ( qryStr.split("=")[1] ) ? qryStr.split("=")[1].split(/&|$/)[0] : '';
14                                 qryStr = ( qryStr.split("=")[1] ) ? qryStr.split(qryStr.split("=")[1].split(/&|$/)[0])[1] : '';
15                                 i++;
16                         } 
17                         while(i < (qryStrOrig.split("=").length - 1));
18                         return params;
19                 },
20
21                 toQueryString: function(params) {
22                         var qryStr = '';
23                         for ( var key in params )
24                                 qryStr += key + '=' + params[key] + '&';
25                         return qryStr;
26                 },
27
28                 initializeVars: function() {
29                         this.urlData  = document.location.href.split('?');
30                         this.params = this.toQueryParams(this.urlData[1]);
31                         this.postID = this.params['post_id'];
32                         this.tab = this.params['tab'];
33                         this.style = this.params['style'];
34                         this.ID = this.params['ID'];
35                         if ( !this.style )
36                                 this.style = 'default';
37                         var nonceEl = jQuery('#nonce-value');
38                         if ( nonceEl )
39                                 this.nonce = jQuery(nonceEl).val();
40                         if ( this.ID ) {
41                                 this.grabImageData( this.ID );
42                                 this.imageView( this.ID );
43                         }
44                 },
45
46                 initializeLinks: function() {
47                         if ( this.ID )
48                                 return;
49                         jQuery('a.file-link').each(function() {
50                                 var id = jQuery(this).attr('id').split('-').pop();
51                                 jQuery(this).attr('href','javascript:void(0)').click(function(e) {
52                                         theFileList[ 'inline' == theFileList.style ? 'imageView' : 'editView' ](id, e);
53                                 });
54                         });
55                 },
56
57                 grabImageData: function(id) {
58                         if ( id == this.currentImage.ID )
59                                 return;
60                         var thumbEl = jQuery('#attachment-thumb-url-' + id);
61                         if ( thumbEl ) {
62                                 this.currentImage.thumb = ( 0 == id ? '' : jQuery(thumbEl).val() );
63                                 this.currentImage.thumbBase = ( 0 == id ? '' : jQuery('#attachment-thumb-url-base-' + id).val() );
64                         } else {
65                                 this.currentImage.thumb = false;
66                         }
67                         this.currentImage.src = ( 0 == id ? '' : jQuery('#attachment-url-' + id).val() );
68                         this.currentImage.srcBase = ( 0 == id ? '' : jQuery('#attachment-url-base-' + id).val() );
69                         this.currentImage.page = ( 0 == id ? '' : jQuery('#attachment-page-url-' + id).val() );
70                         this.currentImage.title = ( 0 == id ? '' : jQuery('#attachment-title-' + id).val() );
71                         this.currentImage.description = ( 0 == id ? '' : jQuery('#attachment-description-' + id).val() );
72                         var widthEl = jQuery('#attachment-width-' + id);
73                         if ( widthEl ) {
74                                 this.currentImage.width = ( 0 == id ? '' : jQuery(widthEl).val() );
75                                 this.currentImage.height = ( 0 == id ? '' : jQuery('#attachment-height-' + id).val() );
76                         } else {
77                                 this.currentImage.width = false;
78                                 this.currentImage.height = false;
79                         }
80                         this.currentImage.isImage = ( 0 == id ? 0 : jQuery('#attachment-is-image-' + id).val() );
81                         this.currentImage.ID = id;
82                 },
83
84                 imageView: function(id, e) {
85                         this.prepView(id);
86                         var h = '';
87
88                         h += "<div id='upload-file'>"
89                         if ( this.ID ) {
90                                 var params = this.params;
91                                 params.ID = '';
92                                 params.action = '';
93                                 h += "<a href='" + this.urlData[0] + '?' + this.toQueryString(params) + "'";
94                         } else {
95                                 h += "<a href='#' onclick='return theFileList.cancelView();'";
96                         }
97                         h += " title='" + this.browseTitle + "' class='back'>" + this.back + "</a>";
98                         h += "<div id='file-title'>"
99                         if ( 0 == this.currentImage.isImage )
100                                 h += "<h2><a href='" + this.currentImage.srcBase + this.currentImage.src + "' onclick='return false;' title='" + this.directTitle + "'>" + this.currentImage.title + "</a></h2>";
101                         else
102                                 h += "<h2>" + this.currentImage.title + "</h2>";
103                         h += " &#8212; <span>";
104                         h += "<a href='#' onclick='return theFileList.editView(" + id + ");'>" + this.edit + "</a>"
105                         h += "</span>";
106                         h += '</div>'
107                         h += "<div id='upload-file-view' class='alignleft'>";
108                         if ( 1 == this.currentImage.isImage ) {
109                                 h += "<a href='" + this.currentImage.srcBase + this.currentImage.src + "' onclick='return false;' title='" + this.directTitle + "'>";
110                                 h += "<img src='" + ( this.currentImage.thumb ? this.currentImage.thumb : this.currentImage.src ) + "' alt='" + this.currentImage.title + "' width='" + this.currentImage.width + "' height='" + this.currentImage.height + "' />";
111                                 h += "</a>";
112                         } else
113                                 h += '&nbsp;';
114                         h += "</div>";
115
116                         h += "<form name='uploadoptions' id='uploadoptions' class='alignleft'>";
117                         h += "<table>";
118                         var display = [];
119                         var checked = 'display-title';
120                         if ( 1 == this.currentImage.isImage ) {
121                                 checked = 'display-full';
122                                 if ( this.currentImage.thumb ) {
123                                         display.push("<label for='display-thumb'><input type='radio' name='display' id='display-thumb' value='thumb' /> " + this.thumb + "</label><br />");
124                                         checked = 'display-thumb';
125                                 }
126                                 display.push("<label for='display-full'><input type='radio' name='display' id='display-full' value='full' /> " + this.full + "</label>");
127                         } else if ( this.currentImage.thumb ) {
128                                 display.push("<label for='display-thumb'><input type='radio' name='display' id='display-thumb' value='thumb' /> " + this.icon + "</label>");
129                         }
130                         if ( display.length ) {
131                                 display.push("<br /><label for='display-title'><input type='radio' name='display' id='display-title' value='title' /> " + this.title + "</label>");
132                                 h += "<tr><th style='padding-bottom:.5em'>" + this.show + "</th><td style='padding-bottom:.5em'>";
133                                 jQuery(display).each( function() { h += this; } );
134                                 h += "</td></tr>";
135                         }
136
137                         h += "<tr><th>" + this.link + "</th><td>";
138                         h += "<label for='link-file'><input type='radio' name='link' id='link-file' value='file' checked='checked'/> " + this.file + "</label><br />";
139                         h += "<label for='link-page'><input type='radio' name='link' id='link-page' value='page' /> " + this.page + "</label><br />";
140                         h += "<label for='link-none'><input type='radio' name='link' id='link-none' value='none' /> " + this.none + "</label>";
141                         h += "</td></tr>";
142
143                         h += "<tr><td colspan='2'><p class='submit'>";
144                         h += "<input type='button' class='button' name='send' onclick='theFileList.sendToEditor(" + id + ")' value='" + this.editorText + "' />";
145                         h += "</p></td></tr></table>";
146                         h += "</form>";
147
148                         h += "</div>";
149
150                         jQuery(h).prependTo('#upload-content');
151                         jQuery('#' + checked).attr('checked','checked');
152                         if (e) return e.stopPropagation();
153                         return false;
154                 },
155
156                 editView: function(id, e) {
157                         this.prepView(id);
158                         var h = '';
159
160                         var action = 'upload.php?style=' + this.style + '&amp;tab=upload';
161                         if ( this.postID )
162                                 action += '&amp;post_id=' + this.postID;
163
164                         h += "<form id='upload-file' method='post' action='" + action + "'>";
165                         if ( this.ID ) {
166                                 var params = this.params;
167                                 params.ID = '';
168                                 params.action = '';
169                                 h += "<a href='" + this.urlData[0] + '?' + this.toQueryString(params) + "'";
170                         } else {
171                                 h += "<a href='#' onclick='return theFileList.cancelView();'";
172                         }
173                         h += " title='" + this.browseTitle + "' class='back'>" + this.back + "</a>";
174                         h += "<div id='file-title'>"
175                         if ( 0 == this.currentImage.isImage )
176                                 h += "<h2><a href='" + this.currentImage.srcBase + this.currentImage.src + "' onclick='return false;' title='" + this.directTitle + "'>" + this.currentImage.title + "</a></h2>";
177                         else
178                                 h += "<h2>" + this.currentImage.title + "</h2>";
179                         h += " &#8212; <span>";
180                         h += "<a href='#' onclick='return theFileList.imageView(" + id + ");'>" + this.insert + "</a>";
181                         h += "</span>";
182                         h += '</div>'
183                         h += "<div id='upload-file-view' class='alignleft'>";
184                         if ( 1 == this.currentImage.isImage ) {
185                                 h += "<a href='" + this.currentImage.srcBase + this.currentImage.src + "' onclick='return false;' title='" + this.directTitle + "'>";
186                                 h += "<img src='" + ( this.currentImage.thumb ? this.currentImage.thumb : this.currentImage.src ) + "' alt='" + this.currentImage.title + "' width='" + this.currentImage.width + "' height='" + this.currentImage.height + "' />";
187                                 h += "</a>";
188                         } else
189                                 h += '&nbsp;';
190                         h += "</div>";
191
192
193                         h += "<table><col /><col class='widefat' /><tr>";
194                         h += "<th scope='row'><label for='url'>" + this.urlText + "</label></th>";
195                         h += "<td><input type='text' id='url' class='readonly' value='" + this.currentImage.srcBase + this.currentImage.src + "' readonly='readonly' /></td>";
196                         h += "</tr><tr>";
197                         h += "<th scope='row'><label for='post_title'>" + this.title + "</label></th>";
198                         h += "<td><input type='text' id='post_title' name='post_title' value='" + this.currentImage.title + "' /></td>";
199                         h += "</tr><tr>";
200                         h += "<th scope='row'><label for='post_content'>" + this.desc + "</label></th>";
201                         h += "<td><textarea name='post_content' id='post_content'>" + this.currentImage.description + "</textarea></td>";
202                         h += "</tr><tr id='buttons' class='submit'><td colspan='2'><input type='button' id='delete' name='delete' class='delete alignleft' value='" + this.deleteText + "' onclick='theFileList.deleteFile(" + id + ");' />";
203                         h += "<input type='hidden' name='from_tab' value='" + this.tab + "' />";
204                         h += "<input type='hidden' name='post_parent' value='" + parseInt(this.postID,10) + "' />";
205                         h += "<input type='hidden' name='action' id='action-value' value='save' />";
206                         h += "<input type='hidden' name='ID' value='" + id + "' />";
207                         h += "<input type='hidden' name='_wpnonce' value='" + this.nonce + "' />";
208                         h += "<div class='submit'><input type='submit' value='" + this.saveText + "' /></div>";
209                         h += "</td></tr></table></form>";
210
211                         jQuery(h).prependTo('#upload-content');
212                         if (e) e.stopPropagation();
213                         return false;
214                 },
215
216                 prepView: function(id) {
217                         this.cancelView( true );
218                         var filesEl = jQuery('#upload-files');
219                         if ( filesEl )
220                                 filesEl.hide();
221                         var navEl = jQuery('#current-tab-nav');
222                         if ( navEl )
223                                 navEl.hide();
224                         this.grabImageData(id);
225                 },
226
227                 cancelView: function( prep ) {
228                         if ( !prep ) {
229                                 var filesEl = jQuery('#upload-files');
230                                 if ( filesEl )
231                                         jQuery(filesEl).show();
232                                 var navEl = jQuery('#current-tab-nav');
233                                 if ( navEl )
234                                         jQuery(navEl).show();
235                         }
236                         if ( !this.ID )
237                                 this.grabImageData(0);
238                         var div = jQuery('#upload-file');
239                         if ( div )
240                                 jQuery(div).remove();
241                         return false;
242                 },
243
244                 sendToEditor: function(id) {
245                         this.grabImageData(id);
246                         var link = '';
247                         var display = '';
248                         var h = '';
249
250                         link = jQuery('input[@type=radio][@name="link"][@checked]','#uploadoptions').val();
251                         displayEl = jQuery('input[@type=radio][@name="display"][@checked]','#uploadoptions');
252                         if ( displayEl )
253                                 display = jQuery(displayEl).val();
254                         else if ( 1 == this.currentImage.isImage )
255                                 display = 'full';
256
257                         if ( 'none' != link )
258                                 h += "<a href='" + ( 'file' == link ? ( this.currentImage.srcBase + this.currentImage.src ) : ( this.currentImage.page + "' rel='attachment wp-att-" + this.currentImage.ID ) ) + "' title='" + this.currentImage.title + "'>";
259                         if ( display && 'title' != display )
260                                 h += "<img src='" + ( 'thumb' == display ? ( this.currentImage.thumbBase + this.currentImage.thumb ) : ( this.currentImage.srcBase + this.currentImage.src ) ) + "' alt='" + this.currentImage.title + "' />";
261                         else
262                                 h += this.currentImage.title;
263                         if ( 'none' != link )
264                                 h += "</a>";
265
266                         var win = window.opener ? window.opener : window.dialogArguments;
267                         if ( !win )
268                                 win = top;
269                         tinyMCE = win.tinyMCE;
270                         if ( typeof tinyMCE != 'undefined' && tinyMCE.getInstanceById('content') ) {
271                                 tinyMCE.selectedInstance.getWin().focus();
272                                 tinyMCE.execCommand('mceInsertContent', false, h);
273                         } else
274                                 win.edInsertContent(win.edCanvas, h);
275                         if ( !this.ID )
276                                 this.cancelView();
277                         return false;
278                 },
279
280                 deleteFile: function(id) {
281                         if ( confirm( this.confirmText.replace(/%title%/g, this.currentImage.title) ) ) {
282                                 jQuery('#action-value').attr('value','delete');
283                                 jQuery('#upload-file').submit();
284                                 return true;
285                         }
286                         return false;
287                 }
288
289         };
290
291         for ( var property in uploadL10n ) 
292                 theFileList[property] = uploadL10n[property];
293         theFileList.initializeVars();
294         theFileList.initializeLinks();
295 } );