oc-dialogs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * ownCloud
  3. *
  4. * @author Bartek Przybylski
  5. * @copyright 2012 Bartek Przybylski bartek@alefzero.eu
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /**
  22. * this class to ease the usage of jquery dialogs
  23. */
  24. var OCdialogs = {
  25. /**
  26. * displays alert dialog
  27. * @param text content of dialog
  28. * @param title dialog title
  29. * @param callback which will be triggered when user press OK
  30. */
  31. alert:function(text, title, callback, modal) {
  32. var content = '<p><span class="ui-icon ui-icon-alert"></span>'+text+'</p>';
  33. OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.OK_BUTTON, callback, modal);
  34. },
  35. /**
  36. * displays info dialog
  37. * @param text content of dialog
  38. * @param title dialog title
  39. * @param callback which will be triggered when user press OK
  40. */
  41. info:function(text, title, callback, modal) {
  42. var content = '<p><span class="ui-icon ui-icon-info"></span>'+text+'</p>';
  43. OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.OK_BUTTON, callback, modal);
  44. },
  45. /**
  46. * displays confirmation dialog
  47. * @param text content of dialog
  48. * @param title dialog title
  49. * @param callback which will be triggered when user press YES or NO (true or false would be passed to callback respectively)
  50. */
  51. confirm:function(text, title, callback, modal) {
  52. var content = '<p><span class="ui-icon ui-icon-notice"></span>'+text+'</p>';
  53. OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.YES_NO_BUTTONS, callback, modal);
  54. },
  55. /**
  56. * prompt for user input
  57. * @param text content of dialog
  58. * @param title dialog title
  59. * @param callback which will be triggered when user press OK (input text will be passed to callback)
  60. */
  61. prompt:function(text, title, default_value, callback, modal) {
  62. 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>';
  63. OCdialogs.message(content, title, OCdialogs.PROMPT_DIALOG, OCdialogs.OK_CANCEL_BUTTONS, callback, modal);
  64. },
  65. /**
  66. * prompt user for input with custom form
  67. * fields should be passed in following format: [{text:'prompt text', name:'return name', type:'input type', value: 'dafault value'},...]
  68. * select example var fields=[{text:'Test', name:'test', type:'select', options:[{text:'hallo',value:1},{text:'hallo1',value:2}] }];
  69. * @param fields to display
  70. * @param title dialog title
  71. * @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'},...])
  72. */
  73. form:function(fields, title, callback, modal) {
  74. var content = '<table>';
  75. $.each(fields, function(index, val){
  76. content += '<tr><td>'+val.text+'</td><td>';
  77. var type=val.type;
  78. if (type == 'text' || type == 'checkbox' || type == 'password') {
  79. content += '<input type="'+type+'" name="'+val.name+'"';
  80. if (type == 'checkbox') {
  81. if (val.value != undefined && val.value == true) {
  82. content += ' checked="checked">';
  83. } else {
  84. content += '>';
  85. }
  86. } else if (type == 'text' || type == 'password' && val.value) {
  87. content += ' value="'+val.value+'">';
  88. }
  89. } else if (type == 'select') {
  90. content += '<select name="'+val.name+'"';
  91. if (val.value != undefined) {
  92. content += ' value="'+val.value+'"';
  93. }
  94. content += '>';
  95. $.each(val.options, function(index, valo){
  96. content += '<option value="'+valo.value+'">'+valo.text+'</option>';
  97. });
  98. content += '</select>';
  99. }
  100. content += '</td></tr>';
  101. });
  102. content += '</table>';
  103. OCdialogs.message(content, title, OCdialogs.FORM_DIALOG, OCdialogs.OK_CANCEL_BUTTONS, callback, modal);
  104. },
  105. filepicker:function(title, callback, multiselect, mimetype_filter, modal) {
  106. var c_name = 'oc-dialog-'+OCdialogs.dialogs_counter+'-content';
  107. var c_id = '#'+c_name;
  108. 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>';
  109. if (!modal) modal = false; // Huh..
  110. if (!multiselect) multiselect = false;
  111. $('body').append(d);
  112. $(c_id + ' #dirtree').focus(function() {
  113. var t = $(this);
  114. t.data('oldval', t.val())
  115. }).change({dcid: c_id}, OC.dialogs.handleTreeListSelect);
  116. $(c_id).ready(function(){
  117. $.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {mimetype: mimetype_filter} ,function(r) {
  118. OC.dialogs.fillFilePicker(r, c_id, callback)
  119. });
  120. }).data('multiselect', multiselect).data('mimetype',mimetype_filter);
  121. // build buttons
  122. var b = [{
  123. text: t('core', 'Choose'),
  124. click: function(){
  125. if (callback != undefined) {
  126. var p;
  127. if ($(c_id).data('multiselect') == true) {
  128. p = [];
  129. $(c_id+' .filepicker_element_selected #filename').each(function(i, elem) {
  130. p.push(($(c_id).data('path')?$(c_id).data('path'):'')+'/'+$(elem).text());
  131. });
  132. } else {
  133. var p = $(c_id).data('path');
  134. if (p == undefined) p = '';
  135. p = p+'/'+$(c_id+' .filepicker_element_selected #filename').text()
  136. }
  137. callback(p);
  138. $(c_id).dialog('close');
  139. }
  140. }
  141. },
  142. {
  143. text: t('core', 'Cancel'),
  144. click: function(){$(c_id).dialog('close'); }}
  145. ];
  146. $(c_id).dialog({width: ((4*$('body').width())/9), height: 400, modal: modal, buttons: b});
  147. OCdialogs.dialogs_counter++;
  148. },
  149. // guts, dont use, dont touch
  150. message:function(content, title, dialog_type, buttons, callback, modal) {
  151. var c_name = 'oc-dialog-'+OCdialogs.dialogs_counter+'-content';
  152. var c_id = '#'+c_name;
  153. var d = '<div id="'+c_name+'" title="'+title+'">'+content+'</div>';
  154. if (modal == undefined) modal = false;
  155. $('body').append(d);
  156. var b = [];
  157. switch (buttons) {
  158. case OCdialogs.YES_NO_BUTTONS:
  159. b[1] = {text: t('core', 'No'), click: function(){ if (callback != undefined) callback(false); $(c_id).dialog('close'); }};
  160. b[0] = {text: t('core', 'Yes'), click: function(){ if (callback != undefined) callback(true); $(c_id).dialog('close');}};
  161. break;
  162. case OCdialogs.OK_CANCEL_BUTTONS:
  163. b[1] = {text: t('core', 'Cancel'), click: function(){$(c_id).dialog('close'); }};
  164. case OCdialogs.OK_BUTTON: // fallthrough
  165. var f;
  166. switch(dialog_type) {
  167. case OCdialogs.ALERT_DIALOG:
  168. f = function(){$(c_id).dialog('close'); if(callback) callback();};
  169. break;
  170. case OCdialogs.PROMPT_DIALOG:
  171. f = function(){OCdialogs.prompt_ok_handler(callback, c_id)};
  172. break;
  173. case OCdialogs.FORM_DIALOG:
  174. f = function(){OCdialogs.form_ok_handler(callback, c_id)};
  175. break;
  176. }
  177. b[0] = {text: t('core', 'Ok'), click: f};
  178. break;
  179. }
  180. var possible_height = ($('tr', d).size()+1)*30;
  181. $(c_id).dialog({width: 4*$(document).width()/9, height: possible_height + 120, modal: modal, buttons: b});
  182. OCdialogs.dialogs_counter++;
  183. },
  184. // dialogs buttons types
  185. YES_NO_BUTTONS: 70,
  186. OK_BUTTONS: 71,
  187. OK_CANCEL_BUTTONS: 72,
  188. // dialogs types
  189. ALERT_DIALOG: 80,
  190. INFO_DIALOG: 81,
  191. PROMPT_DIALOG: 82,
  192. FORM_DIALOG: 83,
  193. dialogs_counter: 0,
  194. determineValue: function(element) {
  195. switch ($(element).attr('type')) {
  196. case 'checkbox': return element.checked;
  197. }
  198. return $(element).val();
  199. },
  200. prompt_ok_handler: function(callback, c_id) { $(c_id).dialog('close'); if (callback != undefined) callback($(c_id + " input#oc-dialog-prompt-input").val()); },
  201. form_ok_handler: function(callback, c_id) {
  202. if (callback != undefined) {
  203. var r = [];
  204. var c = 0;
  205. $(c_id + ' input, '+c_id+' select').each(function(i, elem) {
  206. r[c] = {name: $(elem).attr('name'), value: OCdialogs.determineValue(elem)};
  207. c++;
  208. });
  209. $(c_id).dialog('close');
  210. callback(r);
  211. } else {
  212. $(c_id).dialog('close');
  213. }
  214. },
  215. fillFilePicker:function(r, dialog_content_id) {
  216. 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>';
  217. var names = '';
  218. $.each(r.data, function(index, a) {
  219. names += entry_template.replace('*LASTMODDATE*', OC.mtime2date(a.mtime)).replace('*NAME*', a.name).replace('*MIMETYPEICON*', a.mimetype_icon).replace('*ENTRYNAME*', a.name).replace('*ENTRYTYPE*', a.type);
  220. });
  221. $(dialog_content_id + ' #filelist').html(names);
  222. $(dialog_content_id + ' .filepicker_loader').css('visibility', 'hidden');
  223. },
  224. handleTreeListSelect:function(event) {
  225. var newval = parseInt($(this).val());
  226. var oldval = parseInt($(this).data('oldval'));
  227. while (newval != oldval && oldval > 0) {
  228. $('option:last', this).remove();
  229. $('option:last', this).attr('selected','selected');
  230. oldval--;
  231. }
  232. var skip_first = true;
  233. var path = '';
  234. $(this).children().each(function(i, element) {
  235. if (skip_first) {
  236. skip_first = false;
  237. return;
  238. }
  239. path += '/'+$(element).text();
  240. });
  241. $(event.data.dcid).data('path', path);
  242. $(event.data.dcid + ' .filepicker_loader').css('visibility', 'visible');
  243. $.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {dir: path, mimetype: $(event.data.dcid).data('mimetype')}, function(r){OC.dialogs.fillFilePicker(r, event.data.dcid)});
  244. },
  245. // this function is in early development state, please dont use it unlsess you know what you are doing
  246. handlePickerClick:function(element, name, dcid) {
  247. var p = $(dcid).data('path');
  248. if (p == undefined) p = '';
  249. p = p+'/'+name;
  250. if ($(element).attr('data') == 'file'){
  251. if ($(dcid).data('multiselect') != true) {
  252. $(dcid+' .filepicker_element_selected').removeClass('filepicker_element_selected');
  253. }
  254. $(element).toggleClass('filepicker_element_selected');
  255. return;
  256. }
  257. $(dcid).data('path', p);
  258. $(dcid + ' #dirtree option:last').removeAttr('selected');
  259. var newval = parseInt($(dcid + ' #dirtree option:last').val())+1;
  260. $(dcid + ' #dirtree').append('<option selected="selected" value="'+newval+'">'+name+'</option>');
  261. $(dcid + ' .filepicker_loader').css('visibility', 'visible');
  262. $.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), {dir: p, mimetype: $(dcid).data('mimetype')}, function(r){OC.dialogs.fillFilePicker(r, dcid)});
  263. }
  264. };