Projet

Général

Profil

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

root / drupal7 / sites / all / modules / imce / js / imce.js @ ba09eb79

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 row = imce.fids[fid];
165
  var url = imce.getURL(fid);
166
  return row ? {
167
    name: imce.decode(fid),
168
    url: url,
169
    size: row.cells[1].innerHTML,
170
    bytes: row.cells[1].id * 1,
171
    width: row.cells[2].innerHTML * 1,
172
    height: row.cells[3].innerHTML * 1,
173
    date: row.cells[4].innerHTML,
174
    time: row.cells[4].id * 1,
175
    id: imce.urlId[url] || 0, //file id for newly uploaded files
176
    relpath: (imce.conf.dir == '.' ? '' : imce.conf.dir +'/') + fid //rawurlencoded path relative to file directory path.
177
  } : null;
178
},
179

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

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

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

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

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

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

    
280
//refresh ops states. enable/disable
281
refreshOps: function() {
282
  for (var p in imce.conf.perm) {
283
    if (imce.conf.perm[p]) imce.opEnable(p);
284
    else imce.opDisable(p);
285
  }
286
},
287

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

    
306
//click event for file operations
307
opClickEvent: function(e) {
308
  imce.opClick(this.name);
309
  return false;
310
},
311

    
312
//void operation function
313
opVoid: function() {},
314

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

    
349
//enable a file operation
350
opEnable: function(name) {
351
  var Op = imce.ops[name];
352
  if (Op && Op.disabled) {
353
    Op.disabled = false;
354
    $(Op.li).show();
355
  }
356
},
357

    
358
//disable a file operation
359
opDisable: function(name) {
360
  var Op = imce.ops[name];
361
  if (Op && !Op.disabled) {
362
    Op.div && imce.opShrink(name);
363
    $(Op.li).hide();
364
    Op.disabled = true;
365
  }
366
},
367

    
368
//hide contents of a file operation
369
opShrink: function(name, effect) {
370
  if (imce.vars.op != name) return;
371
  var Op = imce.ops[name];
372
  $(Op.div).stop(true, true)[effect || 'hide']();
373
  $(Op.li).removeClass('active');
374
  $(imce.opCloseLink).hide();
375
  Op.func(false);
376
  imce.vars.op = null;
377
},
378

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

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

    
426
//set cache
427
navCache: function (dir, newdir) {
428
  var C = imce.cache[dir] = {'dir': dir, files: imce.el('file-list'), dirsize: imce.el('dir-size').innerHTML, perm: $.extend({}, imce.conf.perm)};
429
  C.files.id = 'cached-list-'+ dir;
430
  imce.FW.appendChild(C.files);
431
  imce.invoke('cache', C, newdir);
432
},
433

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

    
449
//settings for upload
450
uploadSettings: function () {
451
  return {
452
    beforeSubmit: imce.uploadValidate,
453
    success: function (response) {
454
      try{
455
        imce.processResponse($.parseJSON(response));
456
      } catch(e) {}
457
    },
458
    complete: function () {
459
      imce.fopLoading('upload', false);
460
    },
461
    resetForm: true,
462
    dataType: 'text'
463
  };
464
},
465

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

    
487
  var func = fop +'OpValidate';
488
  if (imce[func]) return imce[func](fop);
489
  return true;
490
},
491

    
492
//submit wrapper for default ops
493
fopSubmit: function(fop) {
494
  switch (fop) {
495
    case 'thumb': case 'delete': case 'resize':  return imce.commonSubmit(fop);
496
  }
497
  var func = fop +'OpSubmit';
498
  if (imce[func]) return imce[func](fop);
499
},
500

    
501
//common submit function shared by default ops
502
commonSubmit: function(fop) {
503
  if (!imce.fopValidate(fop)) return false;
504
  imce.fopLoading(fop, true);
505
  $.ajax(imce.fopSettings(fop));
506
},
507

    
508
//settings for default file operations
509
fopSettings: function (fop) {
510
  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() : '')};
