nextcloud/core/js/oc-dialogs.js

344 lines
9.5 KiB
JavaScript
Raw Normal View History

2012-02-29 02:02:02 +04:00
/**
* ownCloud
*
2013-05-16 20:19:28 +04:00
* @author Bartek Przybylski, Christopher Schäpers, Thomas Tanghus
2012-03-24 21:35:16 +04:00
* @copyright 2012 Bartek Przybylski bartek@alefzero.eu
2012-02-29 02:02:02 +04:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* this class to ease the usage of jquery dialogs
2012-02-29 02:02:02 +04:00
*/
2012-09-06 00:22:04 +04:00
var OCdialogs = {
2013-05-17 09:14:43 +04:00
// dialog button types
YES_NO_BUTTONS: 70,
OK_BUTTONS: 71,
// used to name each dialog
dialogs_counter: 0,
/**
* displays alert dialog
* @param text content of dialog
* @param title dialog title
* @param callback which will be triggered when user presses OK
* @param modal make the dialog modal
*/
alert:function(text, title, callback, modal) {
2013-05-17 09:14:43 +04:00
this.message(text, title, 'alert', OCdialogs.OK_BUTTON, callback, modal);
},
/**
* displays info dialog
* @param text content of dialog
* @param title dialog title
* @param callback which will be triggered when user presses OK
* @param modal make the dialog modal
*/
info:function(text, title, callback, modal) {
2013-05-17 09:14:43 +04:00
this.message(text, title, 'info', OCdialogs.OK_BUTTON, callback, modal);
},
/**
* displays confirmation dialog
* @param text content of dialog
* @param title dialog title
* @param callback which will be triggered when user presses YES or NO (true or false would be passed to callback respectively)
* @param modal make the dialog modal
*/
confirm:function(text, title, callback, modal) {
2013-05-17 09:14:43 +04:00
this.message(text, title, 'notice', OCdialogs.YES_NO_BUTTONS, callback, modal);
},
/**
* show a file picker to pick a file from
* @param title dialog title
* @param callback which will be triggered when user presses Choose
* @param multiselect whether it should be possible to select multiple files
* @param mimetype_filter mimetype to filter by
* @param modal make the dialog modal
*/
filepicker:function(title, callback, multiselect, mimetype_filter, modal) {
2013-05-17 06:54:08 +04:00
var self = this;
$.when(this._getFilePickerTemplate()).then(function($tmpl) {
2013-05-17 06:54:08 +04:00
var dialog_name = 'oc-dialog-filepicker-content';
var dialog_id = '#' + dialog_name;
2013-05-17 06:54:08 +04:00
if(self.$filePicker) {
self.$filePicker.ocdialog('close');
2013-05-17 06:54:08 +04:00
}
self.$filePicker = $tmpl.octemplate({
dialog_name: dialog_name,
title: title
}).data('path', '').data('multiselect', multiselect).data('mimetype', mimetype_filter);
if (modal === undefined) {
modal = false;
}
if (multiselect === undefined) {
multiselect = false;
}
if (mimetype_filter === undefined) {
mimetype_filter = '';
}
2013-05-17 06:54:08 +04:00
$('body').append(self.$filePicker);
2013-05-17 06:54:08 +04:00
self.$filePicker.ready(function() {
self.$filelist = self.$filePicker.find('.filelist');
self.$dirTree = self.$filePicker.find('.dirtree');
2013-05-17 10:42:15 +04:00
self.$dirTree.on('click', 'span:not(:last-child)', self, self._handleTreeListSelect);
2013-05-17 06:54:08 +04:00
self.$filelist.on('click', 'li', function(event) {
2013-05-17 10:42:15 +04:00
self._handlePickerClick(event, $(this));
});
2013-05-17 10:42:15 +04:00
self._fillFilePicker('');
});
// build buttons
var functionToCall = function() {
if (callback !== undefined) {
var datapath;
if (multiselect === true) {
datapath = [];
2013-05-17 06:54:08 +04:00
self.$filelist.find('.filepicker_element_selected .filename').each(function(index, element) {
datapath.push(self.$filePicker.data('path') + '/' + $(element).text());
});
} else {
2013-07-31 23:21:02 +04:00
datapath = self.$filePicker.data('path');
2013-05-17 06:54:08 +04:00
datapath += '/' + self.$filelist.find('.filepicker_element_selected .filename').text();
}
callback(datapath);
self.$filePicker.ocdialog('close');
}
};
var buttonlist = [{
text: t('core', 'Choose'),
2013-06-03 05:31:33 +04:00
click: functionToCall,
defaultButton: true
}];
self.$filePicker.ocdialog({
2013-05-17 09:14:43 +04:00
closeOnEscape: true,
width: (4/9)*$(document).width(),
height: 420,
modal: modal,
2013-05-17 06:54:08 +04:00
buttons: buttonlist,
close: function(event, ui) {
2013-06-03 05:19:35 +04:00
try {
$(this).ocdialog('destroy').remove();
} catch(e) {}
2013-05-17 06:54:08 +04:00
self.$filePicker = null;
}
});
})
.fail(function() {
alert(t('core', 'Error loading file picker template'));
});
},
/**
* Displays raw dialog
* You better use a wrapper instead ...
*/
message:function(content, title, dialog_type, buttons, callback, modal) {
$.when(this._getMessageTemplate()).then(function($tmpl) {
var dialog_name = 'oc-dialog-' + OCdialogs.dialogs_counter + '-content';
var dialog_id = '#' + dialog_name;
var $dlg = $tmpl.octemplate({
dialog_name: dialog_name,
title: title,
message: content,
type: dialog_type
});
if (modal === undefined) {
modal = false;
}
$('body').append($dlg);
var buttonlist = [];
switch (buttons) {
case OCdialogs.YES_NO_BUTTONS:
buttonlist = [{
text: t('core', 'Yes'),
click: function(){
if (callback !== undefined) {
callback(true);
}
$(dialog_id).ocdialog('close');
2013-06-03 05:31:33 +04:00
},
defaultButton: true
},
{
text: t('core', 'No'),
click: function(){
if (callback !== undefined) {
callback(false);
}
$(dialog_id).ocdialog('close');
}
}];
break;
case OCdialogs.OK_BUTTON:
2013-05-20 11:54:43 +04:00
var functionToCall = function() {
$(dialog_id).ocdialog('close');
if(callback !== undefined) {
callback();
}
2013-05-20 11:54:43 +04:00
};
buttonlist[0] = {
text: t('core', 'Ok'),
2013-06-03 05:31:33 +04:00
click: functionToCall,
defaultButton: true
};
break;
2013-07-31 23:21:02 +04:00
}
$(dialog_id).ocdialog({
2013-05-17 09:14:43 +04:00
closeOnEscape: true,
modal: modal,
buttons: buttonlist
});
OCdialogs.dialogs_counter++;
})
.fail(function() {
alert(t('core', 'Error loading file picker template'));
});
},
2013-05-17 10:42:15 +04:00
_getFilePickerTemplate: function() {
var defer = $.Deferred();
if(!this.$filePickerTemplate) {
var self = this;
$.get(OC.filePath('core', 'templates', 'filepicker.html'), function(tmpl) {
self.$filePickerTemplate = $(tmpl);
self.$listTmpl = self.$filePickerTemplate.find('.filelist li:first-child').detach();
defer.resolve(self.$filePickerTemplate);
})
.fail(function() {
defer.reject();
});
} else {
defer.resolve(this.$filePickerTemplate);
}
return defer.promise();
},
_getMessageTemplate: function() {
var defer = $.Deferred();
if(!this.$messageTemplate) {
var self = this;
$.get(OC.filePath('core', 'templates', 'message.html'), function(tmpl) {
self.$messageTemplate = $(tmpl);
defer.resolve(self.$messageTemplate);
})
.fail(function() {
defer.reject();
});
} else {
defer.resolve(this.$messageTemplate);
}
return defer.promise();
},
_getFileList: function(dir, mimeType) {
return $.getJSON(
OC.filePath('files', 'ajax', 'rawlist.php'),
{dir: dir, mimetype: mimeType}
);
},
_determineValue: function(element) {
if ( $(element).attr('type') === 'checkbox' ) {
return element.checked;
} else {
return $(element).val();
}
},
/**
* fills the filepicker with files
*/
2013-05-17 10:42:15 +04:00
_fillFilePicker:function(dir) {
2013-04-06 16:42:56 +04:00
var dirs = [];
var others = [];
var self = this;
2013-05-17 06:54:08 +04:00
this.$filelist.empty().addClass('loading');
this.$filePicker.data('path', dir);
$.when(this._getFileList(dir, this.$filePicker.data('mimetype'))).then(function(response) {
$.each(response.data, function(index, file) {
if (file.type === 'dir') {
dirs.push(file);
} else {
others.push(file);
}
});
2013-05-17 10:42:15 +04:00
self._fillSlug();
2013-05-17 06:54:08 +04:00
var sorted = dirs.concat(others);
2013-05-17 06:54:08 +04:00
$.each(sorted, function(idx, entry) {
2013-07-31 23:21:02 +04:00
var $li = self.$listTmpl.octemplate({
2013-05-17 06:54:08 +04:00
type: entry.type,
dir: dir,
filename: entry.name,
date: OC.mtime2date(entry.mtime)
});
2013-05-20 11:44:31 +04:00
$li.find('img').attr('src', entry.mimetype_icon);
2013-05-17 06:54:08 +04:00
self.$filelist.append($li);
});
2013-05-17 06:54:08 +04:00
self.$filelist.removeClass('loading');
});
},
/**
* fills the tree list with directories
*/
2013-05-17 10:42:15 +04:00
_fillSlug: function() {
2013-05-17 06:54:08 +04:00
this.$dirTree.empty();
2013-07-31 23:21:02 +04:00
var self = this;
2013-05-17 06:54:08 +04:00
var path = this.$filePicker.data('path');
var $template = $('<span data-dir="{dir}">{name}</span>');
2013-05-17 09:14:43 +04:00
if(path) {
var paths = path.split('/');
$.each(paths, function(index, dir) {
2013-07-31 23:21:02 +04:00
dir = paths.pop();
2013-05-17 09:14:43 +04:00
if(dir === '') {
return false;
}
self.$dirTree.prepend($template.octemplate({
dir: paths.join('/') + '/' + dir,
name: dir
}));
});
}
$template.octemplate({
2013-05-17 06:54:08 +04:00
dir: '',
name: '&nbsp;&nbsp;&nbsp;&nbsp;' // Ugly but works ;)
}, {escapeFunction: null}).addClass('home svg').prependTo(this.$dirTree);
},
/**
* handle selection made in the tree list
*/
2013-05-17 10:42:15 +04:00
_handleTreeListSelect:function(event) {
2013-05-17 06:54:08 +04:00
var self = event.data;
var dir = $(event.target).data('dir');
2013-05-17 10:42:15 +04:00
self._fillFilePicker(dir);
},
/**
* handle clicks made in the filepicker
*/
2013-05-17 10:42:15 +04:00
_handlePickerClick:function(event, $element) {
2013-05-17 06:54:08 +04:00
if ($element.data('type') === 'file') {
if (this.$filePicker.data('multiselect') !== true || !event.ctrlKey) {
this.$filelist.find('.filepicker_element_selected').removeClass('filepicker_element_selected');
}
2013-05-17 06:54:08 +04:00
$element.toggleClass('filepicker_element_selected');
return;
2013-05-17 06:54:08 +04:00
} else if ( $element.data('type') === 'dir' ) {
2013-07-31 23:21:02 +04:00
this._fillFilePicker(this.$filePicker.data('path') + '/' + $element.data('entryname'));
}
}
2012-02-29 02:02:02 +04:00
};