Projet

Général

Profil

Paste
Télécharger (27,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / imce / js / imce.js @ 05237dd8

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
  if (imce.conf.error != false) return;
11
  imce.ie = (navigator.userAgent.match(/msie (\d+)/i) || ['', 0])[1] * 1;
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
  // Bind global error handler
23
  $(document).ajaxError(imce.ajaxError);
24
  imce.invoke('load', window);//run functions set by external applications.
25
},
26

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

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

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

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

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

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

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

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

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

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

    
162
//return a file object containing all properties.
163
fileGet: function (fid) {
164
  var file = imce.fileProps(fid);
165
  if (file) {
166
    file.name = imce.decode(fid);
167
    file.url = imce.getURL(fid);
168
    file.relpath = imce.getRelpath(fid);
169
    file.id = imce.urlId[file.url] || 0; //file id for newly uploaded files
170
  }
171
  return file;
172
},
173

    
174
//return file properties embedded in html.
175
fileProps: function (fid) {
176
  var row = imce.fids[fid];
177
  return row ? {
178
    size: row.cells[1].innerHTML,
179
    bytes: row.cells[1].id * 1,
180
    width: row.cells[2].innerHTML * 1,
181
    height: row.cells[3].innerHTML * 1,
182
    date: row.cells[4].innerHTML,
183
    time: row.cells[4].id * 1
184
  } : null;
185
},
186

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

    
214
//file select/deselect functions
215
fileSelect: function (fid) {
216
  if (imce.selected[fid] || !imce.fids[fid]) return;
217
  imce.selected[fid] = imce.fids[imce.vars.lastfid=fid];
218
  $(imce.selected[fid]).addClass('selected');
219
  imce.selcount++;
220
},
221
fileDeSelect: function (fid) {
222
  if (!imce.selected[fid] || !imce.fids[fid]) return;
223
  if (imce.vars.lastfid == fid) imce.vars.lastfid = null;
224
  $(imce.selected[fid]).removeClass('selected');
225
  delete imce.selected[fid];
226
  imce.selcount--;
227
},
228
fileToggleSelect: function (fid) {
229
  imce['file'+ (imce.selected[fid] ? 'De' : '') +'Select'](fid);
230
},
231

    
232
//process file operation form and create operation tabs.
233
initiateOps: function() {
234
  imce.setHtmlOps();
235
  imce.setUploadOp();//upload
236
  imce.setFileOps();//thumb, delete, resize
237
},
238

    
239
//process existing html ops.
240
setHtmlOps: function () {
241
  $(imce.el('ops-list')).children('li').each(function() {
242
    if (!this.firstChild) return $(this).remove();
243
    var name = this.id.substr(8);
244
    var Op = imce.ops[name] = {div: imce.el('op-content-'+ name), li: imce.el('op-item-'+ name)};
245
    Op.a = Op.li.firstChild;
246
    Op.title = Op.a.innerHTML;
247
    $(Op.a).click(function() {imce.opClick(name); return false;});
248
  });
249
},
250

    
251
//convert upload form to an op.
252
setUploadOp: function () {
253
  var el, form = imce.el('imce-upload-form');
254
  if (!form) return;
255
  $(form).ajaxForm(imce.uploadSettings()).find('fieldset').each(function() {//clean up fieldsets
256
    this.removeChild(this.firstChild);
257
    $(this).after(this.childNodes);
258
  }).remove();
259
  // Set html response flag
260
  el = form.elements['files[imce]'];
261
  if (el && el.files && window.FormData) {
262
    if (el = form.elements.html_response) {
263
      el.value = 0;
264
    }
265
  } 
266
  imce.opAdd({name: 'upload', title: Drupal.t('Upload'), content: form});//add op
267
},
268

    
269
//convert fileop form submit buttons to ops.
270
setFileOps: function () {
271
  var form = imce.el('imce-fileop-form');
272
  if (!form) return;
273
  $(form.elements.filenames).parent().remove();
274
  $(form).find('fieldset').each(function() {//remove fieldsets
275
    var $sbmt = $('input:submit', this);
276
    if (!$sbmt.length) return;
277
    var Op = {name: $sbmt.attr('id').substr(5)};
278
    var func = function() {imce.fopSubmit(Op.name); return false;};
279
    $sbmt.click(func);
280
    Op.title = $(this).children('legend').remove().text() || $sbmt.val();
281
    Op.name == 'delete' ? (Op.func = func) : (Op.content = this.childNodes);
282
    imce.opAdd(Op);
283
  }).remove();
284
  imce.vars.opform = $(form).serialize();//serialize remaining parts.
285
},
286

    
287
//refresh ops states. enable/disable
288
refreshOps: function() {
289
  for (var p in imce.conf.perm) {
290
    if (imce.conf.perm[p]) imce.opEnable(p);
291
    else imce.opDisable(p);
292
  }
293
},
294

    
295
//add a new file operation
296
opAdd: function (op) {
297
  var oplist = imce.el('ops-list'), opcons = imce.el('op-contents');
298
  var name = op.name || ('op-'+ $(oplist).children('li').length);
299
  var title = op.title || 'Untitled';
300
  var Op = imce.ops[name] = {title: title};
301
  if (op.content) {
302
    Op.div = imce.newEl('div');
303
    $(Op.div).attr({id: 'op-content-'+ name, 'class': 'op-content'}).appendTo(opcons).append(op.content);
304
  }
305
  Op.a = imce.newEl('a');
306
  Op.li = imce.newEl('li');
307
  $(Op.a).attr({href: '#', name: name, title: title}).html('<span>' + title +'</span>').click(imce.opClickEvent);
308
  $(Op.li).attr('id', 'op-item-'+ name).append(Op.a).appendTo(oplist);
309
  Op.func = op.func || imce.opVoid;
310
  return Op;
311
},
312

    
313
//click event for file operations
314
opClickEvent: function(e) {
315
  imce.opClick(this.name);
316
  return false;
317
},
318

    
319
//void operation function
320
opVoid: function() {},
321

    
322
//perform op click
323
opClick: function(name) {
324
  var Op = imce.ops[name], oldop = imce.vars.op;
325
  if (!Op || Op.disabled) {
326
    return imce.setMessage(Drupal.t('You can not perform this operation.'), 'error');
327
  }
328
  if (Op.div) {
329
    if (oldop) {
330
      var toggle = oldop == name;
331
      imce.opShrink(oldop, toggle ? 'fadeOut' : 'hide');
332
      if (toggle) return false;
333
    }
334
    var left = Op.li.offsetLeft;
335
    var $opcon = $('#op-contents').css({left: 0});
336
    $(Op.div).fadeIn('normal', function() {
337
      setTimeout(function() {
338
        if (imce.vars.op) {
339
          var $inputs = $('input', imce.ops[imce.vars.op].div);
340
          $inputs.eq(0).focus();
341
          //form inputs become invisible in IE. Solution is as stupid as the behavior.
342
          $('html').hasClass('ie') && $inputs.addClass('dummyie').removeClass('dummyie');
343
       }
344
      });
345
    });
346
    var diff = left + $opcon.width() - $('#imce-content').width();
347
    $opcon.css({left: diff > 0 ? left - diff - 1 : left});
348
    $(Op.li).addClass('active');
349
    $(imce.opCloseLink).fadeIn(300);
350
    imce.vars.op = name;
351
  }
352
  Op.func(true);
353
  return true;
354
},
355

    
356
//enable a file operation
357
opEnable: function(name) {
358
  var Op = imce.ops[name];
359
  if (Op && Op.disabled) {
360
    Op.disabled = false;
361
    $(Op.li).show();
362
  }
363
},
364

    
365
//disable a file operation
366
opDisable: function(name) {
367
  var Op = imce.ops[name];
368
  if (Op && !Op.disabled) {
369
    Op.div && imce.opShrink(name);
370
    $(Op.li).hide();
371
    Op.disabled = true;
372
  }
373
},
374

    
375
//hide contents of a file operation
376
opShrink: function(name, effect) {
377
  if (imce.vars.op != name) return;
378
  var Op = imce.ops[name];
379
  $(Op.div).stop(true, true)[effect || 'hide']();
380
  $(Op.li).removeClass('active');
381
  $(imce.opCloseLink).hide();
382
  Op.func(false);
383
  imce.vars.op = null;
384
},
385

    
386
//navigate to dir
387
navigate: function(dir) {
388
  if (imce.vars.navbusy || (dir == imce.conf.dir && !confirm(Drupal.t('Do you want to refresh the current directory?')))) return;
389
  var cache = imce.vars.cache && dir != imce.conf.dir;
390
  var set = imce.navSet(dir, cache);
391
  if (cache && imce.cache[dir]) {//load from the cache
392
    set.success({data: imce.cache[dir]});
393
    set.complete();
394
  }
395
  else $.ajax(set);//live load
396
},
397
//ajax navigation settings
398
navSet: function (dir, cache) {
399
  $(imce.tree[dir].li).addClass('loading');
400
  imce.vars.navbusy = dir;
401
  return {url: imce.ajaxURL('navigate', dir),
402
  type: 'GET',
403
  dataType: 'json',
404
  success: function(response) {
405
    if (response.data && !response.data.error) {
406
      if (cache) imce.navCache(imce.conf.dir, dir);//cache the current dir
407
      imce.navUpdate(response.data, dir);
408
    }
409
    imce.processResponse(response);
410
  },
411
  complete: function () {
412
    $(imce.tree[dir].li).removeClass('loading');
413
    imce.vars.navbusy = null;
414
  }
415
  };
416
},
417

    
418
//update directory using the given data
419
navUpdate: function(data, dir) {
420
  var cached = data == imce.cache[dir], olddir = imce.conf.dir;
421
  if (cached) data.files.id = 'file-list';
422
  $(imce.FLW).html(data.files);
423
  imce.dirActivate(dir);
424
  imce.dirSubdirs(dir, data.subdirectories);
425
  $.extend(imce.conf.perm, data.perm);
426
  imce.refreshOps();
427
  imce.initiateList(cached);
428
  imce.setPreview(imce.selcount == 1 ? imce.lastFid() : null);
429
  imce.SBW.scrollTop = 0;
430
  imce.invoke('navigate', data, olddir, cached);
431
},
432

    
433
//set cache
434
navCache: function (dir, newdir) {
435
  var C = imce.cache[dir] = {'dir': dir, files: imce.el('file-list'), dirsize: imce.el('dir-size').innerHTML, perm: $.extend({}, imce.conf.perm)};
436
  C.files.id = 'cached-list-'+ dir;
437
  imce.FW.appendChild(C.files);
438
  imce.invoke('cache', C, newdir);
439
},
440

    
441
//validate upload form
442
uploadValidate: function (data, form, options) {
443
  var path = $('#edit-imce').val();
444
  if (!path) return false;
445
  if (imce.conf.extensions != '*') {
446
    var ext = path.substr(path.lastIndexOf('.') + 1);
447
    if ((' '+ imce.conf.extensions +' ').indexOf(' '+ ext.toLowerCase() +' ') == -1) {
448
      return imce.setMessage(Drupal.t('Only files with the following extensions are allowed: %files-allowed.', {'%files-allowed': imce.conf.extensions}), 'error');
449
    }
450
  }
451
  options.url = imce.ajaxURL('upload');//make url contain current dir.
452
  imce.fopLoading('upload', true);
453
  return true;
454
},
455

    
456
//settings for upload
457
uploadSettings: function () {
458
  return {
459
    beforeSubmit: imce.uploadValidate,
460
    success: function (response) {
461
      try{
462
        imce.processResponse($.parseJSON(response));
463
      } catch(e) {}
464
    },
465
    complete: function () {
466
      imce.fopLoading('upload', false);
467
    },
468
    resetForm: true,
469
    dataType: 'text'
470
  };
471
},
472

    
473
//validate default ops(delete, thumb, resize)
474
fopValidate: function(fop) {
475
  if (!imce.validateSelCount(1, imce.conf.filenum)) return false;
476
  switch (fop) {
477
    case 'delete':
478
      return confirm(Drupal.t('Delete selected files?'));
479
    case 'thumb':
480
      if (!$('input:checked', imce.ops['thumb'].div).length) {
481
        return imce.setMessage(Drupal.t('Please select a thumbnail.'), 'error');
482
      }
483
      return imce.validateImage();
484
    case 'resize':
485
      var w = imce.el('edit-width').value, h = imce.el('edit-height').value;
486
      var maxDim = imce.conf.dimensions.split('x');
487
      var maxW = maxDim[0]*1, maxH = maxW ? maxDim[1]*1 : 0;
488
      if (!(/^[1-9][0-9]*$/).test(w) || !(/^[1-9][0-9]*$/).test(h) || (maxW && (maxW < w*1 || maxH < h*1))) {
489
        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');
490
      }
491
      return imce.validateImage();
492
  }
493

    
494
  var func = fop +'OpValidate';
495
  if (imce[func]) return imce[func](fop);
496
  return true;
497
},
498

    
499
//submit wrapper for default ops
500
fopSubmit: function(fop) {
501
  switch (fop) {
502
    case 'thumb': case 'delete': case 'resize':  return imce.commonSubmit(fop);
503
  }
504
  var func = fop +'OpSubmit';
505
  if (imce[func]) return imce[func](fop);
506
},
507

    
508
//common submit function shared by default ops
509
commonSubmit: function(fop) {
510
  if (!imce.fopValidate(fop)) return false;
511
  imce.fopLoading(fop, true);
512
  $.ajax(imce.fopSettings(fop));
513
},
514

    
515
//settings for default file operations
516
fopSettings: function (fop) {
517
  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() : '')};
