Merge pull request #8200 from owncloud/js-cleanup-dialogs-master

javascript code cleanup + fixing syntax errors
This commit is contained in:
Thomas Müller 2014-04-15 14:20:09 +02:00
commit 2eeab60378
1 changed files with 168 additions and 175 deletions

View File

@ -19,7 +19,7 @@
* *
*/ */
/* global OC, t */ /* global OC, t, alert */
/** /**
* this class to ease the usage of jquery dialogs * this class to ease the usage of jquery dialogs
@ -29,7 +29,7 @@ var OCdialogs = {
YES_NO_BUTTONS: 70, YES_NO_BUTTONS: 70,
OK_BUTTONS: 71, OK_BUTTONS: 71,
// used to name each dialog // used to name each dialog
dialogs_counter: 0, dialogsCounter: 0,
/** /**
* displays alert dialog * displays alert dialog
* @param text content of dialog * @param text content of dialog
@ -54,7 +54,8 @@ var OCdialogs = {
* displays confirmation dialog * displays confirmation dialog
* @param text content of dialog * @param text content of dialog
* @param title dialog title * @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 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 * @param modal make the dialog modal
*/ */
confirm:function(text, title, callback, modal) { confirm:function(text, title, callback, modal) {
@ -65,21 +66,20 @@ var OCdialogs = {
* @param title dialog title * @param title dialog title
* @param callback which will be triggered when user presses Choose * @param callback which will be triggered when user presses Choose
* @param multiselect whether it should be possible to select multiple files * @param multiselect whether it should be possible to select multiple files
* @param mimetype_filter mimetype to filter by * @param mimetypeFilter mimetype to filter by
* @param modal make the dialog modal * @param modal make the dialog modal
*/ */
filepicker:function(title, callback, multiselect, mimetype_filter, modal) { filepicker:function(title, callback, multiselect, mimetypeFilter, modal) {
var self = this; var self = this;
$.when(this._getFilePickerTemplate()).then(function($tmpl) { $.when(this._getFilePickerTemplate()).then(function($tmpl) {
var dialog_name = 'oc-dialog-filepicker-content'; var dialogName = 'oc-dialog-filepicker-content';
var dialog_id = '#' + dialog_name;
if(self.$filePicker) { if(self.$filePicker) {
self.$filePicker.ocdialog('close'); self.$filePicker.ocdialog('close');
} }
self.$filePicker = $tmpl.octemplate({ self.$filePicker = $tmpl.octemplate({
dialog_name: dialog_name, dialog_name: dialogName,
title: title title: title
}).data('path', '').data('multiselect', multiselect).data('mimetype', mimetype_filter); }).data('path', '').data('multiselect', multiselect).data('mimetype', mimetypeFilter);
if (modal === undefined) { if (modal === undefined) {
modal = false; modal = false;
@ -87,8 +87,8 @@ var OCdialogs = {
if (multiselect === undefined) { if (multiselect === undefined) {
multiselect = false; multiselect = false;
} }
if (mimetype_filter === undefined) { if (mimetypeFilter === undefined) {
mimetype_filter = ''; mimetypeFilter = '';
} }
$('body').append(self.$filePicker); $('body').append(self.$filePicker);
@ -133,7 +133,7 @@ var OCdialogs = {
height: 420, height: 420,
modal: modal, modal: modal,
buttons: buttonlist, buttons: buttonlist,
close: function(event, ui) { close: function() {
try { try {
$(this).ocdialog('destroy').remove(); $(this).ocdialog('destroy').remove();
} catch(e) {} } catch(e) {}
@ -156,15 +156,15 @@ var OCdialogs = {
* Displays raw dialog * Displays raw dialog
* You better use a wrapper instead ... * You better use a wrapper instead ...
*/ */
message:function(content, title, dialog_type, buttons, callback, modal) { message:function(content, title, dialogType, buttons, callback, modal) {
$.when(this._getMessageTemplate()).then(function($tmpl) { $.when(this._getMessageTemplate()).then(function($tmpl) {
var dialog_name = 'oc-dialog-' + OCdialogs.dialogs_counter + '-content'; var dialogName = 'oc-dialog-' + OCdialogs.dialogsCounter + '-content';
var dialog_id = '#' + dialog_name; var dialogId = '#' + dialogName;
var $dlg = $tmpl.octemplate({ var $dlg = $tmpl.octemplate({
dialog_name: dialog_name, dialog_name: dialogName,
title: title, title: title,
message: content, message: content,
type: dialog_type type: dialogType
}); });
if (modal === undefined) { if (modal === undefined) {
modal = false; modal = false;
@ -172,48 +172,48 @@ var OCdialogs = {
$('body').append($dlg); $('body').append($dlg);
var buttonlist = []; var buttonlist = [];
switch (buttons) { switch (buttons) {
case OCdialogs.YES_NO_BUTTONS: case OCdialogs.YES_NO_BUTTONS:
buttonlist = [{ buttonlist = [{
text: t('core', 'Yes'), text: t('core', 'Yes'),
click: function(){ click: function(){
if (callback !== undefined) { if (callback !== undefined) {
callback(true); callback(true);
} }
$(dialog_id).ocdialog('close'); $(dialogId).ocdialog('close');
},
defaultButton: true
}, },
{ defaultButton: true
text: t('core', 'No'), },
click: function(){ {
if (callback !== undefined) { text: t('core', 'No'),
callback(false); click: function(){
} if (callback !== undefined) {
$(dialog_id).ocdialog('close'); callback(false);
} }
}]; $(dialogId).ocdialog('close');
}
}];
break; break;
case OCdialogs.OK_BUTTON: case OCdialogs.OK_BUTTON:
var functionToCall = function() { var functionToCall = function() {
$(dialog_id).ocdialog('close'); $(dialogId).ocdialog('close');
if(callback !== undefined) { if(callback !== undefined) {
callback(); callback();
} }
}; };
buttonlist[0] = { buttonlist[0] = {
text: t('core', 'Ok'), text: t('core', 'Ok'),
click: functionToCall, click: functionToCall,
defaultButton: true defaultButton: true
}; };
break; break;
} }
$(dialog_id).ocdialog({ $(dialogId).ocdialog({
closeOnEscape: true, closeOnEscape: true,
modal: modal, modal: modal,
buttons: buttonlist buttons: buttonlist
}); });
OCdialogs.dialogs_counter++; OCdialogs.dialogsCounter++;
}) })
.fail(function(status, error) { .fail(function(status, error) {
// If the method is called while navigating away from // If the method is called while navigating away from
@ -251,7 +251,7 @@ var OCdialogs = {
image.onload = function () { image.onload = function () {
var url = crop(image); var url = crop(image);
deferred.resolve(url); deferred.resolve(url);
} };
}; };
reader.readAsArrayBuffer(file); reader.readAsArrayBuffer(file);
} else { } else {
@ -339,132 +339,133 @@ var OCdialogs = {
//if (selection.defaultAction) { //if (selection.defaultAction) {
// controller[selection.defaultAction](data); // controller[selection.defaultAction](data);
//} else { //} else {
var dialog_name = 'oc-dialog-fileexists-content'; var dialogName = 'oc-dialog-fileexists-content';
var dialog_id = '#' + dialog_name; var dialogId = '#' + dialogName;
if (this._fileexistsshown) { if (this._fileexistsshown) {
// add conflict // add conflict
var conflicts = $(dialog_id+ ' .conflicts'); var conflicts = $(dialogId+ ' .conflicts');
addConflict(conflicts, original, replacement);
var count = $(dialogId+ ' .conflict').length;
var title = n('core',
'{count} file conflict',
'{count} file conflicts',
count,
{count:count}
);
$(dialogId).parent().children('.oc-dialog-title').text(title);
//recalculate dimensions
$(window).trigger('resize');
} else {
//create dialog
this._fileexistsshown = true;
$.when(this._getFileExistsTemplate()).then(function($tmpl) {
var title = t('core','One file conflict');
var $dlg = $tmpl.octemplate({
dialog_name: dialogName,
title: title,
type: 'fileexists',
allnewfiles: t('core','New Files'),
allexistingfiles: t('core','Already existing files'),
why: t('core','Which files do you want to keep?'),
what: t('core','If you select both versions, the copied file will have a number added to its name.')
});
$('body').append($dlg);
var conflicts = $($dlg).find('.conflicts');
addConflict(conflicts, original, replacement); addConflict(conflicts, original, replacement);
var count = $(dialog_id+ ' .conflict').length; var buttonlist = [{
var title = n('core', text: t('core', 'Cancel'),
'{count} file conflict', classes: 'cancel',
'{count} file conflicts', click: function(){
count, if ( typeof controller.onCancel !== 'undefined') {
{count:count} controller.onCancel(data);
);
$(dialog_id).parent().children('.oc-dialog-title').text(title);
//recalculate dimensions
$(window).trigger('resize');
} else {
//create dialog
this._fileexistsshown = true;
$.when(this._getFileExistsTemplate()).then(function($tmpl) {
var title = t('core','One file conflict');
var $dlg = $tmpl.octemplate({
dialog_name: dialog_name,
title: title,
type: 'fileexists',
allnewfiles: t('core','New Files'),
allexistingfiles: t('core','Already existing files'),
why: t('core','Which files do you want to keep?'),
what: t('core','If you select both versions, the copied file will have a number added to its name.')
});
$('body').append($dlg);
var conflicts = $($dlg).find('.conflicts');
addConflict(conflicts, original, replacement);
buttonlist = [{
text: t('core', 'Cancel'),
classes: 'cancel',
click: function(){
if ( typeof controller.onCancel !== 'undefined') {
controller.onCancel(data);
}
$(dialog_id).ocdialog('close');
} }
}, $(dialogId).ocdialog('close');
{ }
text: t('core', 'Continue'), },
classes: 'continue', {
click: function(){ text: t('core', 'Continue'),
if ( typeof controller.onContinue !== 'undefined') { classes: 'continue',
controller.onContinue($(dialog_id + ' .conflict')); click: function(){
} if ( typeof controller.onContinue !== 'undefined') {
$(dialog_id).ocdialog('close'); controller.onContinue($(dialogId + ' .conflict'));
} }
}]; $(dialogId).ocdialog('close');
}
}];
$(dialog_id).ocdialog({ $(dialogId).ocdialog({
width: 500, width: 500,
closeOnEscape: true, closeOnEscape: true,
modal: true, modal: true,
buttons: buttonlist, buttons: buttonlist,
closeButton: null, closeButton: null,
close: function(event, ui) { close: function() {
self._fileexistsshown = false; self._fileexistsshown = false;
$(this).ocdialog('destroy').remove(); $(this).ocdialog('destroy').remove();
} }
});
$(dialog_id).css('height','auto');
//add checkbox toggling actions
$(dialog_id).find('.allnewfiles').on('click', function() {
var checkboxes = $(dialog_id).find('.conflict .replacement input[type="checkbox"]');
checkboxes.prop('checked', $(this).prop('checked'));
});
$(dialog_id).find('.allexistingfiles').on('click', function() {
var checkboxes = $(dialog_id).find('.conflict .original input[type="checkbox"]');
checkboxes.prop('checked', $(this).prop('checked'));
});
$(dialog_id).find('.conflicts').on('click', '.replacement,.original', function() {
var checkbox = $(this).find('input[type="checkbox"]');
checkbox.prop('checked', !checkbox.prop('checked'));
});
$(dialog_id).find('.conflicts').on('click', 'input[type="checkbox"]', function() {
var checkbox = $(this);
checkbox.prop('checked', !checkbox.prop('checked'));
});
//update counters
$(dialog_id).on('click', '.replacement,.allnewfiles', function() {
var count = $(dialog_id).find('.conflict .replacement input[type="checkbox"]:checked').length;
if (count === $(dialog_id+ ' .conflict').length) {
$(dialog_id).find('.allnewfiles').prop('checked', true);
$(dialog_id).find('.allnewfiles + .count').text(t('core','(all selected)'));
} else if (count > 0) {
$(dialog_id).find('.allnewfiles').prop('checked', false);
$(dialog_id).find('.allnewfiles + .count').text(t('core','({count} selected)',{count:count}));
} else {
$(dialog_id).find('.allnewfiles').prop('checked', false);
$(dialog_id).find('.allnewfiles + .count').text('');
}
});
$(dialog_id).on('click', '.original,.allexistingfiles', function(){
var count = $(dialog_id).find('.conflict .original input[type="checkbox"]:checked').length;
if (count === $(dialog_id+ ' .conflict').length) {
$(dialog_id).find('.allexistingfiles').prop('checked', true);
$(dialog_id).find('.allexistingfiles + .count').text(t('core','(all selected)'));
} else if (count > 0) {
$(dialog_id).find('.allexistingfiles').prop('checked', false);
$(dialog_id).find('.allexistingfiles + .count').text(t('core','({count} selected)',{count:count}));
} else {
$(dialog_id).find('.allexistingfiles').prop('checked', false);
$(dialog_id).find('.allexistingfiles + .count').text('');
}
});
})
.fail(function() {
alert(t('core', 'Error loading file exists template'));
}); });
}
$(dialogId).css('height','auto');
//add checkbox toggling actions
$(dialogId).find('.allnewfiles').on('click', function() {
var checkboxes = $(dialogId).find('.conflict .replacement input[type="checkbox"]');
checkboxes.prop('checked', $(this).prop('checked'));
});
$(dialogId).find('.allexistingfiles').on('click', function() {
var checkboxes = $(dialogId).find('.conflict .original input[type="checkbox"]');
checkboxes.prop('checked', $(this).prop('checked'));
});
$(dialogId).find('.conflicts').on('click', '.replacement,.original', function() {
var checkbox = $(this).find('input[type="checkbox"]');
checkbox.prop('checked', !checkbox.prop('checked'));
});
$(dialogId).find('.conflicts').on('click', 'input[type="checkbox"]', function() {
var checkbox = $(this);
checkbox.prop('checked', !checkbox.prop('checked'));
});
//update counters
$(dialogId).on('click', '.replacement,.allnewfiles', function() {
var count = $(dialogId).find('.conflict .replacement input[type="checkbox"]:checked').length;
if (count === $(dialogId+ ' .conflict').length) {
$(dialogId).find('.allnewfiles').prop('checked', true);
$(dialogId).find('.allnewfiles + .count').text(t('core','(all selected)'));
} else if (count > 0) {
$(dialogId).find('.allnewfiles').prop('checked', false);
$(dialogId).find('.allnewfiles + .count').text(t('core','({count} selected)',{count:count}));
} else {
$(dialogId).find('.allnewfiles').prop('checked', false);
$(dialogId).find('.allnewfiles + .count').text('');
}
});
$(dialogId).on('click', '.original,.allexistingfiles', function(){
var count = $(dialogId).find('.conflict .original input[type="checkbox"]:checked').length;
if (count === $(dialogId+ ' .conflict').length) {
$(dialogId).find('.allexistingfiles').prop('checked', true);
$(dialogId).find('.allexistingfiles + .count').text(t('core','(all selected)'));
} else if (count > 0) {
$(dialogId).find('.allexistingfiles').prop('checked', false);
$(dialogId).find('.allexistingfiles + .count')
.text(t('core','({count} selected)',{count:count}));
} else {
$(dialogId).find('.allexistingfiles').prop('checked', false);
$(dialogId).find('.allexistingfiles + .count').text('');
}
});
})
.fail(function() {
alert(t('core', 'Error loading file exists template'));
});
}
//} //}
}, },
_getFilePickerTemplate: function() { _getFilePickerTemplate: function() {
@ -529,13 +530,6 @@ var OCdialogs = {
} }
); );
}, },
_determineValue: function(element) {
if ( $(element).attr('type') === 'checkbox' ) {
return element.checked;
} else {
return $(element).val();
}
},
/** /**
* fills the filepicker with files * fills the filepicker with files
@ -627,7 +621,6 @@ var OCdialogs = {
this.$filelist.find('.filepicker_element_selected').removeClass('filepicker_element_selected'); this.$filelist.find('.filepicker_element_selected').removeClass('filepicker_element_selected');
} }
$element.toggleClass('filepicker_element_selected'); $element.toggleClass('filepicker_element_selected');
return;
} else if ( $element.data('type') === 'dir' ) { } else if ( $element.data('type') === 'dir' ) {
this._fillFilePicker(this.$filePicker.data('path') + '/' + $element.data('entryname')); this._fillFilePicker(this.$filePicker.data('path') + '/' + $element.data('entryname'));
} }