Give filemanager selection a widget front-end.

This commit is contained in:
Nathan Gray 2013-10-01 15:40:14 +00:00
parent e2bc78b9e3
commit a8136aa191
6 changed files with 234 additions and 87 deletions

View File

@ -230,9 +230,6 @@ class etemplate_widget_link extends etemplate_widget
$link = egw_vfs::concat($app_path,egw_vfs::basename($target));
egw_vfs::symlink($target,$link);
}
// Return js to refresh opener and close popup
return 'window.close();';
}
public function ajax_delete($value) {

View File

@ -119,7 +119,8 @@ var et2_link_to = et2_inputWidget.extend(
return this.link_div[0];
} else if (_sender._type == 'file') {
return this.file_div[0];
} else if (_sender._type == 'vfs-select') {
return this.filemanager_button[0];
}
},
@ -152,40 +153,8 @@ var et2_link_to = et2_inputWidget.extend(
et2_link_entry.prototype.set_blur(this.egw().lang("Comment..."),this.comment);
// Filemanager link popup
var self = this;
this.filemanager_button = $j(document.createElement("img"))
.attr("src", this.egw().image("filemanager/navbar"))
.addClass("et2_button et2_button_icon")
.appendTo(this.div)
.click(this, function(e) {
// Open the filemanager select in a popup
var values = e.data.options.value;
var popup = e.data.egw().open_link(
'/index.php?menuaction=filemanager.filemanager_select.select&mode=open-multiple&method=etemplate_widget_link::link_existing&label=link&id=' + values.to_app + ":" + values.to_id,
'link_existing',
'640x580'
);
if(popup)
{
// Update on close doesn't always (ever, in chrome) work, so poll
var poll = self.egw().window.setInterval(
function() {
if(popup.closed) {
self.getRoot().iterateOver(
function(widget) {
if(widget.id == self.id) {
widget._get_links();
}
},
self, et2_link_list
);
self.egw().window.clearInterval(poll);
}
},1000);
}
});
this.filemanager_button = $j(document.createElement("div")).appendTo(this.div);
// Need a div for file upload widget
this.file_div = $j(document.createElement("div")).appendTo(this.div);
@ -208,6 +177,24 @@ var et2_link_to = et2_inputWidget.extend(
};
this.link_entry = et2_createWidget("link-entry", link_entry_attrs,this);
// Filemanager select
var select_attrs = {
method: 'etemplate_widget_link::link_existing',
method_id: function() { return self.options.value.to_app + ':' + self.options.value.to_id;},
button_label: egw.lang('Link')
}
this.vfs_select = et2_createWidget("vfs-select", select_attrs,this);
$j(this.vfs_select.getDOMNode()).change( function() {
self.getRoot().iterateOver(
function(widget) {
if(widget.id == self.id) {
widget._get_links();
}
},
self, et2_link_list
);
});
// File upload
var file_attrs = {
multiple: true,

View File

@ -555,3 +555,157 @@ var et2_vfsUpload = et2_file.extend(
}
});
et2_register_widget(et2_vfsUpload, ["vfs-upload"]);
var et2_vfsSelect = 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",
description: "Server side callback to process selected value(s) in app.class.method or class::method format. The first parameter will be Method ID, the second the file list."
},
"method_id": {
name: "Method ID",
type: "any",
description: "optional parameter passed to server side callback. 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",
type: "string",
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)"
}
},
/**
* 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 = $j(document.createElement("img"))
.attr("src", this.egw().image("filemanager/navbar"))
.addClass("et2_button et2_button_icon")
this.setDOMNode(egw.userData.apps.filemanager ? this.button[0]:document.createElement('span'));
},
click: function(e) {
// No permission
if(typeof egw.userData.apps.filemanager == 'undefined') return;
var self = this;
var attrs = {
menuaction: 'filemanager.filemanager_select.select',
mode: this.options.mode,
method: this.options.method,
label: this.options.button_label,
id: typeof this.options.method_id == "function" ? this.options.method_id.call(): this.options.method_id
};
if(this.options.path)
{
attrs.path = this.options.path;
}
if(this.options.mime)
{
attrs.mime = this.options.mime;
};
// Open the filemanager select in a popup
var popup = this.egw().open_link(
this.egw().link('/index.php', attrs),
'link_existing',
'640x580'
);
if(popup)
{
// Update on close doesn't always (ever, in chrome) work, so poll
var poll = self.egw().window.setInterval(
function() {
if(popup.closed) {
self.egw().window.clearInterval(poll);
// Update path to where the user wound up
var path = popup.etemplate2.getByApplication('filemanager')[0].widgetContainer.getArrayMgr("content").getEntry('path') || '';
self.options.path = path;
// Get the selected files
var files = popup.selected_files || [];
self.value = files;
// Fire a change event so any handlers run
$j(self.node).change();
}
},1000
);
}
},
/**
* Set the dialog's mode.
* Valid options are in et2_vfsSelect.modes
*
*/
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.
*/
set_button_label: function(label)
{
this.options.button_label = label;
},
/**
* Set the ID passed to the server side callback
*/
set_method_id: function(id) {
this.options.method_id = id;
},
get_value: function() {
return this.value;
}
});
et2_register_widget(et2_vfsSelect, ["vfs-select"]);

