egroupware_official/api/js/etemplate/et2_widget_vfs.js

1243 lines
31 KiB
JavaScript
Raw Normal View History

2012-03-26 21:46:51 +02:00
/**
* EGroupware eTemplate2 - JS VFS widgets
2012-03-26 21:46:51 +02:00
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package etemplate
* @subpackage api
* @link http://www.egroupware.org
* @author Nathan Gray
* @copyright Nathan Gray 2012
* @version $Id$
*/
/*egw:uses
2016-06-06 17:38:20 +02:00
/vendor/bower-asset/jquery/dist/jquery.js;
2017-10-11 18:27:42 +02:00
vfsSelectUI;
2012-03-26 21:46:51 +02:00
et2_core_inputWidget;
et2_core_valueWidget;
2013-02-13 10:23:33 +01:00
et2_widget_description;
2013-02-20 21:53:15 +01:00
et2_widget_file;
2016-03-19 14:48:07 +01:00
expose;
2012-03-26 21:46:51 +02:00
*/
/**
* Class which implements the "vfs" XET-Tag
*
* @augments et2_valueWidget
2012-03-26 21:46:51 +02:00
*/
var et2_vfs = (function(){ "use strict"; return et2_valueWidget.extend([et2_IDetachedDOM],
{
2012-03-26 21:46:51 +02:00
attributes: {
"value": {
"type": "any", // Object
2012-03-27 01:30:27 +02:00
"description": "Array of (stat) information about the file"
2012-03-26 21:46:51 +02:00
}
},
/**
* Mime type of directories
*/
DIR_MIME_TYPE: 'httpd/unix-directory',
/**
* Constructor
*
* @memberOf et2_vfs
*/
2012-03-26 21:46:51 +02:00
init: function() {
this._super.apply(this, arguments);
this.value = "";
this.span = jQuery(document.createElement("ul"))
2012-03-26 21:46:51 +02:00
.addClass('et2_vfs');
this.setDOMNode(this.span[0]);
},
2012-07-03 01:02:57 +02:00
getValue: function() {
return this.value;
},
2012-03-26 21:46:51 +02:00
set_value: function(_value) {
if (typeof _value !== 'object')
2012-03-26 21:46:51 +02:00
{
// Only warn if it's an actual value, just blank for falsy values
if(_value)
{
this.egw().debug("warn", "%s only has path, needs full array", this.id, _value);
}
2012-03-26 21:46:51 +02:00
this.span.empty().text(_value);
return;
}
this.span.empty();
2012-07-03 01:02:57 +02:00
this.value = _value;
var path = _value.path ? _value.path : '/';
// calculate path as parent of name, which can contain slashes
// eg. _value.path=/home/ralf/sub/file, _value.name=sub/file --> path=/home/ralf
// --> generate clickable fields for sub/ + file
var sub_path = path.substring(0, _value.path.length-_value.name.length-1);
if(_value.path.indexOf(_value.name) >= 0 && sub_path[sub_path.length-1] === '/')
{
path = sub_path;
var path_offset = path.split('/').length;
var path_parts = _value.path.split('/');
}
else
{
2017-07-27 00:26:56 +02:00
if(_value.path.indexOf(_value.name) >= 0)
{
// Remove name from end, so we can add it again later
path = sub_path;
}
var path_offset = 0;
var path_parts = _value.name.split('/');
}
2012-03-26 21:46:51 +02:00
2012-03-28 01:32:32 +02:00
var text;
for(var i = path_offset; i < path_parts.length; i++)
2012-03-26 21:46:51 +02:00
{
path += (path=='/'?'':'/')+path_parts[i];
text = egw.decodePath(path_parts[i]);
2012-03-26 21:46:51 +02:00
// Nice human-readable stuff for apps
if(path_parts[1] == 'apps')
{
switch(path_parts.length)
{
case 2:
if(i == 1)
{
text = this.egw().lang('applications');
}
break;
case 3:
if( i == 2)
{
text = this.egw().lang(path_parts[2]);
}
break;
case 4:
if(!isNaN(text))
{
var link_title = this.egw().link_title(path_parts[2],path_parts[3],
function(title) {
if(!title || this.value.name == title) return;
jQuery('li',this.span).last().text(title);
}, this
);
if(link_title && typeof link_title !== 'undefined') text = link_title;
2012-03-26 21:46:51 +02:00
}
break;
}
}
2012-07-03 01:02:57 +02:00
var self = this;
var data = {path: path, type: i < path_parts.length-1 ? this.DIR_MIME_TYPE : _value.mime };
var node = jQuery(document.createElement("li"))
2012-03-26 21:46:51 +02:00
.addClass("vfsFilename")
.text(text + (i < path_parts.length-1 ? '/' : ''))
//.attr('title', egw.decodePath(path))
.addClass("et2_clickable et2_link")
.click({data:data, egw: this.egw()}, function(e) {
2012-07-03 01:02:57 +02:00
if(!self.onclick) {
e.data.egw.open(e.data.data, "file");
}
else if (self.click(e))
2012-07-03 01:02:57 +02:00
{
e.data.egw.open(e.data.data, "file");
}
})
2012-03-26 21:46:51 +02:00
.appendTo(this.span);
}
// Last part of path do default action
this._bind_default_action(node, data);
},
_bind_default_action: function(node, data)
{
var links = [];
var widget = this;
var defaultAction = null;
var object = null;
while(links.length == 0 && widget.getParent())
{
object = egw_globalObjectManager.getObjectById(widget.id);
if(object && object.manager && object.manager.children)
{
links = object.manager.children;
}
widget = widget.getParent();
}
for (var k in links)
{
2018-07-26 23:30:14 +02:00
if (links[k].default && links[k].enabled.exec(links[k]))
{
defaultAction = links[k];
break;
}
}
if(defaultAction && !this.onclick)
{
node.off('click').click({data:data, egw: this.egw()}, function(e) {
// Wait until object selection happens
window.setTimeout(function() {
// execute default action
egw_keyHandler(EGW_KEY_ENTER, false, false, false);
});
// Select row
return true;
}.bind({data: data, object: object}));
}
2012-03-28 01:32:32 +02:00
},
/**
* Code for implementing et2_IDetachedDOM (data grid)
*
* @param {array} _attrs array of attribute-names to push further names onto
*/
getDetachedAttributes: function(_attrs)
{
_attrs.push("value");
},
getDetachedNodes: function()
{
return [this.span[0]];
},
setDetachedAttributes: function(_nodes, _values)
{
this.span = jQuery(_nodes[0]);
if(typeof _values["value"] != 'undefined')
{
this.set_value(_values["value"]);
}
}
2012-03-26 21:46:51 +02:00
});}).call(this);
2012-03-28 01:32:32 +02:00
et2_register_widget(et2_vfs, ["vfs"]);
2012-03-27 01:30:27 +02:00
/**
* vfs-name
* filename automatically urlencoded on return (urldecoded on display to user)
*
* @augments et2_textbox
2012-03-27 01:30:27 +02:00
*/
var et2_vfsName = (function(){ "use strict"; return et2_textbox.extend(
{
/**
* Constructor
*
* @memberOf et2_vfsName
*/
2012-03-27 01:30:27 +02:00
init: function() {
this._super.apply(this, arguments);
2012-03-27 01:30:27 +02:00
this.input.addClass("et2_vfs");
},
set_value: function(_value) {
if(_value.path)
{
_value = _value.path;
2012-03-27 01:30:27 +02:00
}
_value = egw.decodePath(_value);
2012-03-27 01:30:27 +02:00
this._super.apply(this,[_value]);
},
getValue: function() {
return egw.encodePath(this._super.apply(this)||'');
2012-03-27 01:30:27 +02:00
}
});}).call(this);
2012-03-27 01:30:27 +02:00
et2_register_widget(et2_vfsName, ["vfs-name"]);
/**
* vfs-name
* filename automatically urlencoded on return (urldecoded on display to user)
*
* @augments et2_textbox_ro
*/
var et2_vfsName_ro = (function(){ "use strict"; return et2_textbox_ro.extend(
{
/**
* Constructor
*
* @memberOf et2_vfsName_ro
*/
init: function() {
this._super.apply(this, arguments);
},
set_value: function(_value) {
if(_value.path)
{
_value = _value.path;
}
_value = egw.decodePath(_value);
this._super.apply(this,[_value]);
},
getValue: function() {
return egw.encodePath(this._super.apply(this));
}
});}).call(this);
et2_register_widget(et2_vfsName_ro, ["vfs-name_ro"]);
2012-03-28 01:32:32 +02:00
/**
* vfs-mime: icon for mimetype of file, or thumbnail
* incl. optional link overlay icon, if file is a symlink
*
* Creates following structure
* <span class="iconOverlayContainer">
* <img class="et2_vfs vfsMimeIcon" src="..."/>
* <span class="overlayContainer">
* <img class="overlay" src="etemplate/templates/default/images/link.png"/>
* </span>
* </span>
*
* span.overlayContainer is optional and only generated for symlinks
*
* @augments et2_valueWidget
2012-03-28 01:32:32 +02:00
*/
var et2_vfsMime = (function(){ "use strict"; return expose(et2_valueWidget.extend([et2_IDetachedDOM],
{
2012-03-28 01:32:32 +02:00
attributes: {
"value": {
"type": "any", // Object
"description": "Array of (stat) information about the file"
},
"size": {
"name": "Icon size",
2012-03-28 01:32:32 +02:00
"type": "integer",
"description": "Size of icon / thumbnail, in pixels",
"default": et2_no_init
},
2015-01-19 19:59:35 +01:00
"expose_callback":{
"name": "expose_callback",
"type": "js",
"default": et2_no_init,
"description": "JS code which is executed when expose slides."
},
expose_view: {
name: "Expose view",
type: "boolean",
default: true,
description: "Clicking on an image would popup an expose view"
},
thumb_mime_size:{
name: "Image thumbnail size",
type: "string",
default:"",
description:" Size of thumbnail in pixel for specified mime type with syntax of: mime_type(s),size (eg. image,video,128)"
2012-03-28 01:32:32 +02:00
}
2012-03-28 01:32:32 +02:00
},
2012-07-03 01:02:57 +02:00
legacyOptions:["size"],
/**
* Constructor
*
* @memberOf et2_vfsMime
*/
2012-03-28 01:32:32 +02:00
init: function() {
this._super.apply(this, arguments);
this.iconOverlayContainer = jQuery(document.createElement('span')).addClass('iconOverlayContainer');
2012-03-28 01:32:32 +02:00
this.image = jQuery(document.createElement("img"));
this.image.addClass("et2_vfs vfsMimeIcon");
2015-01-19 19:59:35 +01:00
this.iconOverlayContainer.append(this.image);
this.setDOMNode(this.iconOverlayContainer[0]);
},
/**
* Handler for expose slide action, from expose
* Returns data needed for the given index, or false to let expose handle it
*
* @param {Gallery} gallery
* @param {integer} index
* @param {DOMNode} slide
* @return {Array} array of objects consist of media contnet
*/
expose_onslide: function(gallery, index, slide)
{
var content = false;
if (this.options.expose_callback && typeof this.options.expose_callback == 'function')
{
//Call the callback to load more items
content = this.options.expose_callback.call(this,[gallery, index]);
if (content) this.add(content);
}
return content;
2012-03-28 01:32:32 +02:00
},
/**
* Function to get media content to feed the expose
*
* @param {type} _value
* @returns {Array} return an array of object consists of media content
*/
getMedia: function (_value)
{
var base_url = egw.webserverUrl.match(/^\//,'ig')?egw(window).window.location.origin + egw.webserverUrl:egw.webserverUrl;
var mediaContent = [{
title: _value.name,
type: _value.mime,
href: _value.download_url
}];
// check if download_url is not already an url (some stream-wrappers allow to specify that!)
if (_value.download_url && (_value.download_url[0] == '/' || _value.download_url.substr(0, 4) != 'http'))
{
mediaContent[0].href = base_url + _value.download_url;
if (mediaContent[0].href && mediaContent[0].href.match(/\/webdav.php/,'ig'))
{
mediaContent[0].download_href = mediaContent[0].href + '?download';
}
}
if (_value && _value.mime && _value.mime.match(/video\//,'ig'))
{
mediaContent[0].thumbnail = this.egw().mime_icon(_value.mime, _value.path, undefined, _value.mtime);
}
else
{
mediaContent[0].thumbnail = _value.path && _value.mime ?
this.egw().mime_icon(_value.mime, _value.path, undefined, _value.mtime) :
this.image.attr('src')+ '&thheight=128';
}
return mediaContent;
},
2012-03-28 01:32:32 +02:00
set_value: function(_value) {
2012-07-03 01:02:57 +02:00
if (typeof _value !== 'object')
{
this.egw().debug("warn", "%s only has path, needs array with path & mime", this.id, _value);
// Keep going, will be 'unknown type'
}
var src = this.egw().mime_icon(_value.mime, _value.path, undefined, _value.mtime);
2012-03-28 01:32:32 +02:00
if(src)
{
// Set size of thumbnail
if(src.indexOf("thumbnail.php") > -1)
{
if(this.options.size)
{
src += "&thsize="+this.options.size;
}
else if (this.options.thumb_mime_size)
{
var mime_size = this.options.thumb_mime_size.split(',');
var mime_regex = RegExp(_value.mime.split('/')[0]);
if (typeof mime_size != 'undefined' && jQuery.isArray(mime_size)
&& !isNaN(mime_size[mime_size.length-1]) && isNaN(mime_size[0]) && this.options.thumb_mime_size.match(mime_regex[0], 'ig'))
{
src += "&thsize=" + mime_size[mime_size.length-1];
}
}
2012-03-28 10:10:57 +02:00
this.image.css("max-width", "100%");
2012-03-28 01:32:32 +02:00
}
this.image.attr("src", src);
// tooltip for mimetypes with available detailed thumbnail
if (_value.mime && _value.mime.match(/application\/vnd\.oasis\.opendocument\.(text|presentation|spreadsheet|chart)/))
{
var tooltip_target = this.image.parent().parent().parent().length > 0 ?
// Nextmatch row
this.image.parent().parent().parent() :
// Not in nextmatch
this.image.parent();
tooltip_target.tooltip({
items:"img",
position: {my:"right top", at:"left top", collision:"flipfit"},
content: function(){
return '<img src="'+this.src+'&thsize=512"/>';
}
});
}
2012-03-28 01:32:32 +02:00
}
// add/remove link icon, if file is (not) a symlink
if ((_value.mode & et2_vfsMode.prototype.types.l) == et2_vfsMode.prototype.types.l)
{
if (typeof this.overlayContainer == 'undefined')
{
this.overlayContainer = jQuery(document.createElement('span')).addClass('overlayContainer');
this.overlayContainer.append(jQuery(document.createElement('img'))
.addClass('overlay').attr('src', this.egw().image('link', 'etemplate')));
this.iconOverlayContainer.append(this.overlayContainer);
}
}
else if (typeof this.overlayContainer != 'undefined')
{
this.overlayContainer.remove();
delete this.overlayContainer;
}
2012-03-28 01:32:32 +02:00
},
/**
* Implementation of "et2_IDetachedDOM" for fast viewing in gridview
* Override to add needed attributes
*
* @param {array} _attrs array of attribute-names to push further names onto
2012-03-28 01:32:32 +02:00
*/
getDetachedAttributes: function(_attrs) {
_attrs.push("value", "class");
},
getDetachedNodes: function() {
return [this.node, this.iconOverlayContainer[0], this.image[0]];
2012-03-28 01:32:32 +02:00
},
setDetachedAttributes: function(_nodes, _values) {
this.iconOverlayContainer = jQuery(_nodes[1]);
this.image = jQuery(_nodes[2]);
this.node = _nodes[0];
this.overlayContainer = _nodes[0].children[1];
2012-03-28 01:32:32 +02:00
if(typeof _values['class'] != "undefined") {
this.image.addClass(_values['class']);
}
if(typeof _values['value'] != "undefined") {
this.set_value(_values['value']);
}
}
}));}).call(this);
2012-03-28 01:32:32 +02:00
et2_register_widget(et2_vfsMime, ["vfs-mime"]);
2012-03-27 01:30:27 +02:00
/**
* vfs-size
* Human readable file sizes
*
* @augments et2_description
2012-03-27 01:30:27 +02:00
*/
var et2_vfsSize = (function(){ "use strict"; return et2_description.extend({
attributes: {
"value": {
"type": "integer"
}
},
/**
* Constructor
*
* @memberOf et2_vfsSize
*/
2012-03-27 01:30:27 +02:00
init: function() {
2012-03-28 10:10:57 +02:00
this._super.apply(this, arguments);
2012-03-27 01:30:27 +02:00
this.span.addClass("et2_vfs");
},
human_size: function(size) {
if(typeof size !== "number")
{
size = parseInt(size);
}
if(!size)
{
size = 0;
}
var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var i = 0;
while(size >= 1024)
{
size /= 1024;
++i;
}
return size.toFixed(i == 0 ? 0 : 1) + ' ' + units[i];
},
set_value: function(_value) {
if(_value.size)
{
_value = _value.size;
2012-03-27 01:30:27 +02:00
}
jQuery(this.node).text(this.human_size(_value));
},
setDetachedAttributes: function(_nodes, _values)
{
2012-03-27 01:30:27 +02:00
if(typeof _values["value"] !== "undefined") {
this.node = _nodes[0];
this.set_value(_values["value"]);
delete _values["value"];
}
this._super.apply(this, arguments);
}
});}).call(this);
2012-03-27 01:30:27 +02:00
et2_register_widget(et2_vfsSize, ["vfs-size"]);
/**
* vfs-mode: textual representation of permissions + extra bits
*
* @augments et2_description
2012-03-27 01:30:27 +02:00
*/
var et2_vfsMode = (function(){ "use strict"; return et2_description.extend({
2012-03-27 01:30:27 +02:00
// Masks for file types
types: {
2012-03-28 01:32:32 +02:00
'l': 0xA000, // link
's': 0xC000, // Socket
2012-03-27 01:30:27 +02:00
'p': 0x1000, // FIFO pipe
'c': 0x2000, // Character special
'd': 0x4000, // Directory
'b': 0x6000, // Block special
2012-03-28 01:32:32 +02:00
'-': 0x8000 // Regular
2012-03-27 01:30:27 +02:00
},
// Sticky / UID / GID
2012-03-27 01:30:27 +02:00
sticky: [
{ mask: 0x200, "char": "T", position: 9 }, // Sticky
{ mask: 0x400, "char": "S", position: 6 }, // sGID
{ mask: 0x800, "char": "S", position: 3 } // SUID
],
perms: {
'x': 0x1, // Execute
'w': 0x2, // Write
'r': 0x4 // Read
},
/**
* Constructor
*
* @memberOf et2_vfsMode
*/
2012-03-27 01:30:27 +02:00
init: function() {
this._super.apply(this, arguments);
2012-03-27 01:30:27 +02:00
this.span.addClass("et2_vfs");
},
/**
* Get text for file stuff
* Result will be like -rwxr--r--. First char is type, then read, write, execute (or other bits) for
* user, group, world
*
* @param {number} _value vfs mode
2012-03-27 01:30:27 +02:00
*/
text_mode: function(_value) {
var text = [];
if(typeof _value != "number")
{
_value = parseInt(_value);
}
if(!_value) return "----------";
// Figure out type
var type = 'u'; // unknown
for(var flag in this.types)
{
if((_value & this.types[flag]) == this.types[flag])
2012-03-27 01:30:27 +02:00
{
type = flag;
break;
}
}
// World, group, user - build string backwards
for(var i = 0; i < 3; i++)
{
for(var perm in this.perms)
{
if(_value & this.perms[perm])
{
text.unshift(perm);
}
else
{
text.unshift("-");
}
}
_value = _value >> 3;
}
// Sticky / UID / GID
for(var i = 0; i < this.sticky.length; i++)
{
if(this.sticky[i].mask & _value)
{
current = text[this.sticky[i].position];
2012-03-28 01:32:32 +02:00
text[this.sticky[i].position] = this.sticky[i]["char"];
if(current == 'x') text[this.sticky[i].position].toLowerCase();
2012-03-27 01:30:27 +02:00
}
}
return type + text.join('');
},
set_value: function(_value) {
if(_value.size)
{
_value = _value.size;
2012-03-27 01:30:27 +02:00
}
var text = this.text_mode(_value);
jQuery(this.node).text(text);
},
setDetachedAttributes: function(_nodes, _values)
{
2012-03-27 01:30:27 +02:00
if(typeof _values["value"] !== "undefined") {
this.node = _nodes[0];
this.set_value(_values["value"]);
delete _values["value"];
}
this._super.apply(this, arguments);
}
});}).call(this);
2012-03-27 01:30:27 +02:00
et2_register_widget(et2_vfsMode, ["vfs-mode"]);
/**
* vfs-uid / vfs-gid: Displays the name for an ID.
2012-03-27 01:30:27 +02:00
* Same as read-only selectAccount, except if there's no user it shows "root"
*
* @augments et2_selectAccount_ro
2012-03-27 01:30:27 +02:00
*/
var et2_vfsUid = (function(){ "use strict"; return et2_selectAccount_ro.extend(
{
/**
* @memberOf et2_vfsUid
* @param _node
* @param _value
*/
2012-03-27 01:30:27 +02:00
set_title: function(_node, _value) {
if(_value == "")
{
arguments[1] = "root";
}
this._super.apply(this, arguments);
}
});}).call(this);
2012-03-27 01:30:27 +02:00
et2_register_widget(et2_vfsUid, ["vfs-uid","vfs-gid"]);
2012-03-29 01:27:18 +02:00
/* vfs-upload aka VFS file: displays either download and delete (x) links or a file upload
* + value is either a vfs path or colon separated $app:$id:$relative_path, eg: infolog:123:special/offer
* + if empty($id) / new entry, file is created in a hidden temporary directory in users home directory
* and calling app is responsible to move content of that dir to entry directory, after entry is saved
* + option: required mimetype or regular expression for mimetype to match, eg. '/^text\//i' for all text files
* + if path ends in a slash, multiple files can be uploaded, their original filename is kept then
*
* @augments et2_file
2012-03-29 01:27:18 +02:00
*/
var et2_vfsUpload = (function(){ "use strict"; return et2_file.extend(
{
attributes: {
"value": {
"type": "any" // Either nothing, or an object with file info
},
"path": {
"name": "Path",
"description": "Upload files to the specified VFS path",
"type": "string",
"default": ''
}
},
legacyOptions: ["mime"],
2012-03-29 01:27:18 +02:00
asyncOptions: {
target: egw.ajaxUrl("EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_upload")
2012-03-29 01:27:18 +02:00
},
/**
* Constructor
*
* @param _parent
* @param attrs
* @memberof et2_vfsUpload
*/
init: function(_parent, attrs) {
2012-03-29 01:27:18 +02:00
this._super.apply(this, arguments);
jQuery(this.node).addClass("et2_vfs");
if(!this.options.path)
{
this.options.path = this.options.id;
}
// If the path is a directory, allow multiple uploads
if(this.options.path.substr(-1) == '/')
{
this.set_multiple(true);
}
this.list = jQuery(document.createElement('table')).appendTo(this.node);
2012-03-29 01:27:18 +02:00
},
/**
* If there is a file / files in the specified location, display them
* Value is the information for the file[s] in the specified location.
*
* @param {Object[]} _value
*/
2012-03-29 01:27:18 +02:00
set_value: function(_value) {
// Remove previous
while(this._children.length > 0)
{
var node = this._children[this._children.length-1];
this.removeChild(node);
node.free();
}
this.progress.empty();
this.list.empty();
// Set new
if(typeof _value == 'object' && _value && _value.length)
{
for(var i = 0; i < _value.length; i++)
{
this._addFile(_value[i]);
}
}
},
getDOMNode: function(sender) {
if(sender !== this && sender._type.indexOf('vfs') >= 0 )
{
var value = sender.getValue && sender.getValue() || sender.options.value || {};
var row = jQuery("[data-path='"+(value.path.replace(/'/g, '&quot'))+"']",this.list);
if(sender._type === 'vfs-mime')
{
return jQuery('.icon',row).get(0) || null;
}
else
{
return jQuery('.title',row).get(0) || null;
}
}
else
{
return this._super.apply(this, arguments);
}
},
/**
* Add in the request id
*
* @param {type} form
*/
beforeSend: function(form)
{
var instance = this.getInstanceManager();
var extra = this._super.apply(this, arguments);
extra.path = this.options.path;
return extra;
},
/**
* A file upload is finished, update the UI
*/
finishUpload: function(file, response) {
var result = this._super.apply(this, arguments);
if(typeof response == 'string') response = jQuery.parseJSON(response);
if(response.response[0] && typeof response.response[0].data.length == 'undefined') {
for(var key in response.response[0].data) {
var value = response.response[0].data[key];
if(value && value.path)
{
this._addFile(value);
jQuery("[data-file='"+file.fileName.replace(/'/g, '&quot')+"']",this.progress).hide();
}
}
}
return result;
},
_addFile: function(file_data) {
if(jQuery("[data-path='"+file_data.path.replace(/'/g, '&quot')+"']").remove().length)
{
for(var child_index = this._children.length-1; child_index >= 0; child_index--)
{
var child = this._children[child_index];
if(child.options.value.path === file_data.path)
{
this.removeChild(child);
child.free();
}
}
}
var row = jQuery(document.createElement("tr"))
.attr("data-path", file_data.path.replace(/'/g, '&quot'))
.attr("draggable", "true")
.appendTo(this.list);
var mime = jQuery(document.createElement("td"))
.addClass('icon')
.appendTo(row);
var title = jQuery(document.createElement("td"))
.addClass('title')
.appendTo(row);
var mime = et2_createWidget('vfs-mime',{value: file_data},this);
var vfs = et2_createWidget('vfs', {value: file_data}, this);
// If already attached, need to do this explicitly
if(this.isAttached())
{
mime.set_value(file_data);
vfs.set_value(file_data);
mime.doLoadingFinished();
vfs.doLoadingFinished();
}
// Add in delete button
if (!this.options.readonly)
{
var self = this;
var delete_button = jQuery(document.createElement("td"))
.appendTo(row);
jQuery("<div />")
.appendTo(delete_button)
// We don't use ui-icon because it assigns a bg image
.addClass("delete icon")
.bind( 'click', function() {
et2_createWidget("dialog", {
callback: function(button) {
if(button == et2_dialog.YES_BUTTON)
{
egw.json("filemanager_ui::ajax_action", [
'delete',
[row.attr('data-path').replace(/&quot/g, "'")],
''
],
function(data) {
if(data && data.errs == 0) {row.slideUp(row.remove);}
if(data && data.msg) {
self.egw().message(data.msg, data.errs == 0 ? 'success' : 'error');
}
}
).sendRequest();
}
},
message: self.egw().lang('Delete file')+'?',
title: self.egw().lang('Confirmation required'),
buttons: et2_dialog.BUTTONS_YES_NO,
dialog_type: et2_dialog.QUESTION_MESSAGE,
width: 250
}, self);
});
}
2012-03-29 01:27:18 +02:00
}
});}).call(this);
2012-03-29 01:27:18 +02:00
et2_register_widget(et2_vfsUpload, ["vfs-upload"]);
var et2_vfsSelect = (function(){ "use strict"; return et2_inputWidget.extend(
{
// Allowed mode options
modes: ['open','open-multiple','saveas','select-dir'],
attributes: {
"mode": {
name: "Dialog mode",
type: "string",
description: "One of {open|open-multiple|saveas|select-dir}",
default: "open-multiple"
},
"method": {
name: "Server side callback",
type: "string",
2017-10-11 18:27:42 +02:00
description: "Server side callback to process selected value(s) in \n\
app.class.method or class::method format. The first parameter will \n\
be Method ID, the second the file list. 'ckeditor' is reserved and it \n\
means it should use download_baseUrl instead of path in value (no method\n\
will be actually executed)."
},
"method_id": {
name: "Method ID",
type: "any",
2017-10-11 18:27:42 +02:00
description: "optional parameter passed to server side callback.\n\
Can be a string or a function.",
default: ""
},
"path": {
name: "Path",
type: "string",
description:"Start path in VFS. Leave unset to use the last used path."
},
"mime": {
name: "Mime type",
2017-11-03 15:05:35 +01:00
type: "any",
description: "Limit display to the given mime-type"
},
"button_label": {
name: "Button label",
description: "Set the label on the dialog's OK button.",
default: "open"
},
"value": {
type: "any", // Object
description: "Array of paths (strings)"
},
"button_caption":{
name: "button caption",
type: "string",
default: "Select files from Filemanager ...",
description: "Caption for vfs-select button.",
translate:true
},
"button_icon":{
name: "button icon",
type: "string",
default: "check",
description: "Custom icon to show on submit button.",
},
"name": {
name:"File name",
type: "any", // Object
description: "file name",
default: ""
},
"dialog_title":{
name: "dialog title",
type: "string",
default: "Save as",
description: "Title of dialog",
translate:true
},
"extra_buttons": {
name: "extra action buttons",
type: "any",
description: "Extra buttons passed to dialog. It's co-related to method.",
}
},
/**
* Constructor
*
* @param _parent
* @param _attrs
* @memberOf et2_vfsSelect
*/
init: function(_parent, _attrs) {
// _super.apply is responsible for the actual setting of the params (some magic)
this._super.apply(this, arguments);
// Allow no child widgets
this.supportedWidgetClasses = [];
this.button = jQuery(document.createElement("button"))
.attr("title", this.egw().lang("Select file(s) from VFS"))
.addClass("et2_button et2_vfs_btn")
.css("background-image","url("+this.egw().image("filemanager/navbar")+")");
if(this.options.readonly)
{
this.button.hide();
}
2017-10-11 18:27:42 +02:00
if (this.options.button_caption != "")
{
this.button.text(this.options.button_caption);
}
this.setDOMNode(this.button[0]);
},
2017-10-11 18:27:42 +02:00
_content: function (_content, _callback)
{
egw(window).loading_prompt('vfs-select', true, '', 'body');
var self = this;
2017-10-11 18:27:42 +02:00
if (typeof app.vfsSelectUI !="undefined")
{
if (this.dialog && this.dialog.div) this.dialog.div.dialog('close');
delete app.vfsSelectUI;
}
var attrs = {
mode: this.options.mode,
label: this.options.button_label,
2017-10-11 18:27:42 +02:00
path: this.options.path || null,
mime: this.options.mime || null,
name: this.options.name,
method: this.options.method
};
2017-10-11 18:27:42 +02:00
var callback = _callback || this._buildDialog;
egw(window).json(
'EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_vfsSelect_content',
[_content, attrs],
function(_content){
egw(window).loading_prompt('vfs-select', false);
callback.apply(self, arguments);
}
).sendRequest(true);
},
/**
* Builds file navigator dialog
*
* @param {object} _data content
*/
_buildDialog: function (_data)
{
if (!_data.content.mode.match(/open|open-multiple|saveas|select-dir/)) {
egw.debug('Mode is not matched!');
return;
}
2017-10-11 18:27:42 +02:00
var self = this;
var buttons = [
{
text: egw.lang(_data.content.label),
id:"submit",
image: _data.content.mode.match(/saveas|select-dir/) ? "save" : this.options.button_icon
}
2017-10-11 18:27:42 +02:00
];
if (this.options.extra_buttons && this.options.method)
{
var extra_buttons_action = [];
for (var i=0; i < this.options.extra_buttons.length; i++)
{
delete(this.options.extra_buttons[i]['click']);
buttons.push(this.options.extra_buttons[i]);
extra_buttons_action[this.options.extra_buttons[i]['id']] = this.options.extra_buttons[i]['id'];
}
}
buttons.push({text: egw.lang("Close"), id:"close"});
var data = jQuery.extend(_data, {'currentapp': egw(window).app_name()});
2017-10-11 18:27:42 +02:00
// define a mini app object for vfs select UI
app.vfsSelectUI = new app.classes.vfsSelectUI;
this.dialog = et2_createWidget("dialog",
{
2017-10-11 18:27:42 +02:00
callback: function(_button_id, _value)
{
if ((_button_id == 'submit' || (extra_buttons_action && extra_buttons_action[_button_id])) && _value)
2017-10-11 18:27:42 +02:00
{
var files = [];
switch(_data.content.mode)
{
case 'open-multiple':
if (_value.dir && _value.dir.selected)
{
for(var key in Object.keys(_value.dir.selected))
{
if (_value.dir.selected[key] != "")
{
files.push(_value.path+'/'+_value.dir.selected[key]);
}
}
2017-10-11 18:27:42 +02:00
}
break;
case 'select-dir':
files = _value.path;
break;
default:
if (self.options.method === 'ckeditor') _value.path = _data.content.download_baseUrl;
2017-10-11 18:27:42 +02:00
files = _value.path+'/'+_value.name;
break;
}
self.value = files;
if (self.options.method && self.options.method !== 'ckeditor')
2017-10-11 18:27:42 +02:00
{
egw(window).json(
self.options.method,
[self.options.method_id, files, _button_id],
2017-10-11 18:27:42 +02:00
function(){
jQuery(self.node).change();
}
).sendRequest(true);
}
else
{
jQuery(self.node).change();
}
2017-10-11 18:27:42 +02:00
delete app.vfsSelectUI;
return true;
2017-10-11 18:27:42 +02:00
}
},
title: this.options.dialog_title,
2017-10-11 18:27:42 +02:00
buttons: buttons,
minWidth: 500,
minHeight: 400,
width:400,
2017-10-11 18:27:42 +02:00
value: data,
template: egw.webserverUrl+'/api/templates/default/vfsSelectUI.xet?1',
resizable: false
}, et2_dialog._create_parent('api'));
this.dialog.template.uniqueId = 'api.vfsSelectUI';
// Don't rely only on app_name to fetch et2 object as app_name may not
// always represent current app of the window, e.g.: mail admin account.
// Try to fetch et2 from its template name.
var etemplate = jQuery('form').data('etemplate');
var et2 = {};
if (etemplate && etemplate.name && !app[egw(window).app_name()])
{
et2 = etemplate2.getByTemplate(etemplate.name)[0]
}
else
{
et2 = etemplate2.getByApplication(egw(window).app_name())[0];
}
// we need an etemplate_exec_id for better handling serverside parts of
// widgets and since we can not have a etemplate_exec_id specifically
// for dialog template our best shot is to inherit its parent etemplate_exec_id.
this.dialog.template.etemplate_exec_id = et2.etemplate_exec_id;
2017-10-11 18:27:42 +02:00
app.vfsSelectUI.et2 = this.dialog.template.widgetContainer;
// Keep the dialog always at the top, seems CKEDITOR dialogs have very
// high z-index set.
this.dialog.div.parent().css({"z-index": 100000});
2017-10-11 18:27:42 +02:00
app.vfsSelectUI.vfsSelectWidget = this;
this.dialog.div.on('load', function(e) {
app.vfsSelectUI.et2_ready(app.vfsSelectUI.et2, 'api.vfsSelectUI');
});
},
/**
* click handler
* @param {event object} e
*/
click: function(e) {
this._content.call(this, null);
},
/**
* Set the dialog's mode.
* Valid options are in et2_vfsSelect.modes
*
* @param {string} mode 'open', 'open-multiple', 'saveas' or 'select-dir'
*/
set_mode: function(mode) {
// Check mode
if(jQuery.inArray(mode, this.modes) < 0)
{
this.egw().debug("warn", "Invalid mode for '%s': %s Valid options:", this.id,mode, this.modes);
return;
}
this.options.mode = mode;
},
/**
* Set the label on the dialog's OK button.
*
* @param {string} label
*/
set_button_label: function(label)
{
this.options.button_label = label;
},
/**
* Set the caption for vfs-select button
*
* @param {string} caption string value as a caption
*/
set_button_caption: function (caption)
{
this.options.button_caption = caption;
},
/**
* Set the ID passed to the server side callback
*
* @param {string} id
*/
set_method_id: function(id) {
this.options.method_id = id;
},
set_readonly: function(readonly) {
this.options.readonly = Boolean(readonly);
2017-10-11 18:27:42 +02:00
if(this.options.readonly)
{
this.button.hide();
}
else
{
this.button.show();
}
},
2018-06-13 22:44:12 +02:00
set_value: function(value) {
this.value = value;
},
2013-10-01 17:52:25 +02:00
getValue: function() {
return this.value;
}
});}).call(this);
et2_register_widget(et2_vfsSelect, ["vfs-select"]);