bugfix, cosmétique.
[ckeditor.git] / skins / ckeditor / plugins / plinn_image / plugin.js
index 7181a9e..c7ae5ec 100644 (file)
-/* © Benoît Pin, MINES ParisTech */
+/* © 2011 Benoît Pin, MINES ParisTech */
 
 
 (function(){
 
 
 
 (function(){
 
+var reImage = /^image\//;
+
+var PlinnCKDDUploader = function(editor) {
+       this.editor = editor;
+       this.uploadUrl = editor.config.baseHref + 'attachments/put_upload';
+       this.uploadQueue = [];
+       this._uploadQueueRunning = false;
+       var self = this;
+       editor.document.on('dragenter', function(e) {self.dragenter(e);});
+       editor.document.on('dragover',  function(e) {self.dragover(e);});
+       editor.document.on('drop',              function(e) {self.drop(e);});
+};
+
+// Drag and drop
+PlinnCKDDUploader.prototype.dragenter = function(e) {
+       var evt = e.data.$;
+       disableDefault(evt);
+       disablePropagation(evt);
+};
+
+PlinnCKDDUploader.prototype.dragover = function(e) {
+       var evt = e.data.$;
+       disableDefault(evt);
+       disablePropagation(evt);
+       evt = getEventObject(evt);
+       var dt = evt.dataTransfer;
+       dt.dropEffect = 'copy';
+};
+
+PlinnCKDDUploader.prototype.drop = function(e) {
+       var evt = e.data.$;
+       disableDefault(evt);
+       disablePropagation(evt);
+       getEventObject(evt);
+       var dt = evt.dataTransfer;
+       dt.dropEffect = 'copy';
+       this.handleFiles(dt.files);
+};
+
+PlinnCKDDUploader.prototype.createFileProxy = function(file) {
+       var container = new CKEDITOR.dom.element('span');
+       var rel = CKEDITOR.dom.element.createFromHtml('<span style="position:relative"/>');
+       container.append(rel);
+       var progressBar = CKEDITOR.dom.element.createFromHtml(
+               '<span style="display:block; position:absolute; background:#ef8e32; height:4px; border-radius:2px; width:0; left:0; top:1em"/>');
+       rel.append(progressBar);
+       var link = new CKEDITOR.dom.element('a');
+       link.setAttribute('href', '#');
+       link.appendText(file.name);
+       container.append(link);
+
+       var proxy = {};
+       proxy.file = file;
+       proxy.container = container;
+       proxy.progressBar = progressBar;
+       proxy.link = link;
+       return proxy;
+};
+
+// Methods about upload
+PlinnCKDDUploader.prototype.handleFiles = function(files) {
+       var file, i, proxy;
+       for (i=0 ; i<files.length ; i++) {
+               file = files[i];
+               if (reImage.test(file.type)) {
+                       // TODO
+               }
+               else {
+                       proxy = this.createFileProxy(file);
+                       this.editor.insertElement(proxy.container);
+                       this.editor.insertText(' ');
+                       this.uploadQueuePush(proxy);
+               }
+       }
+};
+
+
+
+PlinnCKDDUploader.prototype.beforeUpload = function(item) {
+       this.uploadedItem = item;
+       this.progressBar = item.progressBar;
+       this.progressBarMaxSize = item.container.getSize('width');
+};
+
+
+PlinnCKDDUploader.prototype.upload = function(item) {
+       // item.file must be the file to be uploaded
+       this.beforeUpload(item);
+       var reader = new FileReader();
+       var req = new XMLHttpRequest();
+       var file = item.file;
+       
+       var self = this;
+       
+       addListener(req.upload, 'progress', function(evt){self.progressHandler(evt);});
+       addListener(req, 'readystatechange',
+               function(evt) {
+                       if (req.readyState === 4) {
+                               self.uploadCompleteHandler(req);
+                       }
+               });
+
+       req.open("PUT", this.uploadUrl);
+       req.setRequestHeader("Content-Type", file.type);
+       req.setRequestHeader("X-File-Name", encodeURI(file.name));
+       addListener(reader, 'load',
+               function(evt){
+                       try {
+                               req.sendAsBinary(evt.target.result);
+                       }
+                       catch(e){}
+               });
+       reader.readAsBinaryString(file);
+};
+
+
+PlinnCKDDUploader.prototype.uploadCompleteHandlerCB = function(req) {
+       var item = this.uploadedItem;
+       var data = req.responseXML.documentElement;
+       var link = new CKEDITOR.dom.element('a');
+       link.setAttribute('href', 'attachments/' + data.getAttribute('id'));
+       link.appendText(data.getAttribute('title'));
+       link.replace(item.container);
+};
+
+PlinnCKDDUploader.prototype.uploadCompleteHandler = function(req) {
+       this.uploadCompleteHandlerCB(req);
+       this.uploadQueueLoadNext();
+};
+
+PlinnCKDDUploader.prototype.progressHandlerCB = function(progress) {
+       // 0 <= progress <= 1
+       var size = this.progressBarMaxSize * progress;
+       size = Math.round(size);
+       this.progressBar.setStyle('width', String(size) + 'px');
+};
+
+PlinnCKDDUploader.prototype.progressHandler = function(evt) {
+       if (evt.lengthComputable) {
+               var progress = evt.loaded / evt.total;
+               this.progressHandlerCB(progress);
+       }
+};
+
+// Methods about queue
+PlinnCKDDUploader.prototype.uploadQueuePush = function(item) {
+       this.uploadQueue.push(item);
+       if (!this._uploadQueueRunning) {
+               this.startUploadQueue();
+       }
+};
+
+PlinnCKDDUploader.prototype.startUploadQueue = function() {
+       this._uploadQueueRunning = true;
+       this.uploadQueueLoadNext();
+};
+
+PlinnCKDDUploader.prototype.uploadQueueLoadNext = function() {
+       var item = this.uploadQueue.shift();
+       if (item) {
+               this.upload(item);
+       }
+       else {
+               this._uploadQueueRunning = false;
+       }
+};
+
 var reSize = /getResizedImage\?size=(\d+)_(\d+)$/;
 
 function updateImageSizeUrlParameters(img) {
        if (reSize.test(img.src)){
                var matches = reSize.exec(img.src);
 var reSize = /getResizedImage\?size=(\d+)_(\d+)$/;
 
 function updateImageSizeUrlParameters(img) {
        if (reSize.test(img.src)){
                var matches = reSize.exec(img.src);
-               var srcWidth = parseInt(matches[1]);
-               var srcHeight = parseInt(matches[2]);
+               var srcWidth = parseInt(matches[1], 10);
+               var srcHeight = parseInt(matches[2], 10);
        
        
-               var imgWidth = parseInt((img.style.width) ? img.style.width : img.width);
-               var imgHeight = parseInt((img.style.height) ? img.style.height : img.height);
+               var imgWidth = parseInt((img.style.width) ? img.style.width : img.width, 10);
+               var imgHeight = parseInt((img.style.height) ? img.style.height : img.height, 10);
        
        
-               if ((imgWidth && imgHeight) && srcWidth != imgWidth && srcHeight != imgHeight) {
+               if ((imgWidth && imgHeight) && srcWidth !== imgWidth && srcHeight !== imgHeight) {
                        var newUrl = img.getAttribute('src', 2).replace(reSize, 'getResizedImage?size=' + imgWidth + '_' + imgHeight);
                        img.width = imgWidth;
                        img.height = imgHeight;
                        var newUrl = img.getAttribute('src', 2).replace(reSize, 'getResizedImage?size=' + imgWidth + '_' + imgHeight);
                        img.width = imgWidth;
                        img.height = imgHeight;
@@ -23,23 +190,65 @@ function updateImageSizeUrlParameters(img) {
        }
 }
 
        }
 }
 
+function openPlinnImageDialog(path, editor) {
+       var winOptions  = "location=no,menubar=no,toolbar=no,dependent=yes,dialog=yes,minimizable=no,modal=yes,alwaysRaised=yes" +
+               ",resizable=yes"  +
+               ",width=801"  +
+               ",height=600";
+               //",top="  + iTop +
+               //",left=" + iLeft ;
+       
+       var win = open(path + 'dialog/plinn_image.html', 'PlinnImageDialog', winOptions);
+       win.dialogArguments = {};
+       win.dialogArguments.editor = editor;
+       win.dialogArguments.pluginPath = path;
+       win.dialogArguments.CKEDITOR = CKEDITOR;
+}
+
 
 CKEDITOR.plugins.add( 'plinn_image',
 {
        init : function( editor )
        {
 
 CKEDITOR.plugins.add( 'plinn_image',
 {
        init : function( editor )
        {
+               /* Add listener on getData event to compute images
+                  src attributes before saving data.
+               */
                editor.on('instanceReady', function(){
                        editor.on('getData',
                                function(evt) {
                editor.on('instanceReady', function(){
                        editor.on('getData',
                                function(evt) {
-                                       var body = evt.editor.document.$.body;
-                                       var images = body.getElementsByTagName('IMG');
-                                       for (var i = 0 ; i < images.length ; i++)
-                                               updateImageSizeUrlParameters(images[i]);
-                                       evt.data.dataValue = evt.editor.document.$.body.innerHTML;
+                                       var tmpDiv = document.createElement('div');
+                                       tmpDiv.innerHTML = evt.data.dataValue;
+                                       var images = tmpDiv.getElementsByTagName('IMG');
+                                       var i;
+                                       for (i = 0 ; i < images.length ; i++) {
+                                               updateImageSizeUrlParameters(images[i]);}
+                                       evt.data.dataValue = tmpDiv.innerHTML;
                                }
                        );
                                }
                        );
+                       // drag & drop upload initialisation
+                       var dd = new PlinnCKDDUploader(editor);
                });
                });
+               
+                               
+               var pluginPath = this.path;
+               var allowed = 'img[alt,!src]{border-style,border-width,float,height,margin,margin-bottom,margin-left,margin-right,margin-top,width}';
+               var required = 'img[alt,src]';
+               var command = editor.addCommand('plinn_image',
+                       {
+                               exec : function(editor){openPlinnImageDialog(pluginPath, editor);},
+                               allowedContent: allowed,
+                               requiredContent: required
+                       }
+               );
+               
+               editor.ui.addButton('PlinnImage',
+                       {
+                               label : editor.lang.common.image,
+                               icon : pluginPath + 'dialog/plinn_image.png',
+                               command : 'plinn_image'
+                       });
        }
        }
+       
 });
 
 });
 
-})();
+}());