imce.js
26.6 KB
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
(function($) {
//Global container.
window.imce = {tree: {}, findex: [], fids: {}, selected: {}, selcount: 0, ops: {}, cache: {}, urlId: {},
vars: {previewImages: 1, cache: 1},
hooks: {load: [], list: [], navigate: [], cache: []},
//initiate imce.
initiate: function() {
imce.conf = Drupal.settings.imce || {};
if (imce.conf.error != false) return;
imce.ie = (navigator.userAgent.match(/msie (\d+)/i) || ['', 0])[1] * 1;
imce.FLW = imce.el('file-list-wrapper'), imce.SBW = imce.el('sub-browse-wrapper');
imce.NW = imce.el('navigation-wrapper'), imce.BW = imce.el('browse-wrapper');
imce.PW = imce.el('preview-wrapper'), imce.FW = imce.el('forms-wrapper');
imce.updateUI();
imce.prepareMsgs();//process initial status messages
imce.initiateTree();//build directory tree
imce.hooks.list.unshift(imce.processRow);//set the default list-hook.
imce.initiateList();//process file list
imce.initiateOps();//prepare operation tabs
imce.refreshOps();
// Bind global error handler
$(document).ajaxError(imce.ajaxError);
imce.invoke('load', window);//run functions set by external applications.
},
//process navigation tree
initiateTree: function() {
$('#navigation-tree li').each(function(i) {
var a = this.firstChild, txt = a.firstChild;
txt && (txt.data = imce.decode(txt.data));
var branch = imce.tree[a.title] = {'a': a, li: this, ul: this.lastChild.tagName == 'UL' ? this.lastChild : null};
if (a.href) imce.dirClickable(branch);
imce.dirCollapsible(branch);
});
},
//Add a dir to the tree under parent
dirAdd: function(dir, parent, clickable) {
if (imce.tree[dir]) return clickable ? imce.dirClickable(imce.tree[dir]) : imce.tree[dir];
var parent = parent || imce.tree['.'];
parent.ul = parent.ul ? parent.ul : parent.li.appendChild(imce.newEl('ul'));
var branch = imce.dirCreate(dir, imce.decode(dir.substr(dir.lastIndexOf('/')+1)), clickable);
parent.ul.appendChild(branch.li);
return branch;
},
//create list item for navigation tree
dirCreate: function(dir, text, clickable) {
if (imce.tree[dir]) return imce.tree[dir];
var branch = imce.tree[dir] = {li: imce.newEl('li'), a: imce.newEl('a')};
$(branch.a).addClass('folder').text(text).attr('title', dir).appendTo(branch.li);
imce.dirCollapsible(branch);
return clickable ? imce.dirClickable(branch) : branch;
},
//change currently active directory
dirActivate: function(dir) {
if (dir != imce.conf.dir) {
if (imce.tree[imce.conf.dir]){
$(imce.tree[imce.conf.dir].a).removeClass('active');
}
$(imce.tree[dir].a).addClass('active');
imce.conf.dir = dir;
}
return imce.tree[imce.conf.dir];
},
//make a dir accessible
dirClickable: function(branch) {
if (branch.clkbl) return branch;
$(branch.a).attr('href', '#').removeClass('disabled').click(function() {imce.navigate(this.title); return false;});
branch.clkbl = true;
return branch;
},
//sub-directories expand-collapse ability
dirCollapsible: function (branch) {
if (branch.clpsbl) return branch;
$(imce.newEl('span')).addClass('expander').html(' ').click(function() {
if (branch.ul) {
$(branch.ul).toggle();
$(branch.li).toggleClass('expanded');
imce.ie && $('#navigation-header').css('top', imce.NW.scrollTop);
}
else if (branch.clkbl){
$(branch.a).click();
}
}).prependTo(branch.li);
branch.clpsbl = true;
return branch;
},
//update navigation tree after getting subdirectories.
dirSubdirs: function(dir, subdirs) {
var branch = imce.tree[dir];
if (subdirs && subdirs.length) {
var prefix = dir == '.' ? '' : dir +'/';
for (var i in subdirs) {//add subdirectories
imce.dirAdd(prefix + subdirs[i], branch, true);
}
$(branch.li).removeClass('leaf').addClass('expanded');
$(branch.ul).show();
}
else if (!branch.ul){//no subdirs->leaf
$(branch.li).removeClass('expanded').addClass('leaf');
}
},
//process file list
initiateList: function(cached) {
var L = imce.hooks.list, dir = imce.conf.dir, token = {'%dir': dir == '.' ? $(imce.tree['.'].a).text() : imce.decode(dir)}
imce.findex = [], imce.fids = {}, imce.selected = {}, imce.selcount = 0, imce.vars.lastfid = null;
imce.tbody = imce.el('file-list').tBodies[0];
if (imce.tbody.rows.length) {
for (var row, i = 0; row = imce.tbody.rows[i]; i++) {
var fid = row.id;
imce.findex[i] = imce.fids[fid] = row;
if (cached) {
if (imce.hasC(row, 'selected')) {
imce.selected[imce.vars.lastfid = fid] = row;
imce.selcount++;
}
}
else {
for (var func, j = 0; func = L[j]; j++) func(row);//invoke list-hook
}
}
}
if (!imce.conf.perm.browse) {
imce.setMessage(Drupal.t('File browsing is disabled in directory %dir.', token), 'error');
}
},
//add a file to the list. (having properties name,size,formatted size,width,height,date,formatted date)
fileAdd: function(file) {
var row, fid = file.name, i = imce.findex.length, attr = ['name', 'size', 'width', 'height', 'date'];
if (!(row = imce.fids[fid])) {
row = imce.findex[i] = imce.fids[fid] = imce.tbody.insertRow(i);
for (var i in attr) row.insertCell(i).className = attr[i];
}
row.cells[0].innerHTML = row.id = fid;
row.cells[1].innerHTML = file.fsize; row.cells[1].id = file.size;
row.cells[2].innerHTML = file.width;
row.cells[3].innerHTML = file.height;
row.cells[4].innerHTML = file.fdate; row.cells[4].id = file.date;
imce.invoke('list', row);
if (imce.vars.prvfid == fid) imce.setPreview(fid);
if (file.id) imce.urlId[imce.getURL(fid)] = file.id;
},
//remove a file from the list
fileRemove: function(fid) {
if (!(row = imce.fids[fid])) return;
imce.fileDeSelect(fid);
imce.findex.splice(row.rowIndex, 1);
$(row).remove();
delete imce.fids[fid];
if (imce.vars.prvfid == fid) imce.setPreview();
},
//return a file object containing all properties.
fileGet: function (fid) {
var row = imce.fids[fid];
var url = imce.getURL(fid);
return row ? {
name: imce.decode(fid),
url: url,
size: row.cells[1].innerHTML,
bytes: row.cells[1].id * 1,
width: row.cells[2].innerHTML * 1,
height: row.cells[3].innerHTML * 1,
date: row.cells[4].innerHTML,
time: row.cells[4].id * 1,
id: imce.urlId[url] || 0, //file id for newly uploaded files
relpath: (imce.conf.dir == '.' ? '' : imce.conf.dir +'/') + fid //rawurlencoded path relative to file directory path.
} : null;
},
//simulate row click. selection-highlighting
fileClick: function(row, ctrl, shft) {
if (!row) return;
var fid = typeof(row) == 'string' ? row : row.id;
if (ctrl || fid == imce.vars.prvfid) {
imce.fileToggleSelect(fid);
}
else if (shft) {
var last = imce.lastFid();
var start = last ? imce.fids[last].rowIndex : -1;
var end = imce.fids[fid].rowIndex;
var step = start > end ? -1 : 1;
while (start != end) {
start += step;
imce.fileSelect(imce.findex[start].id);
}
}
else {
for (var fname in imce.selected) {
imce.fileDeSelect(fname);
}
imce.fileSelect(fid);
}
//set preview
imce.setPreview(imce.selcount == 1 ? imce.lastFid() : null);
},
//file select/deselect functions
fileSelect: function (fid) {
if (imce.selected[fid] || !imce.fids[fid]) return;
imce.selected[fid] = imce.fids[imce.vars.lastfid=fid];
$(imce.selected[fid]).addClass('selected');
imce.selcount++;
},
fileDeSelect: function (fid) {
if (!imce.selected[fid] || !imce.fids[fid]) return;
if (imce.vars.lastfid == fid) imce.vars.lastfid = null;
$(imce.selected[fid]).removeClass('selected');
delete imce.selected[fid];
imce.selcount--;
},
fileToggleSelect: function (fid) {
imce['file'+ (imce.selected[fid] ? 'De' : '') +'Select'](fid);
},
//process file operation form and create operation tabs.
initiateOps: function() {
imce.setHtmlOps();
imce.setUploadOp();//upload
imce.setFileOps();//thumb, delete, resize
},
//process existing html ops.
setHtmlOps: function () {
$(imce.el('ops-list')).children('li').each(function() {
if (!this.firstChild) return $(this).remove();
var name = this.id.substr(8);
var Op = imce.ops[name] = {div: imce.el('op-content-'+ name), li: imce.el('op-item-'+ name)};
Op.a = Op.li.firstChild;
Op.title = Op.a.innerHTML;
$(Op.a).click(function() {imce.opClick(name); return false;});
});
},
//convert upload form to an op.
setUploadOp: function () {
var el, form = imce.el('imce-upload-form');
if (!form) return;
$(form).ajaxForm(imce.uploadSettings()).find('fieldset').each(function() {//clean up fieldsets
this.removeChild(this.firstChild);
$(this).after(this.childNodes);
}).remove();
// Set html response flag
el = form.elements['files[imce]'];
if (el && el.files && window.FormData) {
if (el = form.elements.html_response) {
el.value = 0;
}
}
imce.opAdd({name: 'upload', title: Drupal.t('Upload'), content: form});//add op
},
//convert fileop form submit buttons to ops.
setFileOps: function () {
var form = imce.el('imce-fileop-form');
if (!form) return;
$(form.elements.filenames).parent().remove();
$(form).find('fieldset').each(function() {//remove fieldsets
var $sbmt = $('input:submit', this);
if (!$sbmt.length) return;
var Op = {name: $sbmt.attr('id').substr(5)};
var func = function() {imce.fopSubmit(Op.name); return false;};
$sbmt.click(func);
Op.title = $(this).children('legend').remove().text() || $sbmt.val();
Op.name == 'delete' ? (Op.func = func) : (Op.content = this.childNodes);
imce.opAdd(Op);
}).remove();
imce.vars.opform = $(form).serialize();//serialize remaining parts.
},
//refresh ops states. enable/disable
refreshOps: function() {
for (var p in imce.conf.perm) {
if (imce.conf.perm[p]) imce.opEnable(p);
else imce.opDisable(p);
}
},
//add a new file operation
opAdd: function (op) {
var oplist = imce.el('ops-list'), opcons = imce.el('op-contents');
var name = op.name || ('op-'+ $(oplist).children('li').length);
var title = op.title || 'Untitled';
var Op = imce.ops[name] = {title: title};
if (op.content) {
Op.div = imce.newEl('div');
$(Op.div).attr({id: 'op-content-'+ name, 'class': 'op-content'}).appendTo(opcons).append(op.content);
}
Op.a = imce.newEl('a');
Op.li = imce.newEl('li');
$(Op.a).attr({href: '#', name: name, title: title}).html('<span>' + title +'</span>').click(imce.opClickEvent);
$(Op.li).attr('id', 'op-item-'+ name).append(Op.a).appendTo(oplist);
Op.func = op.func || imce.opVoid;
return Op;
},
//click event for file operations
opClickEvent: function(e) {
imce.opClick(this.name);
return false;
},
//void operation function
opVoid: function() {},
//perform op click
opClick: function(name) {
var Op = imce.ops[name], oldop = imce.vars.op;
if (!Op || Op.disabled) {
return imce.setMessage(Drupal.t('You can not perform this operation.'), 'error');
}
if (Op.div) {
if (oldop) {
var toggle = oldop == name;
imce.opShrink(oldop, toggle ? 'fadeOut' : 'hide');
if (toggle) return false;
}
var left = Op.li.offsetLeft;
var $opcon = $('#op-contents').css({left: 0});
$(Op.div).fadeIn('normal', function() {
setTimeout(function() {
if (imce.vars.op) {
var $inputs = $('input', imce.ops[imce.vars.op].div);
$inputs.eq(0).focus();
//form inputs become invisible in IE. Solution is as stupid as the behavior.
$('html').hasClass('ie') && $inputs.addClass('dummyie').removeClass('dummyie');
}
});
});
var diff = left + $opcon.width() - $('#imce-content').width();
$opcon.css({left: diff > 0 ? left - diff - 1 : left});
$(Op.li).addClass('active');
$(imce.opCloseLink).fadeIn(300);
imce.vars.op = name;
}
Op.func(true);
return true;
},
//enable a file operation
opEnable: function(name) {
var Op = imce.ops[name];
if (Op && Op.disabled) {
Op.disabled = false;
$(Op.li).show();
}
},
//disable a file operation
opDisable: function(name) {
var Op = imce.ops[name];
if (Op && !Op.disabled) {
Op.div && imce.opShrink(name);
$(Op.li).hide();
Op.disabled = true;
}
},
//hide contents of a file operation
opShrink: function(name, effect) {
if (imce.vars.op != name) return;
var Op = imce.ops[name];
$(Op.div).stop(true, true)[effect || 'hide']();
$(Op.li).removeClass('active');
$(imce.opCloseLink).hide();
Op.func(false);
imce.vars.op = null;
},
//navigate to dir
navigate: function(dir) {
if (imce.vars.navbusy || (dir == imce.conf.dir && !confirm(Drupal.t('Do you want to refresh the current directory?')))) return;
var cache = imce.vars.cache && dir != imce.conf.dir;
var set = imce.navSet(dir, cache);
if (cache && imce.cache[dir]) {//load from the cache
set.success({data: imce.cache[dir]});
set.complete();
}
else $.ajax(set);//live load
},
//ajax navigation settings
navSet: function (dir, cache) {
$(imce.tree[dir].li).addClass('loading');
imce.vars.navbusy = dir;
return {url: imce.ajaxURL('navigate', dir),
type: 'GET',
dataType: 'json',
success: function(response) {
if (response.data && !response.data.error) {
if (cache) imce.navCache(imce.conf.dir, dir);//cache the current dir
imce.navUpdate(response.data, dir);
}
imce.processResponse(response);
},
complete: function () {
$(imce.tree[dir].li).removeClass('loading');
imce.vars.navbusy = null;
}
};
},
//update directory using the given data
navUpdate: function(data, dir) {
var cached = data == imce.cache[dir], olddir = imce.conf.dir;
if (cached) data.files.id = 'file-list';
$(imce.FLW).html(data.files);
imce.dirActivate(dir);
imce.dirSubdirs(dir, data.subdirectories);
$.extend(imce.conf.perm, data.perm);
imce.refreshOps();
imce.initiateList(cached);
imce.setPreview(imce.selcount == 1 ? imce.lastFid() : null);
imce.SBW.scrollTop = 0;
imce.invoke('navigate', data, olddir, cached);
},
//set cache
navCache: function (dir, newdir) {
var C = imce.cache[dir] = {'dir': dir, files: imce.el('file-list'), dirsize: imce.el('dir-size').innerHTML, perm: $.extend({}, imce.conf.perm)};
C.files.id = 'cached-list-'+ dir;
imce.FW.appendChild(C.files);
imce.invoke('cache', C, newdir);
},
//validate upload form
uploadValidate: function (data, form, options) {
var path = $('#edit-imce').val();
if (!path) return false;
if (imce.conf.extensions != '*') {
var ext = path.substr(path.lastIndexOf('.') + 1);
if ((' '+ imce.conf.extensions +' ').indexOf(' '+ ext.toLowerCase() +' ') == -1) {
return imce.setMessage(Drupal.t('Only files with the following extensions are allowed: %files-allowed.', {'%files-allowed': imce.conf.extensions}), 'error');
}
}
options.url = imce.ajaxURL('upload');//make url contain current dir.
imce.fopLoading('upload', true);
return true;
},
//settings for upload
uploadSettings: function () {
return {
beforeSubmit: imce.uploadValidate,
success: function (response) {
try{
imce.processResponse($.parseJSON(response));
} catch(e) {}
},
complete: function () {
imce.fopLoading('upload', false);
},
resetForm: true,
dataType: 'text'
};
},
//validate default ops(delete, thumb, resize)
fopValidate: function(fop) {
if (!imce.validateSelCount(1, imce.conf.filenum)) return false;
switch (fop) {
case 'delete':
return confirm(Drupal.t('Delete selected files?'));
case 'thumb':
if (!$('input:checked', imce.ops['thumb'].div).length) {
return imce.setMessage(Drupal.t('Please select a thumbnail.'), 'error');
}
return imce.validateImage();
case 'resize':
var w = imce.el('edit-width').value, h = imce.el('edit-height').value;
var maxDim = imce.conf.dimensions.split('x');
var maxW = maxDim[0]*1, maxH = maxW ? maxDim[1]*1 : 0;
if (!(/^[1-9][0-9]*$/).test(w) || !(/^[1-9][0-9]*$/).test(h) || (maxW && (maxW < w*1 || maxH < h*1))) {
return imce.setMessage(Drupal.t('Please specify dimensions within the allowed range that is from 1x1 to @dimensions.', {'@dimensions': maxW ? imce.conf.dimensions : Drupal.t('unlimited')}), 'error');
}
return imce.validateImage();
}
var func = fop +'OpValidate';
if (imce[func]) return imce[func](fop);
return true;
},
//submit wrapper for default ops
fopSubmit: function(fop) {
switch (fop) {
case 'thumb': case 'delete': case 'resize': return imce.commonSubmit(fop);
}
var func = fop +'OpSubmit';
if (imce[func]) return imce[func](fop);
},
//common submit function shared by default ops
commonSubmit: function(fop) {
if (!imce.fopValidate(fop)) return false;
imce.fopLoading(fop, true);
$.ajax(imce.fopSettings(fop));
},
//settings for default file operations
fopSettings: function (fop) {
return {url: imce.ajaxURL(fop), type: 'POST', dataType: 'json', success: imce.processResponse, complete: function (response) {imce.fopLoading(fop, false);}, data: imce.vars.opform +'&filenames='+ encodeURIComponent(imce.serialNames()) +'&jsop='+ fop + (imce.ops[fop].div ? '&'+ $('input, select, textarea', imce.ops[fop].div).serialize() : '')};
},
//toggle loading state
fopLoading: function(fop, state) {
var el = imce.el('edit-'+ fop), func = state ? 'addClass' : 'removeClass';
if (el) {
$(el)[func]('loading').attr('disabled', state);
}
else {
$(imce.ops[fop].li)[func]('loading');
imce.ops[fop].disabled = state;
}
},
//preview a file.
setPreview: function (fid) {
var row, html = '';
imce.vars.prvfid = fid;
if (fid && (row = imce.fids[fid])) {
var width = row.cells[2].innerHTML * 1;
html = imce.vars.previewImages && width ? imce.imgHtml(fid, width, row.cells[3].innerHTML) : imce.decodePlain(fid);
html = '<a href="#" onclick="imce.send(\''+ fid +'\'); return false;" title="'+ (imce.vars.prvtitle||'') +'">'+ html +'</a>';
}
imce.el('file-preview').innerHTML = html;
},
//default file send function. sends the file to the new window.
send: function (fid) {
fid && window.open(imce.getURL(fid));
},
//add an operation for an external application to which the files are send.
setSendTo: function (title, func) {
imce.send = function (fid) { fid && func(imce.fileGet(fid), window);};
var opFunc = function () {
if (imce.selcount != 1) return imce.setMessage(Drupal.t('Please select a file.'), 'error');
imce.send(imce.vars.prvfid);
};
imce.vars.prvtitle = title;
return imce.opAdd({name: 'sendto', title: title, func: opFunc});
},
//move initial page messages into log
prepareMsgs: function () {
var msgs;
if (msgs = imce.el('imce-messages')) {
$('>div', msgs).each(function (){
var type = this.className.split(' ')[1];
var li = $('>ul li', this);
if (li.length) li.each(function () {imce.setMessage(this.innerHTML, type);});
else imce.setMessage(this.innerHTML, type);
});
$(msgs).remove();
}
},
//insert log message
setMessage: function (msg, type) {
var $box = $(imce.msgBox);
var logs = imce.el('log-messages') || $(imce.newEl('div')).appendTo('#help-box-content').before('<h4>'+ Drupal.t('Log messages') +':</h4>').attr('id', 'log-messages')[0];
var msg = '<div class="message '+ (type || 'status') +'">'+ msg +'</div>';
$box.queue(function() {
$box.css({opacity: 0, display: 'block'}).html(msg);
$box.dequeue();
});
var q = $box.queue().length, t = imce.vars.msgT || 1000;
q = q < 2 ? 1 : q < 3 ? 0.8 : q < 4 ? 0.7 : 0.4;//adjust speed with respect to queue length
$box.fadeTo(600 * q, 1).fadeTo(t * q, 1).fadeOut(400 * q);
$(logs).append(msg);
return false;
},
//invoke hooks
invoke: function (hook) {
var i, args, func, funcs;
if ((funcs = imce.hooks[hook]) && funcs.length) {
(args = $.makeArray(arguments)).shift();
for (i = 0; func = funcs[i]; i++) func.apply(this, args);
}
},
//process response
processResponse: function (response) {
if (response.data) imce.resData(response.data);
if (response.messages) imce.resMsgs(response.messages);
},
//process response data
resData: function (data) {
var i, added, removed;
if (added = data.added) {
var cnt = imce.findex.length;
for (i in added) {//add new files or update existing
imce.fileAdd(added[i]);
}
if (added.length == 1) {//if it is a single file operation
imce.highlight(added[0].name);//highlight
}
if (imce.findex.length != cnt) {//if new files added, scroll to bottom.
$(imce.SBW).animate({scrollTop: imce.SBW.scrollHeight}).focus();
}
}
if (removed = data.removed) for (i in removed) {
imce.fileRemove(removed[i]);
}
imce.conf.dirsize = data.dirsize;
imce.updateStat();
},
//set response messages
resMsgs: function (msgs) {
for (var type in msgs) for (var i in msgs[type]) {
imce.setMessage(msgs[type][i], type);
}
},
//return img markup
imgHtml: function (fid, width, height) {
return '<img src="'+ imce.getURL(fid) +'" width="'+ width +'" height="'+ height +'" alt="'+ imce.decodePlain(fid) +'">';
},
//check if the file is an image
isImage: function (fid) {
return imce.fids[fid].cells[2].innerHTML * 1;
},
//find the first non-image in the selection
getNonImage: function (selected) {
for (var fid in selected) {
if (!imce.isImage(fid)) return fid;
}
return false;
},
//validate current selection for images
validateImage: function () {
var nonImg = imce.getNonImage(imce.selected);
return nonImg ? imce.setMessage(Drupal.t('%filename is not an image.', {'%filename': imce.decode(nonImg)}), 'error') : true;
},
//validate number of selected files
validateSelCount: function (Min, Max) {
if (Min && imce.selcount < Min) {
return imce.setMessage(Min == 1 ? Drupal.t('Please select a file.') : Drupal.t('You must select at least %num files.', {'%num': Min}), 'error');
}
if (Max && Max < imce.selcount) {
return imce.setMessage(Drupal.t('You are not allowed to operate on more than %num files.', {'%num': Max}), 'error');
}
return true;
},
//update file count and dir size
updateStat: function () {
imce.el('file-count').innerHTML = imce.findex.length;
imce.el('dir-size').innerHTML = imce.conf.dirsize;
},
//serialize selected files. return fids with a colon between them
serialNames: function () {
var str = '';
for (var fid in imce.selected) {
str += ':'+ fid;
}
return str.substr(1);
},
//get file url. re-encode & and # for mod rewrite
getURL: function (fid) {
var path = (imce.conf.dir == '.' ? '' : imce.conf.dir +'/') + fid;
return imce.conf.furl + (imce.conf.modfix ? path.replace(/%(23|26)/g, '%25$1') : path);
},
//el. by id
el: function (id) {
return document.getElementById(id);
},
//find the latest selected fid
lastFid: function () {
if (imce.vars.lastfid) return imce.vars.lastfid;
for (var fid in imce.selected);
return fid;
},
//create ajax url
ajaxURL: function (op, dir) {
return imce.conf.url + (imce.conf.clean ? '?' :'&') +'jsop='+ op +'&dir='+ (dir||imce.conf.dir);
},
//fast class check
hasC: function (el, name) {
return el.className && (' '+ el.className +' ').indexOf(' '+ name +' ') != -1;
},
//highlight a single file
highlight: function (fid) {
if (imce.vars.prvfid) imce.fileClick(imce.vars.prvfid);
imce.fileClick(fid);
},
//process a row
processRow: function (row) {
row.cells[0].innerHTML = '<span>' + imce.decodePlain(row.id) + '</span>';
row.onmousedown = function(e) {
var e = e||window.event;
imce.fileClick(this, e.ctrlKey, e.shiftKey);
return !(e.ctrlKey || e.shiftKey);
};
row.ondblclick = function(e) {
imce.send(this.id);
return false;
};
},
//decode urls. uses unescape. can be overridden to use decodeURIComponent
decode: function (str) {
try {
return decodeURIComponent(str);
} catch(e) {}
return str;
},
//decode and convert to plain text
decodePlain: function (str) {
return Drupal.checkPlain(imce.decode(str));
},
//global ajax error function
ajaxError: function (e, response, settings, thrown) {
imce.setMessage(Drupal.ajaxError(response, settings.url).replace(/\n/g, '<br />'), 'error');
},
//convert button elements to standard input buttons
convertButtons: function(form) {
$('button:submit', form).each(function(){
$(this).replaceWith('<input type="submit" value="'+ $(this).text() +'" name="'+ this.name +'" class="form-submit" id="'+ this.id +'" />');
});
},
//create element
newEl: function(name) {
return document.createElement(name);
},
//scroll syncronization for section headers
syncScroll: function(scrlEl, fixEl, bottom) {
var $fixEl = $(fixEl);
var prop = bottom ? 'bottom' : 'top';
var factor = bottom ? -1 : 1;
var syncScrl = function(el) {
$fixEl.css(prop, factor * el.scrollTop);
}
$(scrlEl).scroll(function() {
var el = this;
syncScrl(el);
setTimeout(function() {
syncScrl(el);
});
});
},
//get UI ready. provide backward compatibility.
updateUI: function() {
//file urls.
var furl = imce.conf.furl, isabs = furl.indexOf('://') > -1;
var absurls = imce.conf.absurls = imce.vars.absurls || imce.conf.absurls;
var host = location.host;
var baseurl = location.protocol + '//' + host;
if (furl.charAt(furl.length - 1) != '/') {
furl = imce.conf.furl = furl + '/';
}
imce.conf.modfix = imce.conf.clean && furl.indexOf(host + '/system/') > -1;
if (absurls && !isabs) {
imce.conf.furl = baseurl + furl;
}
else if (!absurls && isabs && furl.indexOf(baseurl) == 0) {
imce.conf.furl = furl.substr(baseurl.length);
}
//convert button elements to input elements.
imce.convertButtons(imce.FW);
//ops-list
$('#ops-list').removeClass('tabs secondary').addClass('clear-block clearfix');
imce.opCloseLink = $(imce.newEl('a')).attr({id: 'op-close-link', href: '#', title: Drupal.t('Close')}).click(function() {
imce.vars.op && imce.opClick(imce.vars.op);
return false;
}).appendTo('#op-contents')[0];
//navigation-header
if (!$('#navigation-header').length) {
$(imce.NW).children('.navigation-text').attr('id', 'navigation-header').wrapInner('<span></span>');
}
//log
$('#log-prv-wrapper').before($('#log-prv-wrapper > #preview-wrapper')).remove();
$('#log-clearer').remove();
//content resizer
$('#content-resizer').remove();
//message-box
imce.msgBox = imce.el('message-box') || $(imce.newEl('div')).attr('id', 'message-box').prependTo('#imce-content')[0];
//create help tab
var $hbox = $('#help-box');
$hbox.is('a') && $hbox.replaceWith($(imce.newEl('div')).attr('id', 'help-box').append($hbox.children()));
imce.hooks.load.push(function() {
imce.opAdd({name: 'help', title: $('#help-box-title').remove().text(), content: $('#help-box').show()});
});
//add ie classes
imce.ie && $('html').addClass('ie') && imce.ie < 8 && $('html').addClass('ie-7');
// enable box view for file list
imce.vars.boxW && imce.boxView();
//scrolling file list
imce.syncScroll(imce.SBW, '#file-header-wrapper');
imce.syncScroll(imce.SBW, '#dir-stat', true);
//scrolling directory tree
imce.syncScroll(imce.NW, '#navigation-header');
}
};
//initiate
$(document).ready(imce.initiate);
})(jQuery);