518
},
519

    
520
//toggle loading state
521
fopLoading: function(fop, state) {
522
  var el = imce.el('edit-'+ fop), func = state ? 'addClass' : 'removeClass';
523
  if (el) {
524
    $(el)[func]('loading');
525
    el.disabled = state;
526
  }
527
  else {
528
    $(imce.ops[fop].li)[func]('loading');
529
    imce.ops[fop].disabled = state;
530
  }
531
},
532

    
533
//preview a file.
534
setPreview: function (fid) {
535
  var row, html = '';
536
  imce.vars.prvfid = fid;
537
  if (fid && (row = imce.fids[fid])) {
538
    var width = row.cells[2].innerHTML * 1;
539
    html = imce.vars.previewImages && width ? imce.imgHtml(fid, width, row.cells[3].innerHTML) : imce.decodePlain(fid);
540
    html = '<a href="#" onclick="imce.send(\''+ fid +'\'); return false;" title="'+ (imce.vars.prvtitle||'') +'">'+ html +'</a>';
541
  }
542
  imce.el('file-preview').innerHTML = html;
543
},
544

    
545
//default file send function. sends the file to the new window.
546
send: function (fid) {
547
  fid && window.open(imce.getURL(fid));
548
},
549

    
550
//add an operation for an external application to which the files are send.
551
setSendTo: function (title, func) {
552
  imce.send = function (fid) { fid && func(imce.fileGet(fid), window);};
553
  var opFunc = function () {
554
    if (imce.selcount != 1) return imce.setMessage(Drupal.t('Please select a file.'), 'error');
555
    imce.send(imce.vars.prvfid);
556
  };
557
  imce.vars.prvtitle = title;
558
  return imce.opAdd({name: 'sendto', title: title, func: opFunc});
559
},
560

    
561
//move initial page messages into log
562
prepareMsgs: function () {
563
  var msgs;
564
  if (msgs = imce.el('imce-messages')) {
565
    $('>div', msgs).each(function (){
566
      var type = this.className.split(' ')[1];
567
      var li = $('>ul li', this);
568
      if (li.length) li.each(function () {imce.setMessage(this.innerHTML, type);});
569
      else imce.setMessage(this.innerHTML, type);
570
    });
571
    $(msgs).remove();
572
  }
573
},
574

    
575
//insert log message
576
setMessage: function (msg, type) {
577
  var $box = $(imce.msgBox);
578
  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];
