1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/**
* ownCloud
*
* @author Bartek Przybylski
* @copyright 2012 Bartek Przybylski bartek@alefzero.eu
*
* 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
*/
var OCdialogs = {
/**
* 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) {
var content = '<p><span class="ui-icon ui-icon-alert"></span>' + escapeHTML(text) + '</p>';
OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, 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) {
var content = '<p><span class="ui-icon ui-icon-info"></span>' + escapeHTML(text) + '</p>';
OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, 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) {
var content = '<p><span class="ui-icon ui-icon-notice"></span>' + escapeHTML(text) + '</p>';
OCdialogs.message(content, title, OCdialogs.ALERT_DIALOG, OCdialogs.YES_NO_BUTTONS, callback, modal);
},
/**
* prompt for user input
* @param text content of dialog
* @param title dialog title
* @param callback which will be triggered when user presses OK (input text will be passed to callback)
* @param modal make the dialog modal
*/
prompt:function(text, title, default_value, callback, modal) {
var input = '<input type="text" id="oc-dialog-prompt-input" value="' + escapeHTML(default_value) + '" style="width:90%">';
var content = '<p><span class="ui-icon ui-icon-pencil"></span>' + escapeHTML(text) + ':<br/>' + input + '</p>';
OCdialogs.message(content, title, OCdialogs.PROMPT_DIALOG, OCdialogs.OK_BUTTON, callback, modal);
},
/**
* prompt user for input with custom form
* fields should be passed in following format: [{text:'prompt text', name:'return name', type:'input type', value: 'default value'},...]
* example:
* var fields=[{text:'Test', name:'test', type:'select', options:[{text:'hello1',value:1},{text:'hello2',value:2}] }];
* @param fields to display
* @param title dialog title
* @param callback which will be triggered when user presses OK (user answers will be passed to callback in following format: [{name:'return name', value: 'user value'},...])
* @param modal make the dialog modal
*/
form:function(fields, title, callback, modal) {
var content = '<table>';
$.each(fields, function(index, field){
content += '<tr><td>' + escapeHTML(field.text) + '</td><td>';
var type = field.type;
if (type === 'text' || type === 'checkbox' || type === 'password') {
content += '<input type="' + type + '" name="' + field.name + '"';
if (type === 'checkbox' && field.value === true) {
content += ' checked="checked"';
} else if (type === 'text' || type === 'password' && val.value) {
content += ' value="' + escapeHTML(field.value) + '"';
}
content += '>';
} else if (type === 'select') {
content += '<select name="' + escapeHTML(field.name) + '"';
if (field.value !== undefined) {
content += ' value="' + escapeHTML(field.value) + '"';
}
content += '>';
$.each(field.options, function(index, field_option){
content += '<option value="' + escapeHTML(field_option.value) + '">' + escapeHTML(field_option.text) + '</option>';
});
content += '</select>';
}
content += '</td></tr>';
});
content += '</table>';
var dialog_name = 'oc-dialog-' + OCdialogs.dialogs_counter + '-content';
var dialog_id = '#' + dialog_name;
var dialog_div = '<div id="' + dialog_name + '" title="' + escapeHTML(title) + '">' + content + '</div>';
if (modal === undefined) { modal = false };
$('body').append(dialog_div);
var buttonlist = [{
text: t('core', 'Ok'),
click: function(){ OCdialogs.form_ok_handler(callback, dialog_id); }
},
{
text: t('core', 'Cancel'),
click: function(){ $(dialog_id).dialog('close'); }
}];
var dialog_height = ( $('tr', dialog_div).length + 1 ) * 30 + 120;
$(dialog_id).dialog({
width: (4/9) * $(document).width(),
height: dialog_height,
modal: modal,
buttons: buttonlist
});
OCdialogs.dialogs_counter++;
},
/**
* 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) {
var dialog_name = 'oc-dialog-' + OCdialogs.dialogs_counter + '-content';
var dialog_id = '#' + dialog_name;
var dialog_content = '<button id="dirup">↑</button><select id="dirtree"></select><div id="filelist"></div>';
var dialog_loader = '<div class="filepicker_loader"><img src="' + OC.filePath('gallery','img','loading.gif') + '"></div>';
var dialog_div = '<div id="' + dialog_name + '" title="' + escapeHTML(title) + '">' + dialog_content + dialog_loader + '</div>';
if (modal === undefined) { modal = false };
if (multiselect === undefined) { multiselect = false };
if (mimetype_filter === undefined) { mimetype_filter = '' };
$('body').append(dialog_div);
$(dialog_id).data('path', '/');
$(dialog_id + ' #dirtree').focus().change( {dcid: dialog_id}, OCdialogs.handleTreeListSelect );
$(dialog_id + ' #dirup').click( {dcid: dialog_id}, OCdialogs.filepickerDirUp );
$(dialog_id).ready(function(){
$.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), { mimetype: mimetype_filter } ,function(request) {
OCdialogs.fillFilePicker(request, dialog_id);
});
$.getJSON(OC.filePath('files', 'ajax', 'rawlist.php'), { mimetype: "httpd/unix-directory" }, function(request) {
OCdialogs.fillTreeList(request, dialog_id);
});
}).data('multiselect', multiselect).data('mimetype',mimetype_filter);
// build buttons
var functionToCall = function() {
if (callback !== undefined) {
var datapath;
if (multiselect === true) {
datapath = [];
$(dialog_id + ' .filepicker_element_selected .filename').each(function(index, element) {
datapath.push( $(dialog_id).data('path') + $(element).text() );
});
} else {
var datapath = $(dialog_id).data('path');
datapath += $(dialog_id+' .filepicker_element_selected .filename').text();
}
callback(datapath);
$(dialog_id).dialog('close');
}
};
var buttonlist = [{
text: t('core', 'Choose'),
click: functionToCall
},
{
text: t('core', 'Cancel'),
click: function(){$(dialog_id).dialog('close'); }
}];
$(dialog_id).dialog({
width: (4/9)*$(document).width(),
height: 420,
modal: modal,
buttons: buttonlist
});
OCdialogs.dialogs_counter++;
},
/**
* Displays raw dialog
* You better use a wrapper instead ...
*/
message:function(content, title, dialog_type, buttons, callback, modal) {
var dialog_name = 'oc-dialog-' + OCdialogs.dialogs_counter + '-content';
var dialog_id = '#' + dialog_name;
var dialog_div = '<div id="' + dialog_name + '" title="' + escapeHTML(title) + '">' + content + '</div>';
if (modal === undefined) { modal = false };
$('body').append(dialog_div);
var buttonlist = [];
switch (buttons) {
case OCdialogs.YES_NO_BUTTONS:
buttonlist = [{
text: t('core', 'Yes'),
click: function(){
if (callback !== undefined) { callback(true) };
$(dialog_id).dialog('close');
}
},
{
text: t('core', 'No'),
click: function(){
if (callback !== undefined) { callback(false) };
$(dialog_id).dialog('close');
}
}];
break;
case OCdialogs.OK_BUTTON:
var functionToCall;
switch(dialog_type) {
case OCdialogs.ALERT_DIALOG:
functionToCall = function() {
$(dialog_id).dialog('close');
if(callback !== undefined) { callback() };
};
break;
case OCdialogs.PROMPT_DIALOG:
buttonlist[1] = {
text: t('core', 'Cancel'),
click: function() { $(dialog_id).dialog('close'); }
};
functionToCall = function() { OCdialogs.prompt_ok_handler(callback, dialog_id); };
break;
}
buttonlist[0] = {
text: t('core', 'Ok'),
click: functionToCall
};
break;
};
$(dialog_id).dialog({
width: (4/9) * $(document).width(),
height: 180,
modal: modal,
buttons: buttonlist
});
OCdialogs.dialogs_counter++;
},
// dialog button types
YES_NO_BUTTONS: 70,
OK_BUTTONS: 71,
// dialogs types
ALERT_DIALOG: 80,
INFO_DIALOG: 81,
FORM_DIALOG: 82,
// used to name each dialog
dialogs_counter: 0,
determineValue: function(element) {
if ( $(element).attr('type') === 'checkbox' ) {
return element.checked;
} else {
return $(element).val();
}
},
prompt_ok_handler: function(callback, dialog_id) {
$(dialog_id).dialog('close');
if (callback !== undefined) { callback($(dialog_id + " input#oc-dialog-prompt-input").val()) };
},
form_ok_handler: function(callback, dialog_id) {
if (callback !== undefined) {
var valuelist = [];
$(dialog_id + ' input, ' + dialog_id + ' select').each(function(index, element) {
valuelist[index] = { name: $(element).attr('name'), value: OCdialogs.determineValue(element) };
});
$(dialog_id).dialog('close');
callback(valuelist);
} else {
$(dialog_id).dialog('close');
}
},
/**
* fills the filepicker with files
*/
fillFilePicker:function(request, dialog_content_id) {
var template_content = '<img src="*MIMETYPEICON*" style="margin: 2px 1em 0 4px;"><span class="filename">*NAME*</span><div style="float:right;margin-right:1em;">*LASTMODDATE*</div>';
var template = '<div data-entryname="*ENTRYNAME*" data-dcid="' + escapeHTML(dialog_content_id) + '" data="*ENTRYTYPE*">*CONTENT*</div>';
var files = '';
var dirs = [];
var others = [];
$.each(request.data, function(index, file) {
if (file.type === 'dir') {
dirs.push(file);
} else {
others.push(file);
}
});
var sorted = dirs.concat(others);
for (var i = 0; i < sorted.length; i++) {
files_content = template_content.replace('*LASTMODDATE*', OC.mtime2date(sorted[i].mtime)).replace('*NAME*', escapeHTML(sorted[i].name)).replace('*MIMETYPEICON*', sorted[i].mimetype_icon);
files += template.replace('*ENTRYNAME*', escapeHTML(sorted[i].name)).replace('*ENTRYTYPE*', escapeHTML(sorted[i].type)).replace('*CONTENT*', files_content);
}
$(dialog_content_id + ' #filelist').html(files);
$('#filelist div').click(function() {
OCdialogs.handlePickerClick($(this), $(this).data('entryname'), dialog_content_id);
});
$(dialog_content_id + ' .filepicker_loader').css('visibility', 'hidden');
},
/**
* fills the tree list with directories
*/
fillTreeList: function(request, dialog_id) {
var template = '<option value="*COUNT*">*NAME*</option>';
var paths = '<option value="0">' + escapeHTML($(dialog_id).data('path')) + '</option>';
$.each(request.data, function(index, file) {
paths += template.replace('*COUNT*', index).replace('*NAME*', escapeHTML(file.name));
});
$(dialog_id + ' #dirtree').html(paths);
},
/**
* handle selection made in the tree list
*/
handleTreeListSelect:function(event) {
if ($("option:selected", this).html().indexOf('/') !== -1) { // if there's a slash in the selected path, don't append it
$(event.data.dcid).data('path', $("option:selected", this).html());
} else {
$(event.data.dcid).data('path', $(event.data.dcid).data('path') + $("option:selected", this).html() + '/');
}
$(event.data.dcid + ' .filepicker_loader').css('visibility', 'visible');
$.getJSON(
OC.filePath('files', 'ajax', 'rawlist.php'),
{
dir: $(event.data.dcid).data('path'),
mimetype: $(event.data.dcid).data('mimetype')
},
function(request) { OCdialogs.fillFilePicker(request, event.data.dcid) }
);
$.getJSON(
OC.filePath('files', 'ajax', 'rawlist.php'),
{
dir: $(event.data.dcid).data('path'),
mimetype: "httpd/unix-directory"
},
function(request) { OCdialogs.fillTreeList(request, event.data.dcid) }
);
},
/**
* go one directory up
*/
filepickerDirUp:function(event) {
var old_path = $(event.data.dcid).data('path');
if ( old_path !== "/") {
var splitted_path = old_path.split("/");
var new_path = ""
for (var i = 0; i < splitted_path.length - 2; i++) {
new_path += splitted_path[i] + "/"
}
$(event.data.dcid).data('path', new_path);
$.getJSON(
OC.filePath('files', 'ajax', 'rawlist.php'),
{
dir: $(event.data.dcid).data('path'),
mimetype: $(event.data.dcid).data('mimetype')
},
function(request) { OCdialogs.fillFilePicker(request, event.data.dcid) }
);
$.getJSON(
OC.filePath('files', 'ajax', 'rawlist.php'),
{
dir: $(event.data.dcid).data('path'),
mimetype: "httpd/unix-directory"
},
function(request) { OCdialogs.fillTreeList(request, event.data.dcid) }
);
}
},
/**
* handle clicks made in the filepicker
*/
handlePickerClick:function(element, name, dialog_content_id) {
if ( $(element).attr('data') === 'file' ){
if ( $(dialog_content_id).data('multiselect') !== true) {
$(dialog_content_id + ' .filepicker_element_selected').removeClass('filepicker_element_selected');
}
$(element).toggleClass('filepicker_element_selected');
return;
} else if ( $(element).attr('data') === 'dir' ) {
var datapath = escapeHTML( $(dialog_content_id).data('path') + name + '/' );
$(dialog_content_id).data('path', datapath);
$(dialog_content_id + ' .filepicker_loader').css('visibility', 'visible');
$.getJSON(
OC.filePath('files', 'ajax', 'rawlist.php'),
{
dir: datapath,
mimetype: $(dialog_content_id).data('mimetype')
},
function(request){ OCdialogs.fillFilePicker(request, dialog_content_id) }
);
$.getJSON(
OC.filePath('files', 'ajax', 'rawlist.php'),
{
dir: datapath,
mimetype: "httpd/unix-directory"
},
function(request) { OCdialogs.fillTreeList(request, dialog_content_id) }
);
}
}
};
|