]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/js/tinymce/plugins/image/plugin.js
WordPress 4.0
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / image / plugin.js
index c7f6f5acda595fa90e0afc5d32855c533fa30ed2..9d105eff303477d0a71d53a654ffb9a3e410bdc1 100644 (file)
@@ -40,14 +40,27 @@ tinymce.PluginManager.add('image', function(editor) {
                img.src = url;
        }
 
                img.src = url;
        }
 
-       function applyPreview(items) {
-               tinymce.each(items, function(item) {
-                       item.textStyle = function() {
-                               return editor.formatter.getCssText({inline: 'img', classes: [item.value]});
-                       };
-               });
+       function buildListItems(inputList, itemCallback, startItems) {
+               function appendItems(values, output) {
+                       output = output || [];
+
+                       tinymce.each(values, function(item) {
+                               var menuItem = {text: item.text || item.title};
+
+                               if (item.menu) {
+                                       menuItem.menu = appendItems(item.menu);
+                               } else {
+                                       menuItem.value = item.value;
+                                       itemCallback(menuItem);
+                               }
+
+                               output.push(menuItem);
+                       });
+
+                       return output;
+               }
 
 
-               return items;
+               return appendItems(inputList, startItems || []);
        }
 
        function createImageList(callback) {
        }
 
        function createImageList(callback) {
@@ -61,6 +74,8 @@ tinymce.PluginManager.add('image', function(editor) {
                                                callback(tinymce.util.JSON.parse(text));
                                        }
                                });
                                                callback(tinymce.util.JSON.parse(text));
                                        }
                                });
+                       } else if (typeof(imageList) == "function") {
+                               imageList(callback);
                        } else {
                                callback(imageList);
                        }
                        } else {
                                callback(imageList);
                        }