View File

@ -183,11 +183,11 @@ class filemanager_select
break;
}
if ($content['method'] != 'ckeditor_return')
if ($content['method'] && $content['method'] != 'ckeditor_return')
{
$js = ExecMethod2($content['method'],$content['id'],$files);
}
else
else if ($content['method'] == 'ckeditor_return')
{
$download_url = egw_vfs::download_url(egw_vfs::concat($content['path'],$content['name']));
if ($download_url[0] == '/') $download_url = egw::link($download_url);
@ -196,8 +196,22 @@ class filemanager_select
htmlspecialchars($download_url)."',".
"'');\nwindow.close();";
}
header('Content-type: text/html; charset='.translation::charset());
echo "<html>\n<head>\n<script type='text/javascript'>\n$js\n</script>\n</head>\n</html>\n";
if(egw_json_response::isJSONResponse())
{
$response = egw_json_response::get();
if($js)
{
$response->script($js);
}
// Ahh!
// The vfs-select widget looks for this
$response->script('this.selected_files = '.json_encode($files) . '; this.close();');
}
else
{
header('Content-type: text/html; charset='.translation::charset());
echo "<html>\n<head>\n<script type='text/javascript'>\n$js\n</script>\n</head>\n</html>\n";
}
common::egw_exit();
}
}
@ -228,7 +242,7 @@ class filemanager_select
{
$content['path'] = filemanager_ui::get_home_dir();
}
$tpl = new etemplate('filemanager.select');
$tpl = new etemplate_new('filemanager.select');
$et2 = class_exists('etemplate_widget', false) && is_a($tpl, 'etemplate_widget');
if (!($files = egw_vfs::find($content['path'],array(
@ -259,9 +273,9 @@ class filemanager_select
'name' => $name,
'path' => $path,
'mime' => $mime,
'onclick' => $is_dir ? "return select_goto('".addslashes($path)."'".($et2?',widget':'').");" :
($content['mode'] != 'open-multiple' ? "return select_show('".addslashes($name)."');" :
"return select_toggle('".addslashes($name)."');"),
'onclick' => $is_dir ? "return app.filemanager.select_goto('".addslashes($path)."'".($et2?',widget':'').");" :
($content['mode'] != 'open-multiple' ? "return app.filemanager.select_show('".addslashes($name)."');" :
"return app.filemanager.select_toggle('".addslashes($name)."');"),
);
if ($is_dir && $content['mode'] == 'open-multiple')
{
@ -273,45 +287,6 @@ class filemanager_select
}
$readonlys['button[createdir]'] = !egw_vfs::is_writable($content['path']);
$content['js'] = '<script type="text/javascript">
function select_goto(to,widget)
{
path = document.getElementById("exec[path]");
if(path)
{
path.value = to;
path.form.submit();
}
else if (widget)
{
var path = null;
// Cannot do this, there are multiple widgets named path
// widget.getRoot().getWidgetById("path");
widget.getRoot().iterateOver(function(widget) {
if(widget.id == "path") path = widget;
},null, et2_textbox);
if(path)
{
path.set_value(to);
path.getInstanceManager().postSubmit();
}
}
return false;
}
function select_show(file)
{
var editfield = document.getElementById("exec[name]");
editfield.value = file;
return false;
}
function select_toggle(file)
{
checkbox = document.getElementById("exec[dir][selected]["+file+"]");
if (checkbox) checkbox.checked = !checkbox.checked;
return false;
}
</script>
';
// scroll to end of path
$GLOBALS['egw']->js->set_onload("var p = document.getElementById('exec[path][c". (count(explode('/',$content['path']))-1) ."]'); if (p) p.scrollIntoView();");

View File

@ -643,5 +643,40 @@ app.filemanager = AppJS.extend(
}
}
}
},
/**
* Functions for the select dialog
*/
select_goto: function(to,widget)
{
if (widget)
{
var path = null;
// Cannot do this, there are multiple widgets named path
// widget.getRoot().getWidgetById("path");
widget.getRoot().iterateOver(function(widget) {
if(widget.id == "path") path = widget;
},null, et2_textbox);
if(path)
{
path.set_value(to);
path.getInstanceManager().postSubmit();
}
}
return false;
},
select_show: function(file)
{
var editfield = document.getElementById("exec[name]");
editfield.value = file;
return false;
},
select_toggle: function(file)
{
checkbox = document.getElementById("exec[dir][selected]["+file+"]");
if (checkbox) checkbox.checked = !checkbox.checked;
return false;
}
});

View File

@ -34,9 +34,8 @@
<button label="Up" id="button[up]" image="goup"/>
<button label="Go to your home directory" id="button[home]" image="gohome"/>
<box class="selectPathContainer">
<vfs id="path" onclick="path=document.getElementById(form::name('path')); path.value=$path; path.form.submit();" class="selectPath"/>
<vfs-name id="path" class="selectPath"/>
</box>
<hidden id="path"/>
<button label="Create directory" id="button[createdir]" onclick="var dir = prompt(egw::lang('New directory')); if (!dir) return false; document.getElementById(form::name('path')).value+='/'+dir;" image="button_createdir" ro_image="createdir_disabled" class="createDir"/>
</hbox>
</row>