Projet

Général

Profil

Paste
Télécharger (26,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / imce / js / imce.js @ 74f6bef0

1
(function($) {
2
//Global container.
3
window.imce = {tree: {}, findex: [], fids: {}, selected: {}, selcount: 0, ops: {}, cache: {}, urlId: {},
4
vars: {previewImages: 1, cache: 1},
5
hooks: {load: [], list: [], navigate: [], cache: []},
6

    
7
//initiate imce.
8
initiate: function() {
9
  imce.conf = Drupal.settings.imce || {};
10
  imce.ie = (navigator.userAgent.match(/msie (\d+)/i) || ['', 0])[1] * 1;
11
  if (imce.conf.error != false) return;
12
  imce.FLW = imce.el('file-list-wrapper'), imce.SBW = imce.el('sub-browse-wrapper');
13
  imce.NW = imce.el('navigation-wrapper'), imce.BW = imce.el('browse-wrapper');
14
  imce.PW = imce.el('preview-wrapper'), imce.FW = imce.el('forms-wrapper');
15
  imce.updateUI();
16
  imce.prepareMsgs();//process initial status messages
17
  imce.initiateTree();//build directory tree
18
  imce.hooks.list.unshift(imce.processRow);//set the default list-hook.
19
  imce.initiateList();//process file list
20
  imce.initiateOps();//prepare operation tabs
21
  imce.refreshOps();
22
  imce.invoke('load', window);//run functions set by external applications.
23
},
24

    
25
//process navigation tree
26
initiateTree: function() {
27
  $('#navigation-tree li').each(function(i) {
28
    var a = this.firstChild, txt = a.firstChild;
29
    txt && (txt.data = imce.decode(txt.data));
30
    var branch = imce.tree[a.title] = {'a': a, li: this, ul: this.lastChild.tagName == 'UL' ? this.lastChild : null};
31
    if (a.href) imce.dirClickable(branch);
32
    imce.dirCollapsible(branch);
33
  });
34
},
35

    
36
//Add a dir to the tree under parent
37
dirAdd: function(dir, parent, clickable) {
38
  if (imce.tree[dir]) return clickable ? imce.dirClickable(imce.tree[dir]) : imce.tree[dir];
39
  var parent = parent || imce.tree['.'];
40
  parent.ul = parent.ul ? parent.ul : parent.li.appendChild(imce.newEl('ul'));
41
  var branch = imce.dirCreate(dir, imce.decode(dir.substr(dir.lastIndexOf('/')+1)), clickable);
42
  parent.ul.appendChild(branch.li);
43
  return branch;
44
},
45

    
46
//create list item for navigation tree
47
dirCreate: function(dir, text, clickable) {
48
  if (imce.tree[dir]) return imce.tree[dir];
49
  var branch = imce.tree[dir] = {li: imce.newEl('li'), a: imce.newEl('a')};
50
  $(branch.a).addClass('folder').text(text).attr('title', dir).appendTo(branch.li);
51
  imce.dirCollapsible(branch);
52
  return clickable ? imce.dirClickable(branch) : branch;
53
},
54

    
55
//change currently active directory
56
dirActivate: function(dir) {
57
  if (dir != imce.conf.dir) {
58
    if (imce.tree[imce.conf.dir]){
59
      $(imce.tree[imce.conf.dir].a).removeClass('active');
60
    }
61
    $(imce.tree[dir].a).addClass('active');
62
    imce.conf.dir = dir;
63
  }
64
  return imce.tree[imce.conf.dir];
65
},
66

    
67
//make a dir accessible
68
dirClickable: function(branch) {
69
  if (branch.clkbl) return branch;
70
  $(branch.a).attr('href', '#').removeClass('disabled').click(function() {imce.navigate(this.title); return false;});
71
  branch.clkbl = true;
72
  return branch;
73
},
74

    
75
//sub-directories expand-collapse ability
76
dirCollapsible: function (branch) {
77
  if (branch.clpsbl) return branch;
78
  $(imce.newEl('span')).addClass('expander').html(' ').click(function() {
79
    if (branch.ul) {
80
      $(branch.ul).toggle();
81
      $(branch.li).toggleClass('expanded');
82
      imce.ie && $('#navigation-header').css('top', imce.NW.scrollTop);
83
    }
84
    else if (branch.clkbl){
85
      $(branch.a).click();
86
    }
87
  }).prependTo(branch.li);
88
  branch.clpsbl = true;
89
  return branch;
90
},
91

    
92
//update navigation tree after getting subdirectories.
93
dirSubdirs: function(dir, subdirs) {
94
  var branch = imce.tree[dir];
95
  if (subdirs && subdirs.length) {
96
    var prefix = dir == '.' ? '' : dir +'/';
97
    for (var i in subdirs) {//add subdirectories
98
      imce.dirAdd(prefix + subdirs[i], branch, true);
99
    }
100
    $(branch.li).removeClass('leaf').addClass('expanded');
101
    $(branch.ul).show();
102
  }
103
  else if (!branch.ul){//no subdirs->leaf
104
    $(branch.li).removeClass('expanded').addClass('leaf');
105
  }
106
},
107

    
108
//process file list
109
initiateList: function(cached) {
110
  var L = imce.hooks.list, dir = imce.conf.dir, token = {'%dir':  dir == '.' ? $(imce.tree['.'].a).text() : imce.decode(dir)}
111
  imce.findex = [], imce.fids = {}, imce.selected = {}, imce.selcount = 0, imce.vars.lastfid = null;
112
  imce.tbody = imce.el('file-list').tBodies[0];
113
  if (imce.tbody.rows.length) {
114
    for (var row, i = 0; row = imce.tbody.rows[i]; i++) {
115
      var fid = row.id;
116
      imce.findex[i] = imce.fids[fid] = row;
117
      if (cached) {
118
        if (imce.hasC(row, 'selected')) {
119
          imce.selected[imce.vars.lastfid = fid] = row;
120
          imce.selcount++;
121
        }
122
      }
123
      else {
124
        for (var func, j = 0; func = L[j]; j++) func(row);//invoke list-hook
125
      }
126
    }
127
  }
128
  if (!imce.conf.perm.browse) {
129
    imce.setMessage(Drupal.t('File browsing is disabled in directory %dir.', token), 'error');
130
  }
131
},
132

    
133
//add a file to the list. (having properties name,size,formatted size,width,height,date,formatted date)
134
fileAdd: function(file) {
135
  var row, fid = file.name, i = imce.findex.length, attr = ['name', 'size', 'width', 'height', 'date'];
136
  if (!(row = imce.fids[fid])) {
137
    row = imce.findex[i] = imce.fids[fid] = imce.tbody.insertRow(i);
138
    for (var i in attr) row.insertCell(i).className = attr[i];
139
  }
140
  row.cells[0].innerHTML = row.id = fid;
141
  row.cells[1].innerHTML = file.fsize; row.cells[1].id = file.size;
142
  row.cells[2].innerHTML = file.width;
143
  row.cells[3].innerHTML = file.height;
144
  row.cells[4].innerHTML = file.fdate; row.cells[4].id = file.date;
145
  imce.invoke('list', row);
146
  if (imce.vars.prvfid == fid) imce.setPreview(fid);
147
  if (file.id) imce.urlId[imce.getURL(fid)] = file.id;
148
},
149

    
150
//remove a file from the list
151
fileRemove: function(fid) {
152
  if (!(row = imce.fids[fid])) return;
153
  imce.fileDeSelect(fid);
154
  imce.findex.splice(row.rowIndex, 1);
155
  $(row).remove();
156
  delete imce.fids[fid];
157
  if (imce.vars.prvfid == fid) imce.setPreview();
158
},
159

    
160
//return a file object containing all properties.
161
fileGet: function (fid) {
162
  var row = imce.fids[fid];
163
  var url = imce.getURL(fid);
164
  return row ? {
165
    name: imce.decode(fid),
166
    url: url,
167
    size: row.cells[1].innerHTML,
168
    bytes: row.cells[1].id * 1,
169
    width: row.cells[2].innerHTML * 1,
170
    height: row.cells[3].innerHTML * 1,
171
    date: row.cells[4].innerHTML,
172
    time: row.cells[4].id * 1,
173
    id: imce.urlId[url] || 0, //file id for newly uploaded files
174
    relpath: (imce.conf.dir == '.' ? '' : imce.conf.dir +'/') + fid //rawurlencoded path relative to file directory path.
175
  } : null;
176
},
177

    
178
//simulate row click. selection-highlighting
179
fileClick: function(row, ctrl, shft) {
180
  if (!row) return;
181
  var fid = typeof(row) == 'string' ? row : row.id;
182
  if (ctrl || fid == imce.vars.prvfid) {
183
    imce.fileToggleSelect(fid);
184
  }
185
  else if (shft) {
186
    var last = imce.lastFid();
187
    var start = last ? imce.fids[last].rowIndex : -1;
188
    var end = imce.fids[fid].rowIndex;
189
    var step = start > end ? -1 : 1;
190
    while (start != end) {
191
      start += step;
192
      imce.fileSelect(imce.findex[start].id);
193
    }
194
  }
195
  else {
196
    for (var fname in imce.selected) {
197
      imce.fileDeSelect(fname);
198
    }
199
    imce.fileSelect(fid);
200
  }
201
  //set preview
202
  imce.setPreview(imce.selcount == 1 ? imce.lastFid() : null);
203
},
204

    
205
//file select/deselect functions
206
fileSelect: function (fid) {
207
  if (imce.selected[fid] || !imce.fids[fid]) return;
208
  imce.selected[fid] = imce.fids[imce.vars.lastfid=fid];
209
  $(imce.selected[fid]).addClass('selected');
210
  imce.selcount++;
211
},
212
fileDeSelect: function (fid) {
213
  if (!imce.selected[fid] || !imce.fids[fid]) return;
214
  if (imce.vars.lastfid == fid) imce.vars.lastfid = null;
215
  $(imce.selected[fid]).removeClass('selected');
216
  delete imce.selected[fid];
217
  imce.selcount--;
218
},
219
fileToggleSelect: function (fid) {
220
  imce['file'+ (imce.selected[fid] ? 'De' : '') +'Select'](fid);
221
},
222

    
223
//process file operation form and create operation tabs.
224
initiateOps: function() {
225
  imce.setHtmlOps();
226
  imce.setUploadOp();//upload
227
  imce.setFileOps();//thumb, delete, resize
228
},
229

    
230
//process existing html ops.
231
setHtmlOps: function () {
232
  $(imce.el('ops-list')).children('li').each(function() {
233
    if (!this.firstChild) return $(this).remove();
234
    var name = this.id.substr(8);
235
    var Op = imce.ops[name] = {div: imce.el('op-content-'+ name), li: imce.el('op-item-'+ name)};
236
    Op.a = Op.li.firstChild;
237
    Op.title = Op.a.innerHTML;
238
    $(Op.a).click(function() {imce.opClick(name); return false;});
239
  });
240
},
241

    
242
//convert upload form to an op.
243
setUploadOp: function () {
244
  var form = imce.el('imce-upload-form');
245
  if (!form) return;
246
  $(form).ajaxForm(imce.uploadSettings()).find('fieldset').each(function() {//clean up fieldsets
247
    this.removeChild(this.firstChild);
248
    $(this).after(this.childNodes);
249
  }).remove();
250
  imce.opAdd({name: 'upload', title: Drupal.t('Upload'), content: form});//add op
251
},
252

    
253
//convert fileop form submit buttons to ops.
254
setFileOps: function () {
255
  var form = imce.el('imce-fileop-form');
256
  if (!form) return;
257
  $(form.elements.filenames).parent().remove();
258
  $(form).find('fieldset').each(function() {//remove fieldsets
259
    var $sbmt = $('input:submit', this);
260
    if (!$sbmt.size()) return;
261
    var Op = {name: $sbmt.attr('id').substr(5)};
262
    var func = function() {imce.fopSubmit(Op.name); return false;};
263
    $sbmt.click(func);
264
    Op.title = $(this).children('legend').remove().text() || $sbmt.val();
265
    Op.name == 'delete' ? (Op.func = func) : (Op.content = this.childNodes);
266
    imce.opAdd(Op);
267
  }).remove();
268
  imce.vars.opform = $(form).serialize();//serialize remaining parts.
269
},
270

    
271
//refresh ops states. enable/disable
272
refreshOps: function() {
273
  for (var p in imce.conf.perm) {
274
    if (imce.conf.perm[p]) imce.opEnable(p);
275
    else imce.opDisable(p);
276
  }
277
},
278

    
279
//add a new file operation
280
opAdd: function (op) {
281
  var oplist = imce.el('ops-list'), opcons = imce.el('op-contents');
282
  var name = op.name || ('op-'+ $(oplist).children('li').size());
283
  var title = op.title || 'Untitled';
284
  var Op = imce.ops[name] = {title: title};
285
  if (op.content) {
286
    Op.div = imce.newEl('div');
287
    $(Op.div).attr({id: 'op-content-'+ name, 'class': 'op-content'}).appendTo(opcons).append(op.content);
288
  }
289
  Op.a = imce.newEl('a');
290
  Op.li = imce.newEl('li');
291
  $(Op.a).attr({href: '#', name: name, title: title}).html('<span>' + title +'</span>').click(imce.opClickEvent);
292
  $(Op.li).attr('id', 'op-item-'+ name).append(Op.a).appendTo(oplist);
293
  Op.func = op.func || imce.opVoid;
294
  return Op;
295
},
296

    
297
//click event for file operations
298
opClickEvent: function(e) {
299
  imce.opClick(this.name);
300
  return false;
301
},
302

    
303
//void operation function
304
opVoid: function() {},
305

    
306
//perform op click
307
opClick: function(name) {
308
  var Op = imce.ops[name], oldop = imce.vars.op;
309
  if (!Op || Op.disabled) {
310
    return imce.setMessage(Drupal.t('You can not perform this operation.'), 'error');
311
  }
312
  if (Op.div) {
313
    if (oldop) {
314
      var toggle = oldop == name;
315
      imce.opShrink(oldop, toggle ? 'fadeOut' : 'hide');
316
      if (toggle) return false;
317
    }
318
    var left = Op.li.offsetLeft;
319
    var $opcon = $('#op-contents').css({left: 0});
320
    $(Op.div).fadeIn('normal', function() {
321
      setTimeout(function() {
322
        if (imce.vars.op) {
323
          var $inputs = $('input', imce.ops[imce.vars.op].div);
324
          $inputs.eq(0).focus();
325
          //form inputs become invisible in IE. Solution is as stupid as the behavior.
326
          $('html').is('.ie') && $inputs.addClass('dummyie').removeClass('dummyie');
327
       }
328
      });
329
    });
330
    var diff = left + $opcon.width() - $('#imce-content').width();
331
    $opcon.css({left: diff > 0 ? left - diff - 1 : left});
332
    $(Op.li).addClass('active');
333
    $(imce.opCloseLink).fadeIn(300);
334
    imce.vars.op = name;
335
  }
336
  Op.func(true);
337
  return true;
338
},
339

    
340
//enable a file operation
341
opEnable: function(name) {
342
  var Op = imce.ops[name];
343
  if (Op && Op.disabled) {
344
    Op.disabled = false;
345
    $(Op.li).show();
346
  }
347
},
348

    
349
//disable a file operation
350
opDisable: function(name) {
351
  var Op = imce.ops[name];
352
  if (Op && !Op.disabled) {
353
    Op.div && imce.opShrink(name);
354
    $(Op.li).hide();
355
    Op.disabled = true;
356
  }
357
},
358

    
359
//hide contents of a file operation
360
opShrink: function(name, effect) {
361
  if (imce.vars.op != name) return;
362
  var Op = imce.ops[name];
363
  $(Op.div).stop(true, true)[effect || 'hide']();
364
  $(Op.li).removeClass('active');
365
  $(imce.opCloseLink).hide();
366
  Op.func(false);
367
  imce.vars.op = null;
368
},
369

    
370
//navigate to dir
371
navigate: function(dir) {
372
  if (imce.vars.navbusy || (dir == imce.conf.dir && !confirm(Drupal.t('Do you want to refresh the current directory?')))) return;
373
  var cache = imce.vars.cache && dir != imce.conf.dir;
374
  var set = imce.navSet(dir, cache);
375
  if (cache && imce.cache[dir]) {//load from the cache
376
    set.success({data: imce.cache[dir]});
377
    set.complete();
378
  }
379
  else $.ajax(set);//live load
380
},
381
//ajax navigation settings
382
navSet: function (dir, cache) {
383
  $(imce.tree[dir].li).addClass('loading');
384
  imce.vars.navbusy = dir;
385
  return {url: imce.ajaxURL('navigate', dir),
386
  type: 'GET',
387
  dataType: 'json',
388
  success: function(response) {
389
    if (response.data && !response.data.error) {
390
      if (cache) imce.navCache(imce.conf.dir, dir);//cache the current dir
391
      imce.navUpdate(response.data, dir);
392
    }
393
    imce.processResponse(response);
394
  },
395
  complete: function () {
396
    $(imce.tree[dir].li).removeClass('loading');
397
    imce.vars.navbusy = null;
398
  }
399
  };
400
},
401

    
402
//update directory using the given data
403
navUpdate: function(data, dir) {
404
  var cached = data == imce.cache[dir], olddir = imce.conf.dir;
405
  if (cached) data.files.id = 'file-list';
406
  $(imce.FLW).html(data.files);
407
  imce.dirActivate(dir);
408
  imce.dirSubdirs(dir, data.subdirectories);
409
  $.extend(imce.conf.perm, data.perm);
410
  imce.refreshOps();
411
  imce.initiateList(cached);
412
  imce.setPreview(imce.selcount == 1 ? imce.lastFid() : null);
413
  imce.SBW.scrollTop = 0;
414
  imce.invoke('navigate', data, olddir, cached);
415
},
416

    
417
//set cache
418
navCache: function (dir, newdir) {
419
  var C = imce.cache[dir] = {'dir': dir, files: imce.el('file-list'), dirsize: imce.el('dir-size').innerHTML, perm: $.extend({}, imce.conf.perm)};
420
  C.files.id = 'cached-list-'+ dir;
421
  imce.FW.appendChild(C.files);
422
  imce.invoke('cache', C, newdir);
423
},
424

    
425
//validate upload form
426
uploadValidate: function (data, form, options) {
427
  var path = $('#edit-imce').val();
428
  if (!path) return false;
429
  if (imce.conf.extensions != '*') {
430
    var ext = path.substr(path.lastIndexOf('.') + 1);
431
    if ((' '+ imce.conf.extensions +' ').indexOf(' '+ ext.toLowerCase() +' ') == -1) {
432
      return imce.setMessage(Drupal.t('Only files with the following extensions are allowed: %files-allowed.', {'%files-allowed': imce.conf.extensions}), 'error');
433
    }
434
  }
435
  options.url = imce.ajaxURL('upload');//make url contain current dir.
436
  imce.fopLoading('upload', true);
437
  return true;
438
},
439

    
440
//settings for upload
441
uploadSettings: function () {
442
  return {beforeSubmit: imce.uploadValidate, success: function (response) {imce.processResponse($.parseJSON(response));}, complete: function () {imce.fopLoading('upload', false);}, resetForm: true};
443
},
444

    
445
//validate default ops(delete, thumb, resize)
446
fopValidate: function(fop) {
447
  if (!imce.validateSelCount(1, imce.conf.filenum)) return false;
448
  switch (fop) {
449
    case 'delete':
450
      return confirm(Drupal.t('Delete selected files?'));
451
    case 'thumb':
452
      if (!$('input:checked', imce.ops['thumb'].div).size()) {
453
        return imce.setMessage(Drupal.t('Please select a thumbnail.'), 'error');
454
      }
455
      return imce.validateImage();
456
    case 'resize':
457
      var w = imce.el('edit-width').value, h = imce.el('edit-height').value;
458
      var maxDim = imce.conf.dimensions.split('x');
459
      var maxW = maxDim[0]*1, maxH = maxW ? maxDim[1]*1 : 0;
460
      if (!(/^[1-9][0-9]*$/).test(w) || !(/^[1-9][0-9]*$/).test(h) || (maxW && (maxW < w*1 || maxH < h*1))) {
461
        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');
462
      }
463
      return imce.validateImage();
464
  }
465

    
466
  var func = fop +'OpValidate';
467
  if (imce[func]) return imce[func](fop);
468
  return true;
469
},
470

    
471
//submit wrapper for default ops
472
fopSubmit: function(fop) {
473
  switch (fop) {
474
    case 'thumb': case 'delete': case 'resize':  return imce.commonSubmit(fop);
475
  }
476
  var func = fop +'OpSubmit';
477
  if (imce[func]) return imce[func](fop);
478
},
479

    
480
//common submit function shared by default ops
481
commonSubmit: function(fop) {
482
  if (!imce.fopValidate(fop)) return false;
483
  imce.fopLoading(fop, true);
484
  $.ajax(imce.fopSettings(fop));
485
},
486

    
487
//settings for default file operations
488
fopSettings: function (fop) {
489
  return {url: imce.ajaxURL(fop), type: 'POST', dataType: 'json', success: imce.processResponse, complete: function (response) {imce.fopLoading(fop, false);}, data: imce.vars.opform +'&filenames='+ escape(imce.serialNames()) +'&jsop='+ fop + (imce.ops[fop].div ? '&'+ $('input, select, textarea', imce.ops[fop].div).serialize() : '')};
490
},
491

    
492
//toggle loading state
493
fopLoading: function(fop, state) {
494
  var el = imce.el('edit-'+ fop), func = state ? 'addClass' : 'removeClass';
495
  if (el) {
496
    $(el)[func]('loading').attr('disabled', state);
497
  }
498
  else {
499
    $(imce.ops[fop].li)[func]('loading');
500
    imce.ops[fop].disabled = state;
501
  }
502
},
503

    
504
//preview a file.
505
setPreview: function (fid) {
506
  var row, html = '';
507
  imce.vars.prvfid = fid;
508
  if (fid && (row = imce.fids[fid])) {
509
    var width = row.cells[2].innerHTML * 1;
510
    html = imce.vars.previewImages && width ? imce.imgHtml(fid, width, row.cells[3].innerHTML) : imce.decodePlain(fid);
511
    html = '<a href="#" onclick="imce.send(\''+ fid +'\'); return false;" title="'+ (imce.vars.prvtitle||'') +'">'+ html +'</a>';
512
  }
513
  imce.el('file-preview').innerHTML = html;
514
},
515

    
516
//default file send function. sends the file to the new window.
517
send: function (fid) {
518
  fid && window.open(imce.getURL(fid));
519
},
520

    
521
//add an operation for an external application to which the files are send.
522
setSendTo: function (title, func) {
523
  imce.send = function (fid) { fid && func(imce.fileGet(fid), window);};
524
  var opFunc = function () {
525
    if (imce.selcount != 1) return imce.setMessage(Drupal.t('Please select a file.'), 'error');
526
    imce.send(imce.vars.prvfid);
527
  };
528
  imce.vars.prvtitle = title;
529
  return imce.opAdd({name: 'sendto', title: title, func: opFunc});
530
},
531

    
532
//move initial page messages into log
533
prepareMsgs: function () {
534
  var msgs;
535
  if (msgs = imce.el('imce-messages')) {
536
    $('>div', msgs).each(function (){
537
      var type = this.className.split(' ')[1];
538
      var li = $('>ul li', this);
539
      if (li.size()) li.each(function () {imce.setMessage(this.innerHTML, type);});
540
      else imce.setMessage(this.innerHTML, type);
541
    });
542
    $(msgs).remove();
543
  }
544
},
545

    
546
//insert log message
547
setMessage: function (msg, type) {
548
  var $box = $(imce.msgBox);
549
  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];
550
  var msg = '<div class="message '+ (type || 'status') +'">'+ msg +'</div>';
551
  $box.queue(function() {
552
    $box.css({opacity: 0, display: 'block'}).html(msg);
553
    $box.dequeue();
554
  });
555
  var q = $box.queue().length, t = imce.vars.msgT || 1000;
556
  q = q < 2 ? 1 : q < 3 ? 0.8 : q < 4 ? 0.7 : 0.4;//adjust speed with respect to queue length
557
  $box.fadeTo(600 * q, 1).fadeTo(t * q, 1).fadeOut(400 * q);
558
  $(logs).append(msg);
559
  return false;
560
},
561

    
562
//invoke hooks
563
invoke: function (hook) {
564
  var i, args, func, funcs;
565
  if ((funcs = imce.hooks[hook]) && funcs.length) {
566
    (args = $.makeArray(arguments)).shift();
567
    for (i = 0; func = funcs[i]; i++) func.apply(this, args);
568
  }
569
},
570

    
571
//process response
572
processResponse: function (response) {
573
  if (response.data) imce.resData(response.data);
574
  if (response.messages) imce.resMsgs(response.messages);
575
},
576

    
577
//process response data
578
resData: function (data) {
579
  var i, added, removed;
580
  if (added = data.added) {
581
    var cnt = imce.findex.length;
582
    for (i in added) {//add new files or update existing
583
      imce.fileAdd(added[i]);
584
    }
585
    if (added.length == 1) {//if it is a single file operation
586
      imce.highlight(added[0].name);//highlight
587
    }
588
    if (imce.findex.length != cnt) {//if new files added, scroll to bottom.
589
      $(imce.SBW).animate({scrollTop: imce.SBW.scrollHeight}).focus();
590
    }
591
  }
592
  if (removed = data.removed) for (i in removed) {
593
    imce.fileRemove(removed[i]);
594
  }
595
  imce.conf.dirsize = data.dirsize;
596
  imce.updateStat();
597
},
598

    
599
//set response messages
600
resMsgs: function (msgs) {
601
  for (var type in msgs) for (var i in msgs[type]) {
602
    imce.setMessage(msgs[type][i], type);
603
  }
604
},
605

    
606
//return img markup
607
imgHtml: function (fid, width, height) {
608
  return '<img src="'+ imce.getURL(fid) +'" width="'+ width +'" height="'+ height +'" alt="'+ imce.decodePlain(fid) +'">';
609
},
610

    
611
//check if the file is an image
612
isImage: function (fid) {
613
  return imce.fids[fid].cells[2].innerHTML * 1;
614
},
615

    
616
//find the first non-image in the selection
617
getNonImage: function (selected) {
618
  for (var fid in selected) {
619
    if (!imce.isImage(fid)) return fid;
620
  }
621
  return false;
622
},
623

    
624
//validate current selection for images
625
validateImage: function () {
626
  var nonImg = imce.getNonImage(imce.selected);
627
  return nonImg ? imce.setMessage(Drupal.t('%filename is not an image.', {'%filename': imce.decode(nonImg)}), 'error') : true;
628
},
629

    
630
//validate number of selected files
631
validateSelCount: function (Min, Max) {
632
  if (Min && imce.selcount < Min) {
633
    return imce.setMessage(Min == 1 ? Drupal.t('Please select a file.') : Drupal.t('You must select at least %num files.', {'%num': Min}), 'error');
634
  }
635
  if (Max && Max < imce.selcount) {
636
    return imce.setMessage(Drupal.t('You are not allowed to operate on more than %num files.', {'%num': Max}), 'error');
637
  }
638
  return true;
639
},
640

    
641
//update file count and dir size
642
updateStat: function () {
643
  imce.el('file-count').innerHTML = imce.findex.length;
644
  imce.el('dir-size').innerHTML = imce.conf.dirsize;
645
},
646

    
647
//serialize selected files. return fids with a colon between them
648
serialNames: function () {
649
  var str = '';
650
  for (var fid in imce.selected) {
651
    str += ':'+ fid;
652
  }
653
  return str.substr(1);
654
},
655

    
656
//get file url. re-encode & and # for mod rewrite
657
getURL: function (fid) {
658
  var path = (imce.conf.dir == '.' ? '' : imce.conf.dir +'/') + fid;
659
  return imce.conf.furl + (imce.conf.modfix ? path.replace(/%(23|26)/g, '%25$1') : path);
660
},
661

    
662
//el. by id
663
el: function (id) {
664
  return document.getElementById(id);
665
},
666

    
667
//find the latest selected fid
668
lastFid: function () {
669
  if (imce.vars.lastfid) return imce.vars.lastfid;
670
  for (var fid in imce.selected);
671
  return fid;
672
},
673

    
674
//create ajax url
675
ajaxURL: function (op, dir) {
676
  return imce.conf.url + (imce.conf.clean ? '?' :'&') +'jsop='+ op +'&dir='+ (dir||imce.conf.dir);
677
},
678

    
679
//fast class check
680
hasC: function (el, name) {
681
  return el.className && (' '+ el.className +' ').indexOf(' '+ name +' ') != -1;
682
},
683

    
684
//highlight a single file
685
highlight: function (fid) {
686
  if (imce.vars.prvfid) imce.fileClick(imce.vars.prvfid);
687
  imce.fileClick(fid);
688
},
689

    
690
//process a row
691
processRow: function (row) {
692
  row.cells[0].innerHTML = '<span>' + imce.decodePlain(row.id) + '</span>';
693
  row.onmousedown = function(e) {
694
    var e = e||window.event;
695
    imce.fileClick(this, e.ctrlKey, e.shiftKey);
696
    return !(e.ctrlKey || e.shiftKey);
697
  };
698
  row.ondblclick = function(e) {
699
    imce.send(this.id);
700
    return false;
701
  };
702
},
703

    
704
//decode urls. uses unescape. can be overridden to use decodeURIComponent
705
decode: function (str) {
706
  return unescape(str);
707
},
708

    
709
//decode and convert to plain text
710
decodePlain: function (str) {
711
  return Drupal.checkPlain(imce.decode(str));
712
},
713

    
714
//global ajax error function
715
ajaxError: function (e, response, settings, thrown) {
716
  imce.setMessage(Drupal.ajaxError(response, settings.url).replace(/\n/g, '<br />'), 'error');
717
},
718

    
719
//convert button elements to standard input buttons
720
convertButtons: function(form) {
721
  $('button:submit', form).each(function(){
722
    $(this).replaceWith('<input type="submit" value="'+ $(this).text() +'" name="'+ this.name +'" class="form-submit" id="'+ this.id +'" />');
723
  });
724
},
725

    
726
//create element
727
newEl: function(name) {
728
  return document.createElement(name);
729
},
730

    
731
//scroll syncronization for section headers
732
syncScroll: function(scrlEl, fixEl, bottom) {
733
  var $fixEl = $(fixEl);
734
  var prop = bottom ? 'bottom' : 'top';
735
  var factor = bottom ? -1 : 1;
736
  var syncScrl = function(el) {
737
    $fixEl.css(prop, factor * el.scrollTop);
738
  }
739
  $(scrlEl).scroll(function() {
740
    var el = this;
741
    syncScrl(el);
742
    setTimeout(function() {
743
      syncScrl(el);
744
    });
745
  });
746
},
747

    
748
//get UI ready. provide backward compatibility.
749
updateUI: function() {
750
  //file urls.
751
  var furl = imce.conf.furl, isabs = furl.indexOf('://') > -1;
752
  var absurls = imce.conf.absurls = imce.vars.absurls || imce.conf.absurls;
753
  var host = location.host;
754
  var baseurl = location.protocol + '//' + host;
755
  if (furl.charAt(furl.length - 1) != '/') {
756
    furl = imce.conf.furl = furl + '/';
757
  }
758
  imce.conf.modfix = imce.conf.clean && furl.indexOf(host + '/system/') > -1;
759
  if (absurls && !isabs) {
760
    imce.conf.furl = baseurl + furl;
761
  }
762
  else if (!absurls && isabs && furl.indexOf(baseurl) == 0) {
763
    imce.conf.furl = furl.substr(baseurl.length);
764
  }
765
  //convert button elements to input elements.
766
  imce.convertButtons(imce.FW);
767
  //ops-list
768
  $('#ops-list').removeClass('tabs secondary').addClass('clear-block clearfix');
769
  imce.opCloseLink = $(imce.newEl('a')).attr({id: 'op-close-link', href: '#', title: Drupal.t('Close')}).click(function() {
770
    imce.vars.op && imce.opClick(imce.vars.op);
771
    return false;
772
  }).appendTo('#op-contents')[0];
773
  //navigation-header
774
  if (!$('#navigation-header').size()) {
775
    $(imce.NW).children('.navigation-text').attr('id', 'navigation-header').wrapInner('<span></span>');
776
  }
777
  //log
778
  $('#log-prv-wrapper').before($('#log-prv-wrapper > #preview-wrapper')).remove();
779
  $('#log-clearer').remove();
780
  //content resizer
781
  $('#content-resizer').remove();
782
  //message-box
783
  imce.msgBox = imce.el('message-box') || $(imce.newEl('div')).attr('id', 'message-box').prependTo('#imce-content')[0];
784
  //create help tab
785
  var $hbox = $('#help-box');
786
  $hbox.is('a') && $hbox.replaceWith($(imce.newEl('div')).attr('id', 'help-box').append($hbox.children()));
787
  imce.hooks.load.push(function() {
788
    imce.opAdd({name: 'help', title: $('#help-box-title').remove().text(), content: $('#help-box').show()});
789
  });
790
  //add ie classes
791
  imce.ie && $('html').addClass('ie') && imce.ie < 8 && $('html').addClass('ie-7');
792
  // enable box view for file list
793
  imce.vars.boxW && imce.boxView();
794
  //scrolling file list
795
  imce.syncScroll(imce.SBW, '#file-header-wrapper');
796
  imce.syncScroll(imce.SBW, '#dir-stat', true);
797
  //scrolling directory tree
798
  imce.syncScroll(imce.NW, '#navigation-header');
799
}
800

    
801
};
802

    
803
//initiate
804
$(document).ready(imce.initiate).ajaxError(imce.ajaxError);
805

    
806
})(jQuery);