@@ -69,45 +84,7 @@ tinymce.PluginManager.add('image', function(editor) {
 
        function showDialog(imageList) {
                var win, data = {}, dom = editor.dom, imgElm = editor.selection.getNode();
 
        function showDialog(imageList) {
                var win, data = {}, dom = editor.dom, imgElm = editor.selection.getNode();
-               var width, height, imageListCtrl, classListCtrl;
-
-               function buildValues(listSettingName, dataItemName, defaultItems) {
-                       var selectedItem, items = [];
-
-                       tinymce.each(editor.settings[listSettingName] || defaultItems, function(target) {
-                               var item = {
-                                       text: target.text || target.title,
-                                       value: target.value
-                               };
-
-                               items.push(item);
-
-                               if (data[dataItemName] === target.value || (!selectedItem && target.selected)) {
-                                       selectedItem = item;
-                               }
-                       });
-
-                       if (selectedItem && !data[dataItemName]) {
-                               data[dataItemName] = selectedItem.value;
-                               selectedItem.selected = true;
-                       }
-
-                       return items;
-               }
-
-               function buildImageList() {
-                       var imageListItems = [{text: 'None', value: ''}];
-
-                       tinymce.each(imageList, function(image) {
-                               imageListItems.push({
-                                       text: image.text || image.title,
-                                       value: editor.convertURL(image.value || image.url, 'src'),
-                                       menu: image.menu
-                               });
-                       });
-
-                       return imageListItems;
-               }
+               var width, height, imageListCtrl, classListCtrl, imageDimensions = editor.settings.image_dimensions !== false;
 
                function recalcSize() {
                        var widthCtrl, heightCtrl, newWidth, newHeight;
 
                function recalcSize() {
                        var widthCtrl, heightCtrl, newWidth, newHeight;
@@ -115,6 +92,10 @@ tinymce.PluginManager.add('image', function(editor) {
                        widthCtrl = win.find('#width')[0];
                        heightCtrl = win.find('#height')[0];
 
                        widthCtrl = win.find('#width')[0];
                        heightCtrl = win.find('#height')[0];
 
+                       if (!widthCtrl || !heightCtrl) {
+                               return;
+                       }
+
                        newWidth = widthCtrl.value();
                        newHeight = heightCtrl.value();
 
                        newWidth = widthCtrl.value();
                        newHeight = heightCtrl.value();
 
@@ -136,12 +117,15 @@ tinymce.PluginManager.add('image', function(editor) {
                        function waitLoad(imgElm) {
                                function selectImage() {
                                        imgElm.onload = imgElm.onerror = null;
                        function waitLoad(imgElm) {
                                function selectImage() {
                                        imgElm.onload = imgElm.onerror = null;
-                                       editor.selection.select(imgElm);
-                                       editor.nodeChanged();
+
+                                       if (editor.selection) {
+                                               editor.selection.select(imgElm);
+                                               editor.nodeChanged();
+                                       }
                                }
 
                                imgElm.onload = function() {
                                }
 
                                imgElm.onload = function() {
-                                       if (!data.width && !data.height) {
+                                       if (!data.width && !data.height && imageDimensions) {
                                                dom.setAttribs(imgElm, {
                                                        width: imgElm.clientWidth,
                                                        height: imgElm.clientHeight
                                                dom.setAttribs(imgElm, {
                                                        width: imgElm.clientWidth,
                                                        height: imgElm.clientHeight
@@ -174,10 +158,11 @@ tinymce.PluginManager.add('image', function(editor) {
                                data.height = null;
                        }
 
                                data.height = null;
                        }
 
-                       if (data.style === '') {
+                       if (!data.style) {
                                data.style = null;
                        }
 
                                data.style = null;
                        }
 
+                       // Setup new data excluding style properties
                        data = {
                                src: data.src,
                                alt: data.alt,
                        data = {
                                src: data.src,
                                alt: data.alt,
@@ -187,10 +172,6 @@ tinymce.PluginManager.add('image', function(editor) {
                                "class": data["class"]
                        };
 
                                "class": data["class"]
                        };
 
-                       if (!data["class"]) {
-                               delete data["class"];
-                       }
-
                        editor.undoManager.transact(function() {
                                // WP
                                var eventData = { node: imgElm, data: data, caption: caption };
                        editor.undoManager.transact(function() {
                                // WP
                                var eventData = { node: imgElm, data: data, caption: caption };
@@ -235,20 +216,28 @@ tinymce.PluginManager.add('image', function(editor) {
                        return value;
                }
 
                        return value;
                }
 
-               function srcChange() {
+               function srcChange(e) {
+                       var meta = e.meta || {};
+
                        if (imageListCtrl) {
                                imageListCtrl.value(editor.convertURL(this.value(), 'src'));
                        }
 
                        if (imageListCtrl) {
                                imageListCtrl.value(editor.convertURL(this.value(), 'src'));
                        }
 
-                       getImageSize(this.value(), function(data) {
-                               if (data.width && data.height) {
-                                       width = data.width;
-                                       height = data.height;
-
-                                       win.find('#width').value(width);
-                                       win.find('#height').value(height);
-                               }
+                       tinymce.each(meta, function(value, key) {
+                               win.find('#' + key).value(value);
                        });
                        });
+
+                       if (!meta.width && !meta.height) {
+                               getImageSize(this.value(), function(data) {
+                                       if (data.width && data.height && imageDimensions) {
+                                               width = data.width;
+                                               height = data.height;
+
+                                               win.find('#width').value(width);
+                                               win.find('#height').value(height);
+                                       }
+                               });
+                       }
                }
 
                width = dom.getAttrib(imgElm, 'width');
                }
 
                width = dom.getAttrib(imgElm, 'width');
@@ -273,7 +262,13 @@ tinymce.PluginManager.add('image', function(editor) {
                        imageListCtrl = {
                                type: 'listbox',
                                label: 'Image list',
                        imageListCtrl = {
                                type: 'listbox',
                                label: 'Image list',
-                               values: buildImageList(),
+                               values: buildListItems(
+                                       imageList,
+                                       function(item) {
+                                               item.value = editor.convertURL(item.value || item.url, 'src');
+                                       },
+                                       [{text: 'None', value: ''}]
+                               ),
                                value: data.src && editor.convertURL(data.src, 'src'),
                                onselect: function(e) {
                                        var altCtrl = win.find('#alt');
                                value: data.src && editor.convertURL(data.src, 'src'),
                                onselect: function(e) {
                                        var altCtrl = win.find('#alt');
@@ -282,7 +277,7 @@ tinymce.PluginManager.add('image', function(editor) {
                                                altCtrl.value(e.control.text());
                                        }
 
                                                altCtrl.value(e.control.text());
                                        }
 
-                                       win.find('#src').value(e.control.value());
+                                       win.find('#src').value(e.control.value()).fire('change');
                                },
                                onPostRender: function() {
                                        imageListCtrl = this;
                                },
                                onPostRender: function() {
                                        imageListCtrl = this;
@@ -295,13 +290,29 @@ tinymce.PluginManager.add('image', function(editor) {
                                name: 'class',
                                type: 'listbox',
                                label: 'Class',
                                name: 'class',
                                type: 'listbox',
                                label: 'Class',
-                               values: applyPreview(buildValues('image_class_list', 'class'))
+                               values: buildListItems(
+                                       editor.settings.image_class_list,
+                                       function(item) {
+                                               if (item.value) {
+                                                       item.textStyle = function() {
+                                                               return editor.formatter.getCssText({inline: 'img', classes: [item.value]});
+                                                       };
+                                               }
+                                       }
+                               )
                        };
                }
 
                // General settings shared between simple and advanced dialogs
                var generalFormItems = [
                        };
                }
 
                // General settings shared between simple and advanced dialogs
                var generalFormItems = [
-                       {name: 'src', type: 'filepicker', filetype: 'image', label: 'Source', autofocus: true, onchange: srcChange},
+                       {
+                               name: 'src',
+                               type: 'filepicker',
+                               filetype: 'image',
+                               label: 'Source',
+                               autofocus: true,
+                               onchange: srcChange
+                       },
                        imageListCtrl
                ];
 
                        imageListCtrl
                ];
 
@@ -309,7 +320,7 @@ tinymce.PluginManager.add('image', function(editor) {
                        generalFormItems.push({name: 'alt', type: 'textbox', label: 'Image description'});
                }
 
                        generalFormItems.push({name: 'alt', type: 'textbox', label: 'Image description'});
                }
 
-               if (editor.settings.image_dimensions !== false) {
+               if (imageDimensions) {
                        generalFormItems.push({
                                type: 'container',
                                label: 'Dimensions',
                        generalFormItems.push({
                                type: 'container',
                                label: 'Dimensions',
@@ -420,11 +431,6 @@ tinymce.PluginManager.add('image', function(editor) {
                }
        }
 
                }
        }
 
-       // WP
-       editor.addCommand( 'mceImage', function() {
-               createImageList( showDialog )();
-       });
-
        editor.addButton('image', {
                icon: 'image',
                tooltip: 'Insert/edit image',
        editor.addButton('image', {
                icon: 'image',
                tooltip: 'Insert/edit image',
@@ -439,4 +445,6 @@ tinymce.PluginManager.add('image', function(editor) {
                context: 'insert',
                prependToContext: true
        });
                context: 'insert',
                prependToContext: true
        });
+
+       editor.addCommand('mceImage', createImageList(showDialog));
 });
 });