Dialogs: Clean indentation. Add some missing brackets.

This commit is contained in:
Thomas Tanghus 2012-05-05 13:48:12 +02:00
parent eda42cece9
commit 2580d8540d
1 changed files with 239 additions and 220 deletions

View File

@ -20,226 +20,245 @@
*/ */
/** /**
* this class ease usage of jquery dialogs * this class to ease the usage of jquery dialogs
*/ */
OCdialogs = { OCdialogs = {
/** /**
* displays alert dialog * displays alert 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 press OK * @param callback which will be triggered when user press OK
*/ */
alert:function(text, title, callback, modal) { alert:function(text, title, callback, modal) {
var content = '<p><span class="ui-icon ui-icon-alert"></span>'+text+'</p>'; var content = '<p><span class="ui-icon ui-icon-alert"></span>'+text+'</p>';
OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.OK_BUTTON, callback, modal); OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.OK_BUTTON, callback, modal);
}, },
/** /**
* displays info dialog * displays info 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 press OK * @param callback which will be triggered when user press OK
*/ */
info:function(text, title, callback, modal) { info:function(text, title, callback, modal) {
var content = '<p><span class="ui-icon ui-icon-info"></span>'+text+'</p>'; var content = '<p><span class="ui-icon ui-icon-info"></span>'+text+'</p>';
OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.OK_BUTTON, callback, modal); OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.OK_BUTTON, callback, modal);
}, },
/** /**
* 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 press YES or NO (true or false would be passed to callback respectively) * @param callback which will be triggered when user press YES or NO (true or false would be passed to callback respectively)
*/ */
confirm:function(text, title, callback, modal) { confirm:function(text, title, callback, modal) {
var content = '<p><span class="ui-icon ui-icon-notice"></span>'+text+'</p>'; var content = '<p><span class="ui-icon ui-icon-notice"></span>'+text+'</p>';
OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.YES_NO_BUTTONS, callback, modal); OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.YES_NO_BUTTONS, callback, modal);
}, },
/** /**
* prompt for user input * prompt for user input
* @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 press OK (input text will be passed to callback) * @param callback which will be triggered when user press OK (input text will be passed to callback)
*/ */
prompt:function(text, title, default_value, callback, modal) { prompt:function(text, title, default_value, callback, modal) {
var content = '<p><span class="ui-icon ui-icon-pencil"></span>'+text+':<br/><input type="text" id="oc-dialog-prompt-input" value="'+default_value+'" style="width:90%"></p>'; var content = '<p><span class="ui-icon ui-icon-pencil"></span>'+text+':<br/><input type="text" id="oc-dialog-prompt-input" value="'+default_value+'" style="width:90%"></p>';
OCdialogs.message(content, title, OCdialogs.PROMPT_DIALOG, OCdialogs.OK_CANCEL_BUTTONS, callback, modal); OCdialogs.message(content, title, OCdialogs.PROMPT_DIALOG, OCdialogs.OK_CANCEL_BUTTONS, callback, modal);
}, },
/** /**
* prompt user for input with custom form * prompt user for input with custom form
* fields should be passed in following format: [{text:'prompt text', name:'return name', type:'input type', value: 'dafault value'},...] * fields should be passed in following format: [{text:'prompt text', name:'return name', type:'input type', value: 'dafault value'},...]
* @param fields to display * @param fields to display
* @param title dialog title * @param title dialog title
* @param callback which will be triggered when user press OK (user answers will be passed to callback in following format: [{name:'return name', value: 'user value'},...]) * @param callback which will be triggered when user press OK (user answers will be passed to callback in following format: [{name:'return name', value: 'user value'},...])
*/ */
form:function(fields, title, callback, modal) { form:function(fields, title, callback, modal) {
var content = '<table>'; var content = '<table>';
for (var a in fields) { for (var a in fields) {
content += '<tr><td>'+fields[a].text+'</td><td>'; content += '<tr><td>'+fields[a].text+'</td><td>';
var type=fields[a].type; var type=fields[a].type;
if (type == 'text' || type == 'checkbox' || type == 'password') { if (type == 'text' || type == 'checkbox' || type == 'password') {
content += '<input type="'+type+'" name="'+fields[a].name+'"'; content += '<input type="'+type+'" name="'+fields[a].name+'"';
if (type == 'checkbox') { if (type == 'checkbox') {
if (fields[a].value != undefined && fields[a].value == true) { if (fields[a].value != undefined && fields[a].value == true) {
content += ' checked="checked">'; content += ' checked="checked">';
} else content += '>'; } else {
} else if (type == 'text' || type == 'password' && fields[a].value) content += '>';
content += ' value="'+fields[a].value+'">'; }
} else if (type == 'select') { } else if (type == 'text' || type == 'password' && fields[a].value) {
content += '<select name="'+fields[a].name+'"'; content += ' value="'+fields[a].value+'">';
if (fields[a].value != undefined) }
content += ' value="'+fields[a].value+'"'; } else if (type == 'select') {
content += '>'; content += '<select name="'+fields[a].name+'"';
for (var o in fields[a].options) if (fields[a].value != undefined) {
content += '<option value="'+fields[a].options[o].value+'">'+fields[a].options[o].text+'</option>'; content += ' value="'+fields[a].value+'"';
content += '</select>'; }
} content += '>';
content += '</td></tr>'; for (var o in fields[a].options) {
} content += '<option value="'+fields[a].options[o].value+'">'+fields[a].options[o].text+'</option>';
content += '</table>'; }
OCdialogs.message(content, title, OCdialogs.FORM_DIALOG, OCdialogs.OK_CANCEL_BUTTONS, callback, modal); content += '</select>';
}, }
filepicker:function(title, callback, multiselect, mimetype_filter, modal) { content += '</td></tr>';
var c_name = 'oc-dialog-'+OCdialogs.dialogs_counter+'-content'; }
var c_id = '#'+c_name; content += '</table>';
var d = '<div id="'+c_name+'" title="'+title+'"><select id="dirtree"><option value="0">'+OC.currentUser+'</option></select><div id="filelist"></div><div class="filepicker_loader"><img src="'+OC.filePath('gallery','img','loading.gif')+'"></div></div>'; OCdialogs.message(content, title, OCdialogs.FORM_DIALOG, OCdialogs.OK_CANCEL_BUTTONS, callback, modal);
if (!modal) modal = false; },
if (!multiselect) multiselect = false; filepicker:function(title, callback, multiselect, mimetype_filter, modal) {
$('body').append(d); var c_name = 'oc-dialog-'+OCdialogs.dialogs_counter+'-content';
$(c_id + ' #dirtree').focus(function() { var t = $(this); t.data('oldval', t.val())}) var c_id = '#'+c_name;
.change({dcid: c_id}, OC.dialogs.handleTreeListSelect); var d = '<div id="'+c_name+'" title="'+title+'"><select id="dirtree"><option value="0">'+OC.currentUser+'</option></select><div id="filelist"></div><div class="filepicker_loader"><img src="'+OC.filePath('gallery','img','loading.gif')+'"></div></div>';
$(c_id).ready(function(){ if (!modal) modal = false; // Huh..?
$.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {mimetype: mimetype_filter} ,function(r){OC.dialogs.fillFilePicker(r, c_id, callback)}); if (!multiselect) multiselect = false;
}).data('multiselect', multiselect).data('mimetype',mimetype_filter); $('body').append(d);
// build buttons $(c_id + ' #dirtree').focus(function() {
var b = [ var t = $(this);
{text: t('dialogs', 'Choose'), click: function(){ t.data('oldval', t.val())
if (callback != undefined) { }).change({dcid: c_id}, OC.dialogs.handleTreeListSelect);
var p; $(c_id).ready(function(){
if ($(c_id).data('multiselect') == true) { $.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {mimetype: mimetype_filter} ,function(r) {
p = []; OC.dialogs.fillFilePicker(r, c_id, callback)
$(c_id+' .filepicker_element_selected #filename').each(function(i, elem) { });
p.push(($(c_id).data('path')?$(c_id).data('path'):'')+'/'+$(elem).text()); }).data('multiselect', multiselect).data('mimetype',mimetype_filter);
}); // build buttons
} else { var b = [{
var p = $(c_id).data('path'); text: t('dialogs', 'Choose'),
if (p == undefined) p = ''; click: function(){
p = p+'/'+$(c_id+' .filepicker_element_selected #filename').text() if (callback != undefined) {
} var p;
callback(p); if ($(c_id).data('multiselect') == true) {
$(c_id).dialog('close'); p = [];
} $(c_id+' .filepicker_element_selected #filename').each(function(i, elem) {
} p.push(($(c_id).data('path')?$(c_id).data('path'):'')+'/'+$(elem).text());
}, });
{text: t('dialogs', 'Cancel'), click: function(){$(c_id).dialog('close'); }} } else {
]; var p = $(c_id).data('path');
$(c_id).dialog({width: ((4*$('body').width())/9), height: 400, modal: modal, buttons: b}); if (p == undefined) p = '';
OCdialogs.dialogs_counter++; p = p+'/'+$(c_id+' .filepicker_element_selected #filename').text()
}, }
// guts, dont use, dont touch callback(p);
message:function(content, title, dialog_type, buttons, callback, modal) { $(c_id).dialog('close');
var c_name = 'oc-dialog-'+OCdialogs.dialogs_counter+'-content'; }
var c_id = '#'+c_name; }
var d = '<div id="'+c_name+'" title="'+title+'">'+content+'</div>'; },
if (modal == undefined) modal = false; {
$('body').append(d); text: t('dialogs', 'Cancel'),
var b = []; click: function(){$(c_id).dialog('close'); }}
switch (buttons) { ];
case OCdialogs.YES_NO_BUTTONS: $(c_id).dialog({width: ((4*$('body').width())/9), height: 400, modal: modal, buttons: b});
b[1] = {text: t('dialogs', 'No'), click: function(){ if (callback != undefined) callback(false); $(c_id).dialog('close'); }}; OCdialogs.dialogs_counter++;
b[0] = {text: t('dialogs', 'Yes'), click: function(){ if (callback != undefined) callback(true); $(c_id).dialog('close');}}; },
break; // guts, dont use, dont touch
case OCdialogs.OK_CANCEL_BUTTONS: message:function(content, title, dialog_type, buttons, callback, modal) {
b[1] = {text: t('dialogs', 'Cancel'), click: function(){$(c_id).dialog('close'); }}; var c_name = 'oc-dialog-'+OCdialogs.dialogs_counter+'-content';
case OCdialogs.OK_BUTTON: // fallthrough var c_id = '#'+c_name;
var f; var d = '<div id="'+c_name+'" title="'+title+'">'+content+'</div>';
switch(dialog_type) { if (modal == undefined) modal = false;
case OCdialogs.ALERT_DIALOG: $('body').append(d);
f = function(){$(c_id).dialog('close'); if(callback) callback();}; var b = [];
break; switch (buttons) {
case OCdialogs.PROMPT_DIALOG: case OCdialogs.YES_NO_BUTTONS:
f = function(){OCdialogs.prompt_ok_handler(callback, c_id)}; b[1] = {text: t('dialogs', 'No'), click: function(){ if (callback != undefined) callback(false); $(c_id).dialog('close'); }};
break; b[0] = {text: t('dialogs', 'Yes'), click: function(){ if (callback != undefined) callback(true); $(c_id).dialog('close');}};
case OCdialogs.FORM_DIALOG: break;
f = function(){OCdialogs.form_ok_handler(callback, c_id)}; case OCdialogs.OK_CANCEL_BUTTONS:
break; b[1] = {text: t('dialogs', 'Cancel'), click: function(){$(c_id).dialog('close'); }};
} case OCdialogs.OK_BUTTON: // fallthrough
b[0] = {text: t('dialogs', 'Ok'), click: f}; var f;
break; switch(dialog_type) {
} case OCdialogs.ALERT_DIALOG:
var possible_height = ($('tr', d).size()+1)*30; f = function(){$(c_id).dialog('close'); if(callback) callback();};
$(c_id).dialog({width: 4*$(document).width()/9, height: possible_height + 120, modal: modal, buttons: b}); break;
OCdialogs.dialogs_counter++; case OCdialogs.PROMPT_DIALOG:
}, f = function(){OCdialogs.prompt_ok_handler(callback, c_id)};
// dialogs buttons types break;
YES_NO_BUTTONS: 70, case OCdialogs.FORM_DIALOG:
OK_BUTTONS: 71, f = function(){OCdialogs.form_ok_handler(callback, c_id)};
OK_CANCEL_BUTTONS: 72, break;
// dialogs types }
ALERT_DIALOG: 80, b[0] = {text: t('dialogs', 'Ok'), click: f};
INFO_DIALOG: 81, break;
PROMPT_DIALOG: 82, }
FORM_DIALOG: 83, var possible_height = ($('tr', d).size()+1)*30;
dialogs_counter: 0, $(c_id).dialog({width: 4*$(document).width()/9, height: possible_height + 120, modal: modal, buttons: b});
determineValue: function(element) { OCdialogs.dialogs_counter++;
switch ($(element).attr('type')) { },
case 'checkbox': return element.checked; // dialogs buttons types
} YES_NO_BUTTONS: 70,
return $(element).val(); OK_BUTTONS: 71,
}, OK_CANCEL_BUTTONS: 72,
prompt_ok_handler: function(callback, c_id) { $(c_id).dialog('close'); if (callback != undefined) callback($(c_id + " input#oc-dialog-prompt-input").val()); }, // dialogs types
form_ok_handler: function(callback, c_id) { ALERT_DIALOG: 80,
if (callback != undefined) { INFO_DIALOG: 81,
var r = []; PROMPT_DIALOG: 82,
var c = 0; FORM_DIALOG: 83,
$(c_id + ' input, '+c_id+' select').each(function(i, elem) { dialogs_counter: 0,
r[c] = {name: $(elem).attr('name'), value: OCdialogs.determineValue(elem)}; determineValue: function(element) {
c++; switch ($(element).attr('type')) {
}); case 'checkbox': return element.checked;
$(c_id).dialog('close'); }
callback(r); return $(element).val();
} else { },
$(c_id).dialog('close'); prompt_ok_handler: function(callback, c_id) { $(c_id).dialog('close'); if (callback != undefined) callback($(c_id + " input#oc-dialog-prompt-input").val()); },
} form_ok_handler: function(callback, c_id) {
}, if (callback != undefined) {
fillFilePicker:function(r, dialog_content_id) { var r = [];
var entry_template = '<div onclick="javascript:OC.dialogs.handlePickerClick(this, \'*ENTRYNAME*\',\''+dialog_content_id+'\')" data="*ENTRYTYPE*"><img src="*MIMETYPEICON*" style="margin-right:1em;"><span id="filename">*NAME*</span><div style="float:right;margin-right:1em;">*LASTMODDATE*</div></div>'; var c = 0;
var names = ''; $(c_id + ' input, '+c_id+' select').each(function(i, elem) {
for (var a in r.data) { r[c] = {name: $(elem).attr('name'), value: OCdialogs.determineValue(elem)};
names += entry_template.replace('*LASTMODDATE*', OC.mtime2date(r.data[a].mtime)).replace('*NAME*', r.data[a].name).replace('*MIMETYPEICON*', r.data[a].mimetype_icon).replace('*ENTRYNAME*', r.data[a].name).replace('*ENTRYTYPE*', r.data[a].type); c++;
} });
$(dialog_content_id + ' #filelist').html(names); $(c_id).dialog('close');
$(dialog_content_id + ' .filepicker_loader').css('visibility', 'hidden'); callback(r);
}, } else {
handleTreeListSelect:function(event) { $(c_id).dialog('close');
var newval = parseInt($(this).val()); }
var oldval = parseInt($(this).data('oldval')); },
while (newval != oldval && oldval > 0) { fillFilePicker:function(r, dialog_content_id) {
$('option:last', this).remove(); var entry_template = '<div onclick="javascript:OC.dialogs.handlePickerClick(this, \'*ENTRYNAME*\',\''+dialog_content_id+'\')" data="*ENTRYTYPE*"><img src="*MIMETYPEICON*" style="margin-right:1em;"><span id="filename">*NAME*</span><div style="float:right;margin-right:1em;">*LASTMODDATE*</div></div>';
$('option:last', this).attr('selected','selected'); var names = '';
oldval--; for (var a in r.data) {
} names += entry_template.replace('*LASTMODDATE*', OC.mtime2date(r.data[a].mtime)).replace('*NAME*', r.data[a].name).replace('*MIMETYPEICON*', r.data[a].mimetype_icon).replace('*ENTRYNAME*', r.data[a].name).replace('*ENTRYTYPE*', r.data[a].type);
var skip_first = true; }
var path = ''; $(dialog_content_id + ' #filelist').html(names);
$(this).children().each(function(i, element) { if (skip_first) {skip_first = false; return; }path += '/'+$(element).text(); }); $(dialog_content_id + ' .filepicker_loader').css('visibility', 'hidden');
$(event.data.dcid).data('path', path); },
$(event.data.dcid + ' .filepicker_loader').css('visibility', 'visible'); handleTreeListSelect:function(event) {
$.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {dir: path, mimetype: $(event.data.dcid).data('mimetype')}, function(r){OC.dialogs.fillFilePicker(r, event.data.dcid)}); var newval = parseInt($(this).val());
}, var oldval = parseInt($(this).data('oldval'));
// this function is in early development state, please dont use it unlsess you know what you are doing while (newval != oldval && oldval > 0) {
handlePickerClick:function(element, name, dcid) { $('option:last', this).remove();
var p = $(dcid).data('path'); $('option:last', this).attr('selected','selected');
if (p == undefined) p = ''; oldval--;
p = p+'/'+name; }
if ($(element).attr('data') == 'file'){ var skip_first = true;
if ($(dcid).data('multiselect') != true) var path = '';
$(dcid+' .filepicker_element_selected').removeClass('filepicker_element_selected'); $(this).children().each(function(i, element) {
$(element).toggleClass('filepicker_element_selected'); if (skip_first) {
return; skip_first = false;
} return;
$(dcid).data('path', p); }
$(dcid + ' #dirtree option:last').removeAttr('selected'); path += '/'+$(element).text();
var newval = parseInt($(dcid + ' #dirtree option:last').val())+1; });
$(dcid + ' #dirtree').append('<option selected="selected" value="'+newval+'">'+name+'</option>'); $(event.data.dcid).data('path', path);
$(dcid + ' .filepicker_loader').css('visibility', 'visible'); $(event.data.dcid + ' .filepicker_loader').css('visibility', 'visible');
$.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {dir: p, mimetype: $(dcid).data('mimetype')}, function(r){OC.dialogs.fillFilePicker(r, dcid)}); $.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {dir: path, mimetype: $(event.data.dcid).data('mimetype')}, function(r){OC.dialogs.fillFilePicker(r, event.data.dcid)});
} },
// this function is in early development state, please dont use it unlsess you know what you are doing
handlePickerClick:function(element, name, dcid) {
var p = $(dcid).data('path');
if (p == undefined) p = '';
p = p+'/'+name;
if ($(element).attr('data') == 'file'){
if ($(dcid).data('multiselect') != true) {
$(dcid+' .filepicker_element_selected').removeClass('filepicker_element_selected');
}
$(element).toggleClass('filepicker_element_selected');
return;
}
$(dcid).data('path', p);
$(dcid + ' #dirtree option:last').removeAttr('selected');
var newval = parseInt($(dcid + ' #dirtree option:last').val())+1;
$(dcid + ' #dirtree').append('<option selected="selected" value="'+newval+'">'+name+'</option>');
$(dcid + ' .filepicker_loader').css('visibility', 'visible');
$.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {dir: p, mimetype: $(dcid).data('mimetype')}, function(r){OC.dialogs.fillFilePicker(r, dcid)});
}
}; };