579
  var msg = '<div class="message '+ (type || 'status') +'">'+ msg +'</div>';
580
  $box.queue(function() {
581
    $box.css({opacity: 0, display: 'block'}).html(msg);
582
    $box.dequeue();
583
  });
584
  var q = $box.queue().length, t = imce.vars.msgT || 1000;
585
  q = q < 2 ? 1 : q < 3 ? 0.8 : q < 4 ? 0.7 : 0.4;//adjust speed with respect to queue length
586
  $box.fadeTo(600 * q, 1).fadeTo(t * q, 1).fadeOut(400 * q);
587
  $(logs).append(msg);
588
  return false;
589
},
590

    
591
//invoke hooks
592
invoke: function (hook) {
593
  var i, args, func, funcs;
594
  if ((funcs = imce.hooks[hook]) && funcs.length) {
595
    (args = $.makeArray(arguments)).shift();
596
    for (i = 0; func = funcs[i]; i++) func.apply(this, args);
597
  }
598
},
599

    
600
//process response
601
processResponse: function (response) {
602
  if (response.data) imce.resData(response.data);
603
  if (response.messages) imce.resMsgs(response.messages);
604
},
605

    
606
//process response data
607
resData: function (data) {
608
  var i, added, removed;
609
  if (added = data.added) {
610
    var cnt = imce.findex.length;
611
    for (i in added) {//add new files or update existing
612
      imce.fileAdd(added[i]);
613
    }
614
    if (added.length == 1) {//if it is a single file operation
615
      imce.highlight(added[0].name);//highlight
616
    }
617
    if (imce.findex.length != cnt) {//if new files added, scroll to bottom.
618
      $(imce.SBW).animate({scrollTop: imce.SBW.scrollHeight}).focus();
619
    }
620
  }
621
  if (removed = data.removed) for (i in removed) {
622
    imce.fileRemove(removed[i]);
623
  }
624
  imce.conf.dirsize = data.dirsize;
625
  imce.updateStat();
626
},
627

    
628
//set response messages
629
resMsgs: function (msgs) {
630
  for (var type in msgs) for (var i in msgs[type]) {
631
    imce.setMessage(msgs[type][i], type);
632
  }
633
},
634

    
635
//return img markup
636
imgHtml: function (fid, width, height) {
637
  return '<img src="'+ imce.getURL(fid, true) +'" width="'+ width +'" height="'+ height +'" alt="'+ imce.decodePlain(fid) +'">';
638
},
639

    
640
//check if the file is an image
641
isImage: function (fid) {
642
  return imce.fids[fid].cells[2].innerHTML * 1;
643
},
644

    
645
//find the first non-image in the selection
646
getNonImage: function (selected) {
647
  for (var fid in selected) {
648
    if (!imce.isImage(fid)) return fid;
649
  }
650
  return false;
651
},
652

    
653
//validate current selection for images
654
validateImage: function () {
655
  var nonImg = imce.getNonImage(imce.selected);
656
  return nonImg ? imce.setMessage(Drupal.t('%filename is not an image.', {'%filename': imce.decode(nonImg)}), 'error') : true;
657
},
658

    
659
//validate number of selected files
660
validateSelCount: function (Min, Max) {
661
  if (Min && imce.selcount < Min) {
662
    return imce.setMessage(Min == 1 ? Drupal.t('Please select a file.') : Drupal.t('You must select at least %num files.', {'%num': Min}), 'error');
663
  }
664
  if (Max && Max < imce.selcount) {
665
    return imce.setMessage(Drupal.t('You are not allowed to operate on more than %num files.', {'%num': Max}), 'error');
666
  }
667
  return true;
668
},
669

    
670
//update file count and dir size
671
updateStat: function () {
672
  imce.el('file-count').innerHTML = imce.findex.length;
673
  imce.el('dir-size').innerHTML = imce.conf.dirsize;
674
},
675

    
676
//serialize selected files. return fids with a colon between them
677
serialNames: function () {
678
  var str = '';
679
  for (var fid in imce.selected) {
680
    str += ':'+ fid;
681
  }
682
  return str.substr(1);
683
},
684

    
685
//get file url. re-encode & and # for mod rewrite
686
getURL: function (fid, uncached) {
687
  var url = imce.getRelpath(fid);
688
  if (imce.conf.modfix) {
689
    url = url.replace(/%(23|26)/g, '%25$1');
690
  }
691
  url = imce.conf.furl + url;
692
  if (uncached) {
693
    var file = imce.fileProps(fid);
694
    url += (url.indexOf('?') === -1 ? '?' : '&') + 's' + file.bytes + 'd' + file.time;
695
  }
696
  return url;
697
},
698

    
699
//get encoded file path relative to root. 
700
getRelpath: function (fid) {
701
  var dir = imce.conf.dir;
702
  return (dir === '.' ? '' : dir + '/') + fid;
703
},
704

    
705
//el. by id
706
el: function (id) {
707
  return document.getElementById(id);
708
},
709

    
710
//find the latest selected fid
711
lastFid: function () {
712
  if (imce.vars.lastfid) return imce.vars.lastfid;
713
  for (var fid in imce.selected);
714
  return fid;
715
},
716

    
717
//create ajax url
718
ajaxURL: function (op, dir) {
719
  return imce.conf.url + (imce.conf.clean ? '?' :'&') +'jsop='+ op +'&dir='+ (dir||imce.conf.dir);
720
},
721

    
722
//fast class check
723
hasC: function (el, name) {
724
  return el.className && (' '+ el.className +' ').indexOf(' '+ name +' ') != -1;
725
},
726

    
727
//highlight a single file
728
highlight: function (fid) {
729
  if (imce.vars.prvfid) imce.fileClick(imce.vars.prvfid);
730
  imce.fileClick(fid);
731
},
732

    
733
//process a row
734
processRow: function (row) {
735
  row.cells[0].innerHTML = '<span>' + imce.decodePlain(row.id) + '</span>';
736
  row.onmousedown = function(e) {
737
    var e = e||window.event;
738
    imce.fileClick(this, e.ctrlKey, e.shiftKey);
739
    return !(e.ctrlKey || e.shiftKey);
740
  };
741
  row.ondblclick = function(e) {
742
    imce.send(this.id);
743
    return false;
744
  };
745
},
746

    
747
//decode urls. uses unescape. can be overridden to use decodeURIComponent
748
decode: function (str) {
749
  try {
750
    return decodeURIComponent(str);
751
  } catch(e) {}
752
  return str;
753
},
754

    
755
//decode and convert to plain text
756
decodePlain: function (str) {
757
  return Drupal.checkPlain(imce.decode(str));
758
},
759

    
760
//global ajax error function
761
ajaxError: function (e, response, settings, thrown) {
762
  imce.setMessage(Drupal.ajaxError(response, settings.url).replace(/\n/g, '<br />'), 'error');
763
},
764

    
765
//convert button elements to standard input buttons
766
convertButtons: function(form) {
767
  $('button:submit', form).each(function(){
768
    $(this).replaceWith('<input type="submit" value="'+ $(this).text() +'" name="'+ this.name +'" class="form-submit" id="'+ this.id +'" />');
769
  });
770
},
771

    
772
//create element
773
newEl: function(name) {
774
  return document.createElement(name);
775
},
776

    
777
//scroll syncronization for section headers
778
syncScroll: function(scrlEl, fixEl, bottom) {
779
  var $fixEl = $(fixEl);
780
  var prop = bottom ? 'bottom' : 'top';
781
  var factor = bottom ? -1 : 1;
782
  var syncScrl = function(el) {
783
    $fixEl.css(prop, factor * el.scrollTop);
784
  }
785
  $(scrlEl).scroll(function() {
786
    var el = this;
787
    syncScrl(el);
788
    setTimeout(function() {
789
      syncScrl(el);
790
    });
791
  });
792
},
793

    
794
//get UI ready. provide backward compatibility.
795
updateUI: function() {
796
  //file urls.
797
  var furl = imce.conf.furl, isabs = furl.indexOf('://') > -1;
798
  var absurls = imce.conf.absurls = imce.vars.absurls || imce.conf.absurls;
799
  var host = location.host;
800
  var baseurl = location.protocol + '//' + host;
801
  if (furl.charAt(furl.length - 1) != '/') {
802
    furl = imce.conf.furl = furl + '/';
803
  }
804
  imce.conf.modfix = imce.conf.clean && furl.split('/')[3] === 'system';
805
  if (absurls && !isabs) {
806
    imce.conf.furl = baseurl + furl;
807
  }
808
  else if (!absurls && isabs && furl.indexOf(baseurl) == 0) {
809
    furl = furl.substr(baseurl.length);
810
    // Server base url is defined with a port which is missing in current page url.
811
    if (furl.charAt(0) === ':') {
812
      furl = furl.replace(/^:\d*/, '');
813
    }
814
    imce.conf.furl = furl;
815
  }
816
  //convert button elements to input elements.
817
  imce.convertButtons(imce.FW);
818
  //ops-list
819
  $('#ops-list').removeClass('tabs secondary').addClass('clear-block clearfix');
820
  imce.opCloseLink = $(imce.newEl('a')).attr({id: 'op-close-link', href: '#', title: Drupal.t('Close')}).click(function() {
821
    imce.vars.op && imce.opClick(imce.vars.op);
822
    return false;
823
  }).appendTo('#op-contents')[0];
824
  //navigation-header
825
  if (!$('#navigation-header').length) {
826
    $(imce.NW).children('.navigation-text').attr('id', 'navigation-header').wrapInner('<span></span>');
827
  }
828
  //log
829
  $('#log-prv-wrapper').before($('#log-prv-wrapper > #preview-wrapper')).remove();
830
  $('#log-clearer').remove();
831
  //content resizer
832
  $('#content-resizer').remove();
833
  //message-box
834
  imce.msgBox = imce.el('message-box') || $(imce.newEl('div')).attr('id', 'message-box').prependTo('#imce-content')[0];
835
  //create help tab
836
  var $hbox = $('#help-box');
837
  $hbox.is('a') && $hbox.replaceWith($(imce.newEl('div')).attr('id', 'help-box').append($hbox.children()));
838
  imce.hooks.load.push(function() {
839
    imce.opAdd({name: 'help', title: $('#help-box-title').remove().text(), content: $('#help-box').show()});
840
  });
841
  //add ie classes
842
  imce.ie && $('html').addClass('ie') && imce.ie < 8 && $('html').addClass('ie-7');
843
  // enable box view for file list
844
  imce.vars.boxW && imce.boxView();
845
  //scrolling file list
846
  imce.syncScroll(imce.SBW, '#file-header-wrapper');
847
  imce.syncScroll(imce.SBW, '#dir-stat', true);
848
  //scrolling directory tree
849
  imce.syncScroll(imce.NW, '#navigation-header');
850
}
851

    
852
};
853

    
854
//initiate
855
$(document).ready(imce.initiate);
856

    
857
})(jQuery);