511
},
512

    
513
//toggle loading state
514
fopLoading: function(fop, state) {
515
  var el = imce.el('edit-'+ fop), func = state ? 'addClass' : 'removeClass';
516
  if (el) {
517
    $(el)[func]('loading').attr('disabled', state);
518
  }
519
  else {
520
    $(imce.ops[fop].li)[func]('loading');
521
    imce.ops[fop].disabled = state;
522
  }
523
},
524

    
525
//preview a file.
526
setPreview: function (fid) {
527
  var row, html = '';
528
  imce.vars.prvfid = fid;
529
  if (fid && (row = imce.fids[fid])) {
530
    var width = row.cells[2].innerHTML * 1;
531
    html = imce.vars.previewImages && width ? imce.imgHtml(fid, width, row.cells[3].innerHTML) : imce.decodePlain(fid);
532
    html = '<a href="#" onclick="imce.send(\''+ fid +'\'); return false;" title="'+ (imce.vars.prvtitle||'') +'">'+ html +'</a>';
533
  }
534
  imce.el('file-preview').innerHTML = html;
535
},
536

    
537
//default file send function. sends the file to the new window.
538
send: function (fid) {
539
  fid && window.open(imce.getURL(fid));
540
},
541

    
542
//add an operation for an external application to which the files are send.
543
setSendTo: function (title, func) {
544
  imce.send = function (fid) { fid && func(imce.fileGet(fid), window);};
545
  var opFunc = function () {
546
    if (imce.selcount != 1) return imce.setMessage(Drupal.t('Please select a file.'), 'error');
547
    imce.send(imce.vars.prvfid);
548
  };
549
  imce.vars.prvtitle = title;
550
  return imce.opAdd({name: 'sendto', title: title, func: opFunc});
551
},
552

    
553
//move initial page messages into log
554
prepareMsgs: function () {
555
  var msgs;
556
  if (msgs = imce.el('imce-messages')) {
557
    $('>div', msgs).each(function (){
558
      var type = this.className.split(' ')[1];
559
      var li = $('>ul li', this);
560
      if (li.length) li.each(function () {imce.setMessage(this.innerHTML, type);});
561
      else imce.setMessage(this.innerHTML, type);
562
    });
563
    $(msgs).remove();
564
  }
565
},
566

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

    
583
//invoke hooks
584
invoke: function (hook) {
585
  var i, args, func, funcs;
586
  if ((funcs = imce.hooks[hook]) && funcs.length) {
587
    (args = $.makeArray(arguments)).shift();
588
    for (i = 0; func = funcs[i]; i++) func.apply(this, args);
589
  }
590
},
591

    
592
//process response
593
processResponse: function (response) {
594
  if (response.data) imce.resData(response.data);
595
  if (response.messages) imce.resMsgs(response.messages);
596
},
597

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

    
620
//set response messages
621
resMsgs: function (msgs) {
622
  for (var type in msgs) for (var i in msgs[type]) {
623
    imce.setMessage(msgs[type][i], type);
624
  }
625
},
626

    
627
//return img markup
628
imgHtml: function (fid, width, height) {
629
  return '<img src="'+ imce.getURL(fid) +'" width="'+ width +'" height="'+ height +'" alt="'+ imce.decodePlain(fid) +'">';
630
},
631

    
632
//check if the file is an image
633
isImage: function (fid) {
634
  return imce.fids[fid].cells[2].innerHTML * 1;
635
},
636

    
637
//find the first non-image in the selection
638
getNonImage: function (selected) {
639
  for (var fid in selected) {
640
    if (!imce.isImage(fid)) return fid;
641
  }
642
  return false;
643
},
644

    
645
//validate current selection for images
646
validateImage: function () {
647
  var nonImg = imce.getNonImage(imce.selected);
648
  return nonImg ? imce.setMessage(Drupal.t('%filename is not an image.', {'%filename': imce.decode(nonImg)}), 'error') : true;
649
},
650

    
651
//validate number of selected files
652
validateSelCount: function (Min, Max) {
653
  if (Min && imce.selcount < Min) {
654
    return imce.setMessage(Min == 1 ? Drupal.t('Please select a file.') : Drupal.t('You must select at least %num files.', {'%num': Min}), 'error');
655
  }
656
  if (Max && Max < imce.selcount) {
657
    return imce.setMessage(Drupal.t('You are not allowed to operate on more than %num files.', {'%num': Max}), 'error');
658
  }
659
  return true;
660
},
661

    
662
//update file count and dir size
663
updateStat: function () {
664
  imce.el('file-count').innerHTML = imce.findex.length;
665
  imce.el('dir-size').innerHTML = imce.conf.dirsize;
666
},
667

    
668
//serialize selected files. return fids with a colon between them
669
serialNames: function () {
670
  var str = '';
671
  for (var fid in imce.selected) {
672
    str += ':'+ fid;
673
  }
674
  return str.substr(1);
675
},
676

    
677
//get file url. re-encode & and # for mod rewrite
678
getURL: function (fid) {
679
  var path = (imce.conf.dir == '.' ? '' : imce.conf.dir +'/') + fid;
680
  return imce.conf.furl + (imce.conf.modfix ? path.replace(/%(23|26)/g, '%25$1') : path);
681
},
682

    
683
//el. by id
684
el: function (id) {
685
  return document.getElementById(id);
686
},
687

    
688
//find the latest selected fid
689
lastFid: function () {
690
  if (imce.vars.lastfid) return imce.vars.lastfid;
691
  for (var fid in imce.selected);
692
  return fid;
693
},
694

    
695
//create ajax url
696
ajaxURL: function (op, dir) {
697
  return imce.conf.url + (imce.conf.clean ? '?' :'&') +'jsop='+ op +'&dir='+ (dir||imce.conf.dir);
698
},
699

    
700
//fast class check
701
hasC: function (el, name) {
702
  return el.className && (' '+ el.className +' ').indexOf(' '+ name +' ') != -1;
703
},
704

    
705
//highlight a single file
706
highlight: function (fid) {
707
  if (imce.vars.prvfid) imce.fileClick(imce.vars.prvfid);
708
  imce.fileClick(fid);
709
},
710

    
711
//process a row
712
processRow: function (row) {
713
  row.cells[0].innerHTML = '<span>' + imce.decodePlain(row.id) + '</span>';
714
  row.onmousedown = function(e) {
715
    var e = e||window.event;
716
    imce.fileClick(this, e.ctrlKey, e.shiftKey);
717
    return !(e.ctrlKey || e.shiftKey);
718
  };
719
  row.ondblclick = function(e) {
720
    imce.send(this.id);
721
    return false;
722
  };
723
},
724

    
725
//decode urls. uses unescape. can be overridden to use decodeURIComponent
726
decode: function (str) {
727
  try {
728
    return decodeURIComponent(str);
729
  } catch(e) {}
730
  return str;
731
},
732

    
733
//decode and convert to plain text
734
decodePlain: function (str) {
735
  return Drupal.checkPlain(imce.decode(str));
736
},
737

    
738
//global ajax error function
739
ajaxError: function (e, response, settings, thrown) {
740
  imce.setMessage(Drupal.ajaxError(response, settings.url).replace(/\n/g, '<br />'), 'error');
741
},
742

    
743
//convert button elements to standard input buttons
744
convertButtons: function(form) {
745
  $('button:submit', form).each(function(){
746
    $(this).replaceWith('<input type="submit" value="'+ $(this).text() +'" name="'+ this.name +'" class="form-submit" id="'+ this.id +'" />');
747
  });
748
},
749

    
750
//create element
751
newEl: function(name) {
752
  return document.createElement(name);
753
},
754

    
755
//scroll syncronization for section headers
756
syncScroll: function(scrlEl, fixEl, bottom) {
757
  var $fixEl = $(fixEl);
758
  var prop = bottom ? 'bottom' : 'top';
759
  var factor = bottom ? -1 : 1;
760
  var syncScrl = function(el) {
761
    $fixEl.css(prop, factor * el.scrollTop);
762
  }
763
  $(scrlEl).scroll(function() {
764
    var el = this;
765
    syncScrl(el);
766
    setTimeout(function() {
767
      syncScrl(el);
768
    });
769
  });
770
},
771

    
772
//get UI ready. provide backward compatibility.
773
updateUI: function() {
774
  //file urls.
775
  var furl = imce.conf.furl, isabs = furl.indexOf('://') > -1;
776
  var absurls = imce.conf.absurls = imce.vars.absurls || imce.conf.absurls;
777
  var host = location.host;
778
  var baseurl = location.protocol + '//' + host;
779
  if (furl.charAt(furl.length - 1) != '/') {
780
    furl = imce.conf.furl = furl + '/';
781
  }
782
  imce.conf.modfix = imce.conf.clean && furl.indexOf(host + '/system/') > -1;
783
  if (absurls && !isabs) {
784
    imce.conf.furl = baseurl + furl;
785
  }
786
  else if (!absurls && isabs && furl.indexOf(baseurl) == 0) {
787
    imce.conf.furl = furl.substr(baseurl.length);
788
  }
789
  //convert button elements to input elements.
790
  imce.convertButtons(imce.FW);
791
  //ops-list
792
  $('#ops-list').removeClass('tabs secondary').addClass('clear-block clearfix');
793
  imce.opCloseLink = $(imce.newEl('a')).attr({id: 'op-close-link', href: '#', title: Drupal.t('Close')}).click(function() {
794
    imce.vars.op && imce.opClick(imce.vars.op);
795
    return false;
796
  }).appendTo('#op-contents')[0];
797
  //navigation-header
798
  if (!$('#navigation-header').length) {
799
    $(imce.NW).children('.navigation-text').attr('id', 'navigation-header').wrapInner('<span></span>');
800
  }
801
  //log
802
  $('#log-prv-wrapper').before($('#log-prv-wrapper > #preview-wrapper')).remove();
803
  $('#log-clearer').remove();
804
  //content resizer
805
  $('#content-resizer').remove();
806
  //message-box
807
  imce.msgBox = imce.el('message-box') || $(imce.newEl('div')).attr('id', 'message-box').prependTo('#imce-content')[0];
808
  //create help tab
809
  var $hbox = $('#help-box');
810
  $hbox.is('a') && $hbox.replaceWith($(imce.newEl('div')).attr('id', 'help-box').append($hbox.children()));
811
  imce.hooks.load.push(function() {
812
    imce.opAdd({name: 'help', title: $('#help-box-title').remove().text(), content: $('#help-box').show()});
813
  });
814
  //add ie classes
815
  imce.ie && $('html').addClass('ie') && imce.ie < 8 && $('html').addClass('ie-7');
816
  // enable box view for file list
817
  imce.vars.boxW && imce.boxView();
818
  //scrolling file list
819
  imce.syncScroll(imce.SBW, '#file-header-wrapper');
820
  imce.syncScroll(imce.SBW, '#dir-stat', true);
821
  //scrolling directory tree
822
  imce.syncScroll(imce.NW, '#navigation-header');
823
}
824

    
825
};
826

    
827
//initiate
828
$(document).ready(imce.initiate);
829

    
830
})(jQuery);