2007-09-16 04:36:42 +02:00
|
|
|
/**
|
2007-10-03 01:08:38 +02:00
|
|
|
* This class is used for upload in TinyMCE editor.
|
2007-09-16 04:36:42 +02:00
|
|
|
* If one of methods is not commented look for comment in Upload.js.
|
|
|
|
*/
|
2007-09-16 04:21:47 +02:00
|
|
|
TinyMCEImageEnhancement = Class.create();
|
|
|
|
TinyMCEImageEnhancement.prototype = {
|
|
|
|
initialize: function() {
|
2007-09-16 04:34:08 +02:00
|
|
|
this.filesUploaed = 0;
|
|
|
|
this.processInProgress = false;
|
2007-09-16 04:21:47 +02:00
|
|
|
Event.observe(window,'load',this.onWindowLoad.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
buildUI: function() {
|
|
|
|
$('Form_EditorToolbarImageForm_FolderID').value = "";
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
divAddFolder = this.addElement('div','group',$('FolderID'),{'id': 'AddFolderGroup'});
|
|
|
|
addFolder = this.addElement('a','link',divAddFolder,{'id': 'AddFolder','href' : '#','innerHTML': 'add folder'});
|
|
|
|
newFolderName = this.addElement('input', 'addFolder',divAddFolder,{'id': 'NewFolderName','type' : 'text'});
|
2007-09-16 04:21:47 +02:00
|
|
|
Element.hide(newFolderName);
|
2007-09-16 04:34:08 +02:00
|
|
|
var folderOk = this.addElement('a','link',divAddFolder,{'id': 'FolderOk','href' : '#','innerHTML': 'ok'});
|
2007-09-16 04:21:47 +02:00
|
|
|
Element.hide(folderOk);
|
2007-09-16 04:34:08 +02:00
|
|
|
Element.addClassName(folderOk,'addFolder');
|
|
|
|
var folderCancel = this.addElement('a','link',divAddFolder,{'id': 'FolderCancel','href' : '#','innerHTML': 'cancel'});
|
2007-09-16 04:21:47 +02:00
|
|
|
Element.hide(folderCancel);
|
2007-09-16 04:34:08 +02:00
|
|
|
Element.addClassName(folderCancel,'addFolder');
|
2007-09-16 04:21:47 +02:00
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
var divUpload = this.addElement('div','group',$('FolderID'),{'id': 'UploadrGroup'});
|
|
|
|
var pipeSeparator = this.addElement('div','',divUpload,{'id' : 'PipeSeparator','innerHTML' : ' | '});
|
2007-09-16 04:21:47 +02:00
|
|
|
pipeSeparator.style.display = "inline";
|
2007-09-16 04:34:08 +02:00
|
|
|
var uploadFiles = this.addElement('a','link',divUpload,{'id' : 'UploadFiles','href' : '#','innerHTML' : 'upload'});
|
2007-09-16 04:21:47 +02:00
|
|
|
|
|
|
|
Event.observe(addFolder,'click',this.onAddFolder.bind(this));
|
|
|
|
Event.observe(folderOk,'click',this.onFolderOk.bind(this));
|
|
|
|
Event.observe(folderCancel,'click',this.onFolderCancel.bind(this));
|
2007-09-16 04:34:08 +02:00
|
|
|
Event.observe($('UploadFiles'),'click',this.onUpload.bind(this));
|
2007-09-16 04:21:47 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Method creates HTML element, only reason for this method is DRY.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
addElement: function(tag, className, parent, properties) {
|
|
|
|
var e = document.createElement(tag);
|
|
|
|
Element.addClassName(e,className);
|
|
|
|
parent.appendChild(e);
|
|
|
|
Object.extend(e,properties);
|
|
|
|
return e;
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:21:47 +02:00
|
|
|
onUpload: function(event) {
|
|
|
|
Event.stop(event);
|
2007-09-16 04:34:08 +02:00
|
|
|
if(!this.processInProgress) {
|
|
|
|
if(this.getParentID() != 'root') {
|
|
|
|
this.upload.browse();
|
2007-09-16 04:21:47 +02:00
|
|
|
} else {
|
|
|
|
statusMessage("Please choose folder","bad");
|
|
|
|
}
|
2007-09-16 04:34:08 +02:00
|
|
|
}
|
2007-09-16 04:21:47 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Called when user clicks "add folder" anchor.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:21:47 +02:00
|
|
|
onAddFolder: function(event) {
|
|
|
|
Event.stop(event);
|
|
|
|
Element.hide('AddFolder');
|
|
|
|
Element.show('NewFolderName','FolderOk','FolderCancel');
|
|
|
|
this.applyIE6Hack();
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Called when user clicks "ok" anchor/ adds folder with given name.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:21:47 +02:00
|
|
|
onFolderOk: function(event) {
|
|
|
|
Event.stop(event);
|
|
|
|
var folderName = $('NewFolderName').value;
|
|
|
|
var options = {
|
|
|
|
method: 'post',
|
2007-09-16 04:34:08 +02:00
|
|
|
postBody: 'ParentID=' + this.getParentID() + '&ajax=1' ,
|
2007-09-16 04:21:47 +02:00
|
|
|
onSuccess: this.onFolderGetSuccess.bind(this),
|
|
|
|
onFailure: function(transport) {
|
|
|
|
errorMessage('Error: Folder not added', transport);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
new Ajax.Request('admin/assets/addfolder', options);
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Method retrieves folder id and sends request which changes folder name.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:21:47 +02:00
|
|
|
onFolderGetSuccess: function(transport) {
|
|
|
|
var t1 = transport.responseText.indexOf('TreeNode(');
|
|
|
|
var t2 = transport.responseText.indexOf(',');
|
|
|
|
var folderID = transport.responseText.substring(t1 + 9,t2);
|
|
|
|
var date = new Date();
|
|
|
|
var year = date.getFullYear();
|
|
|
|
var month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth();
|
|
|
|
var day = date.getDay() < 10 ? '0' + date.getDay() : date.getDay();
|
|
|
|
var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
|
|
|
|
var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
|
|
|
|
var seconds = date.getSeconds() < 10 == 1 ? '0' + date.getSeconds() : date.getSeconds();
|
|
|
|
var currentDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
|
|
|
|
var folderName = $('NewFolderName').value;
|
|
|
|
var options = {
|
|
|
|
method: 'post',
|
|
|
|
postBody: 'Created=' + currentDate + '&Name=' + folderName + '&ClassName=Folder&ID=' + folderID + '&ajax=1&action_save=1',
|
|
|
|
onSuccess: this.onFolderAddSuccess.bind(this),
|
|
|
|
onFailure: function(transport) {
|
|
|
|
errorMessage('Error: Folder not added', transport);
|
|
|
|
}
|
|
|
|
};
|
2007-09-16 04:34:08 +02:00
|
|
|
new Ajax.Request('admin/assets/index/' + this.getParentID() + '?executeForm=EditForm', options);
|
2007-09-16 04:21:47 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* When folder name has been changed we need to refresh folder list and return to initial state of UI.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:21:47 +02:00
|
|
|
onFolderAddSuccess: function(transport) {
|
|
|
|
statusMessage('Creating new folder');
|
|
|
|
document.getElementsBySelector("div.TreeDropdownField.single")[2].itemTree = null;
|
|
|
|
$('NewFolderName').value = '';
|
|
|
|
Element.show('AddFolder');
|
|
|
|
Element.hide('NewFolderName','FolderOk','FolderCancel');
|
|
|
|
this.removeIE6Hack();
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* If user doesn't want to add folder return to default UI.
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:21:47 +02:00
|
|
|
onFolderCancel: function(event) {
|
|
|
|
Event.stop(event);
|
|
|
|
$('NewFolderName').value = '';
|
|
|
|
Element.show('AddFolder');
|
|
|
|
Element.hide('NewFolderName','FolderOk','FolderCancel');
|
|
|
|
this.removeIE6Hack();
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Called on window.onload
|
|
|
|
*/
|
2007-09-16 04:21:47 +02:00
|
|
|
|
|
|
|
onWindowLoad: function() {
|
|
|
|
if($('FolderID') != null) {
|
2007-09-16 04:34:08 +02:00
|
|
|
this.upload = new Upload(
|
|
|
|
{
|
2007-09-16 04:35:30 +02:00
|
|
|
fileTypes : '*.jpeg;*.jpg;*.jpe;*.png;*.gif;',
|
|
|
|
fileTypesDescription : 'Image files',
|
|
|
|
fileUploadLimit : '100',
|
|
|
|
beginUploadOnQueue : true,
|
|
|
|
buildUI : this.buildUI.bind(this),
|
|
|
|
fileQueued : this.uploadFileQueuedCallback.bind(this),
|
|
|
|
fileComplete : this.uploadFileCompleteCallback.bind(this),
|
|
|
|
queueComplete : this.uploadQueueCompleteCallback.bind(this)
|
2007-09-16 04:34:08 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2007-09-16 04:21:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
uploadFileQueuedCallback: function(file,queueLength) {
|
2007-09-16 04:34:08 +02:00
|
|
|
this.processInProgress = true;
|
|
|
|
this.upload.setFolderID(this.getParentID());
|
|
|
|
$('UploadFiles').innerHTML = "Uploading ... 1/" + this.upload.getFilesToUpload();
|
2007-09-16 04:21:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
uploadFileCompleteCallback: function(file,serverData) {
|
2007-09-16 04:34:08 +02:00
|
|
|
Element.addClassName($('UploadFiles'),'link');//Safari hack
|
|
|
|
$('UploadFiles').innerHTML = 'Uploading ... ' + this.upload.getFilesUploaded() + "/" + this.upload.getFilesToUpload();
|
2007-09-16 04:21:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
uploadQueueCompleteCallback: function() {
|
2007-09-16 04:34:08 +02:00
|
|
|
this.filesUploaded = this.upload.getFilesUploaded();
|
2007-09-16 04:21:47 +02:00
|
|
|
$('UploadFiles').innerHTML = "upload";
|
2007-09-16 04:34:08 +02:00
|
|
|
statusMessage('Uploaded ' + this.upload.getFilesUploaded() + ' files','good');
|
|
|
|
if(this.getParentID() != 'root') {
|
|
|
|
$('Image').ajaxGetFiles(this.getParentID(),this.insertImages.bind(this));
|
2007-09-16 04:21:47 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Iterates over all uploaded images and add them to TinyMCE editor
|
|
|
|
*
|
|
|
|
* @param transport object
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
insertImages: function(transport) {
|
|
|
|
//HACK FOR STRANGE ERROR OCCURING UNDER SAFARI
|
|
|
|
if(transport.responseText == '') {
|
|
|
|
$('Image').ajaxGetFiles(this.getParentID(),this.insertImages.bind(this));
|
|
|
|
return;
|
2007-09-16 04:21:47 +02:00
|
|
|
}
|
2007-09-16 04:34:08 +02:00
|
|
|
//END OF HACK
|
2007-09-16 04:21:47 +02:00
|
|
|
$('Image').reapplyBehaviour();
|
|
|
|
this.addToTinyMCE = this.addToTinyMCE.bind(this);
|
|
|
|
var childNodes = $('Image').childNodes[0].childNodes;
|
2007-09-16 04:34:08 +02:00
|
|
|
var newImages = $A(childNodes).slice(childNodes.length - this.filesUploaded);
|
2007-09-16 04:21:47 +02:00
|
|
|
newImages.each(function(item) {
|
|
|
|
tinyMCEImageEnhancement.addToTinyMCE(item.childNodes[0]);
|
|
|
|
});
|
2007-09-16 04:34:08 +02:00
|
|
|
this.processInProgress = false;
|
2007-09-16 04:21:47 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Adds particular image to TinyMCE. Most of code has been copied from tiny_mce_improvements.js / ImageThumbnail.onclick
|
|
|
|
* Sorry for not following DRY, I didn't want to break smth in tiny_mce_improvements.
|
|
|
|
*
|
|
|
|
* @param target object
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:21:47 +02:00
|
|
|
addToTinyMCE: function(target) {
|
|
|
|
var formObj = $('Form_EditorToolbarImageForm');
|
|
|
|
var altText = formObj.elements.AltText.value;
|
|
|
|
var cssClass = formObj.elements.CSSClass.value;
|
|
|
|
var baseURL = document.getElementsByTagName('base')[0].href;
|
|
|
|
var relativeHref = target.href.substr(baseURL.length)
|
|
|
|
if(!tinyMCE.selectedInstance) tinyMCE.selectedInstance = Toolbar.instance().editor;
|
|
|
|
if(tinyMCE.selectedInstance.contentWindow.focus) tinyMCE.selectedInstance.contentWindow.focus();
|
|
|
|
// Extract dest width and dest height from the class name
|
|
|
|
var destWidth = null;
|
|
|
|
var destHeight = null;
|
|
|
|
try {
|
|
|
|
var imgTag = target.getElementsByTagName('img')[0];
|
|
|
|
destWidth = imgTag.className.match(/destwidth=([0-9.\-]+)([, ]|$)/) ? RegExp.$1 : null;
|
|
|
|
destHeight = imgTag.className.match(/destheight=([0-9.\-]+)([, ]|$)/) ? RegExp.$1 : null;
|
|
|
|
} catch(er) {
|
|
|
|
}
|
|
|
|
TinyMCE_AdvancedTheme._insertImage(relativeHref, altText, 0, null, null, destWidth, destHeight, null, null, cssClass);
|
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Under IE6 when we click on "add folder" anchor, rest of anchors loose their correct position
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:21:47 +02:00
|
|
|
applyIE6Hack: function() {
|
|
|
|
if(BrowserDetect.browser == 'Explorer') {
|
|
|
|
elements = [$('FolderOk'),$('FolderCancel'),$('UploadFiles'),$('PipeSeparator')];
|
|
|
|
$A(elements).each(function(element) {
|
|
|
|
element.style.position = "relative";
|
|
|
|
element.style.top = "-3px";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removeIE6Hack: function() {
|
|
|
|
if(BrowserDetect.browser == 'Explorer') {
|
|
|
|
elements = [$('FolderOk'),$('FolderCancel'),$('UploadFiles'),$('PipeSeparator')];
|
|
|
|
$A(elements).each(function(element) {
|
|
|
|
element.style.position = "";
|
|
|
|
});
|
|
|
|
}
|
2007-09-16 04:34:08 +02:00
|
|
|
},
|
|
|
|
|
2007-09-16 04:36:42 +02:00
|
|
|
/**
|
|
|
|
* Returns id of upload folder.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-16 04:34:08 +02:00
|
|
|
getParentID: function() {
|
|
|
|
return $('Form_EditorToolbarImageForm_FolderID').value == '' ? 'root' : $('Form_EditorToolbarImageForm_FolderID').value;
|
2007-09-16 04:21:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tinyMCEImageEnhancement = new TinyMCEImageEnhancement();
|