silverstripe-framework/javascript/HasManyFileField.js
Ingo Schommer 6bd34d3e53 ENHANCEMENT Using ss.i18n in all sapphire form fields with clientside language strings
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63567 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-10-03 00:47:08 +00:00

142 lines
3.9 KiB
JavaScript

HasManyFileField = Class.create();
HasManyFileField.applyTo('div.hasmanyfile');
HasManyFileField.prototype = {
initialize: function() {
HasManyFileFieldAddButton.applyToChildren(this, 'a.addFile');
HasManyFileFieldRemoveButton.applyToChildren(this, 'a.removeFile');
HasManyFileFieldUploadButton.applyToChildren(this, 'a.uploadFile');
this.tree = document.getElementsByClassName('TreeDropdownField', this)[0];
this.list = this.getElementsByTagName('ul')[0];
this.uploadFolderID = this.getElementsByTagName('input')[0].value;
this.uploadMessage = document.getElementsByClassName('uploadMessage')[0];
}
}
HasManyFileFieldAddButton = Class.create();
HasManyFileFieldAddButton.prototype = {
onclick: function() {
tree = this.parentNode.parentNode.tree;
list = this.parentNode.parentNode.list;
fieldid = this.parentNode.parentNode.id;
fileid = tree.getElementsByTagName('input')[0].value;
name = tree.getElementsByTagName('span')[0].innerHTML;
input = document.createElement('input');
input.className = 'hidden';
input.type = 'hidden';
input.name = fieldid + '[]';
input.value = fileid;
text = document.createTextNode(name);
link = document.createElement('a');
link.appendChild(text);
removelink = document.createElement('a');
removelink.className = 'removeFile';
removelink.innerHTML = 'Remove file';
li = document.createElement('li');
li.appendChild(input);
li.appendChild(link);
li.appendChild(removelink);
list.appendChild(li);
HasManyFileFieldRemoveButton.applyTo(removelink);
return false;
}
}
HasManyFileFieldRemoveButton = Class.create();
HasManyFileFieldRemoveButton.prototype = {
onclick: function() {
li = this.parentNode;
list = this.parentNode.parentNode;
list.removeChild(li);
return false;
}
}
HasManyFileFieldUploadButton = Class.create();
HasManyFileFieldUploadButton.prototype = {
initialize: function() {
this.upload = new Upload({
fileUploadLimit : '6',
securityID : $('SecurityID').value,
beginUploadOnQueue : true,
fileQueued : this.uploadFileQueuedCallback.bind(this),
fileComplete : this.uploadFileCompleteCallback.bind(this),
queueComplete : this.uploadQueueCompleteCallback.bind(this)
});
this.upload.setFolderID(this.parentNode.parentNode.uploadFolderID);
},
buildUI: function() {
},
onclick: function(event) {
Event.stop(event);
this.upload.browse();
},
uploadFileQueuedCallback: function(file,queueLength) {
var message = ss.i18n.sprintf(
ss.i18n._t('HASMANYFILEFIELD.UPLOADING'),
this.upload.getFilesToUpload()
);
this.parentNode.parentNode.uploadMessage.innerHTML = message;
},
uploadFileCompleteCallback: function(file,serverData) {
var message = ss.i18n.sprintf(
ss.i18n._t('HASMANYFILEFIELD.UPLOADING'),
this.upload.getFilesUploaded() + "/" + this.upload.getFilesToUpload()
);
this.parentNode.parentNode.uploadMessage.innerHTML = message;
idregex = /\/\* IDs: ([0-9,]+) \*\//;
ids = serverData.match(idregex);
fileid = ids[1];
console.log(serverData);
nameregex = /\/\* Names: ([^\s]+) \*\//;
names = serverData.match(nameregex);
name = names[1];
console.log(name);
fieldid = this.parentNode.parentNode.id;
list = this.parentNode.parentNode.list;
input = document.createElement('input');
input.className = 'hidden';
input.type = 'hidden';
input.name = fieldid + '[]';
input.value = fileid;
text = document.createTextNode(name);
link = document.createElement('a');
link.appendChild(text);
removelink = document.createElement('a');
removelink.className = 'removeFile';
removelink.innerHTML = 'Remove file';
li = document.createElement('li');
li.appendChild(input);
li.appendChild(link);
li.appendChild(removelink);
list.appendChild(li);
HasManyFileFieldRemoveButton.applyTo(removelink);
},
uploadQueueCompleteCallback: function() {
this.parentNode.parentNode.uploadMessage.innerHTML = '';
}
}