oc-dialogs.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /**
  2. * ownCloud
  3. *
  4. * @author Bartek Przybylski, Christopher Schäpers, Thomas Tanghus
  5. * @copyright 2012 Bartek Przybylski bartek@alefzero.eu
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /* global alert */
  22. /**
  23. * this class to ease the usage of jquery dialogs
  24. * @lends OC.dialogs
  25. */
  26. var OCdialogs = {
  27. // dialog button types
  28. YES_NO_BUTTONS: 70,
  29. OK_BUTTONS: 71,
  30. // used to name each dialog
  31. dialogsCounter: 0,
  32. /**
  33. * displays alert dialog
  34. * @param text content of dialog
  35. * @param title dialog title
  36. * @param callback which will be triggered when user presses OK
  37. * @param modal make the dialog modal
  38. */
  39. alert:function(text, title, callback, modal) {
  40. this.message(
  41. text,
  42. title,
  43. 'alert',
  44. OCdialogs.OK_BUTTON,
  45. callback,
  46. modal
  47. );
  48. },
  49. /**
  50. * displays info dialog
  51. * @param text content of dialog
  52. * @param title dialog title
  53. * @param callback which will be triggered when user presses OK
  54. * @param modal make the dialog modal
  55. */
  56. info:function(text, title, callback, modal) {
  57. this.message(text, title, 'info', OCdialogs.OK_BUTTON, callback, modal);
  58. },
  59. /**
  60. * displays confirmation dialog
  61. * @param text content of dialog
  62. * @param title dialog title
  63. * @param callback which will be triggered when user presses YES or NO
  64. * (true or false would be passed to callback respectively)
  65. * @param modal make the dialog modal
  66. */
  67. confirm:function(text, title, callback, modal) {
  68. return this.message(
  69. text,
  70. title,
  71. 'notice',
  72. OCdialogs.YES_NO_BUTTONS,
  73. callback,
  74. modal
  75. );
  76. },
  77. /**
  78. * displays prompt dialog
  79. * @param text content of dialog
  80. * @param title dialog title
  81. * @param callback which will be triggered when user presses YES or NO
  82. * (true or false would be passed to callback respectively)
  83. * @param modal make the dialog modal
  84. * @param name name of the input field
  85. * @param password whether the input should be a password input
  86. */
  87. prompt: function (text, title, callback, modal, name, password) {
  88. return $.when(this._getMessageTemplate()).then(function ($tmpl) {
  89. var dialogName = 'oc-dialog-' + OCdialogs.dialogsCounter + '-content';
  90. var dialogId = '#' + dialogName;
  91. var $dlg = $tmpl.octemplate({
  92. dialog_name: dialogName,
  93. title : title,
  94. message : text,
  95. type : 'notice'
  96. });
  97. var input = $('<input/>');
  98. input.attr('type', password ? 'password' : 'text').attr('id', dialogName + '-input');
  99. var label = $('<label/>').attr('for', dialogName + '-input').text(name + ': ');
  100. $dlg.append(label);
  101. $dlg.append(input);
  102. if (modal === undefined) {
  103. modal = false;
  104. }
  105. $('body').append($dlg);
  106. var buttonlist = [{
  107. text : t('core', 'No'),
  108. click: function () {
  109. if (callback !== undefined) {
  110. callback(false, input.val());
  111. }
  112. $(dialogId).ocdialog('close');
  113. }
  114. }, {
  115. text : t('core', 'Yes'),
  116. click : function () {
  117. if (callback !== undefined) {
  118. callback(true, input.val());
  119. }
  120. $(dialogId).ocdialog('close');
  121. },
  122. defaultButton: true
  123. }
  124. ];
  125. $(dialogId).ocdialog({
  126. closeOnEscape: true,
  127. modal : modal,
  128. buttons : buttonlist
  129. });
  130. OCdialogs.dialogsCounter++;
  131. });
  132. },
  133. /**
  134. * show a file picker to pick a file from
  135. * @param title dialog title
  136. * @param callback which will be triggered when user presses Choose
  137. * @param multiselect whether it should be possible to select multiple files
  138. * @param mimetypeFilter mimetype to filter by
  139. * @param modal make the dialog modal
  140. */
  141. filepicker:function(title, callback, multiselect, mimetypeFilter, modal) {
  142. var self = this;
  143. // avoid opening the picker twice
  144. if (this.filepicker.loading) {
  145. return;
  146. }
  147. this.filepicker.loading = true;
  148. $.when(this._getFilePickerTemplate()).then(function($tmpl) {
  149. self.filepicker.loading = false;
  150. var dialogName = 'oc-dialog-filepicker-content';
  151. if(self.$filePicker) {
  152. self.$filePicker.ocdialog('close');
  153. }
  154. self.$filePicker = $tmpl.octemplate({
  155. dialog_name: dialogName,
  156. title: title
  157. }).data('path', '').data('multiselect', multiselect).data('mimetype', mimetypeFilter);
  158. if (modal === undefined) {
  159. modal = false;
  160. }
  161. if (multiselect === undefined) {
  162. multiselect = false;
  163. }
  164. if (mimetypeFilter === undefined) {
  165. mimetypeFilter = '';
  166. }
  167. $('body').append(self.$filePicker);
  168. self.$filePicker.ready(function() {
  169. self.$filelist = self.$filePicker.find('.filelist');
  170. self.$dirTree = self.$filePicker.find('.dirtree');
  171. self.$dirTree.on('click', 'span:not(:last-child)', self, self._handleTreeListSelect);
  172. self.$filelist.on('click', 'li', function(event) {
  173. self._handlePickerClick(event, $(this));
  174. });
  175. self._fillFilePicker('');
  176. });
  177. // build buttons
  178. var functionToCall = function() {
  179. if (callback !== undefined) {
  180. var datapath;
  181. if (multiselect === true) {
  182. datapath = [];
  183. self.$filelist.find('.filepicker_element_selected .filename').each(function(index, element) {
  184. datapath.push(self.$filePicker.data('path') + '/' + $(element).text());
  185. });
  186. } else {
  187. datapath = self.$filePicker.data('path');
  188. datapath += '/' + self.$filelist.find('.filepicker_element_selected .filename').text();
  189. }
  190. callback(datapath);
  191. self.$filePicker.ocdialog('close');
  192. }
  193. };
  194. var buttonlist = [{
  195. text: t('core', 'Choose'),
  196. click: functionToCall,
  197. defaultButton: true
  198. }];
  199. self.$filePicker.ocdialog({
  200. closeOnEscape: true,
  201. width: (4/5)*$(document).width(),
  202. height: 420,
  203. modal: modal,
  204. buttons: buttonlist,
  205. close: function() {
  206. try {
  207. $(this).ocdialog('destroy').remove();
  208. } catch(e) {}
  209. self.$filePicker = null;
  210. }
  211. });
  212. if (!OC.Util.hasSVGSupport()) {
  213. OC.Util.replaceSVG(self.$filePicker.parent());
  214. }
  215. })
  216. .fail(function(status, error) {
  217. // If the method is called while navigating away
  218. // from the page, it is probably not needed ;)
  219. self.filepicker.loading = false;
  220. if(status !== 0) {
  221. alert(t('core', 'Error loading file picker template: {error}', {error: error}));
  222. }
  223. });
  224. },
  225. /**
  226. * Displays raw dialog
  227. * You better use a wrapper instead ...
  228. */
  229. message:function(content, title, dialogType, buttons, callback, modal) {
  230. return $.when(this._getMessageTemplate()).then(function($tmpl) {
  231. var dialogName = 'oc-dialog-' + OCdialogs.dialogsCounter + '-content';
  232. var dialogId = '#' + dialogName;
  233. var $dlg = $tmpl.octemplate({
  234. dialog_name: dialogName,
  235. title: title,
  236. message: content,
  237. type: dialogType
  238. });
  239. if (modal === undefined) {
  240. modal = false;
  241. }
  242. $('body').append($dlg);
  243. var buttonlist = [];
  244. switch (buttons) {
  245. case OCdialogs.YES_NO_BUTTONS:
  246. buttonlist = [{
  247. text: t('core', 'No'),
  248. click: function(){
  249. if (callback !== undefined) {
  250. callback(false);
  251. }
  252. $(dialogId).ocdialog('close');
  253. }
  254. },
  255. {
  256. text: t('core', 'Yes'),
  257. click: function(){
  258. if (callback !== undefined) {
  259. callback(true);
  260. }
  261. $(dialogId).ocdialog('close');
  262. },
  263. defaultButton: true
  264. }];
  265. break;
  266. case OCdialogs.OK_BUTTON:
  267. var functionToCall = function() {
  268. $(dialogId).ocdialog('close');
  269. if(callback !== undefined) {
  270. callback();
  271. }
  272. };
  273. buttonlist[0] = {
  274. text: t('core', 'Ok'),
  275. click: functionToCall,
  276. defaultButton: true
  277. };
  278. break;
  279. }
  280. $(dialogId).ocdialog({
  281. closeOnEscape: true,
  282. modal: modal,
  283. buttons: buttonlist
  284. });
  285. OCdialogs.dialogsCounter++;
  286. })
  287. .fail(function(status, error) {
  288. // If the method is called while navigating away from
  289. // the page, we still want to deliver the message.
  290. if(status === 0) {
  291. alert(title + ': ' + content);
  292. } else {
  293. alert(t('core', 'Error loading message template: {error}', {error: error}));
  294. }
  295. });
  296. },
  297. _fileexistsshown: false,
  298. /**
  299. * Displays file exists dialog
  300. * @param {object} data upload object
  301. * @param {object} original file with name, size and mtime
  302. * @param {object} replacement file with name, size and mtime
  303. * @param {object} controller with onCancel, onSkip, onReplace and onRename methods
  304. */
  305. fileexists:function(data, original, replacement, controller) {
  306. var self = this;
  307. var getCroppedPreview = function(file) {
  308. var deferred = new $.Deferred();
  309. // Only process image files.
  310. var type = file.type && file.type.split('/').shift();
  311. if (window.FileReader && type === 'image') {
  312. var reader = new FileReader();
  313. reader.onload = function (e) {
  314. var blob = new Blob([e.target.result]);
  315. window.URL = window.URL || window.webkitURL;
  316. var originalUrl = window.URL.createObjectURL(blob);
  317. var image = new Image();
  318. image.src = originalUrl;
  319. image.onload = function () {
  320. var url = crop(image);
  321. deferred.resolve(url);
  322. };
  323. };
  324. reader.readAsArrayBuffer(file);
  325. } else {
  326. deferred.reject();
  327. }
  328. return deferred;
  329. };
  330. var crop = function(img) {
  331. var canvas = document.createElement('canvas'),
  332. width = img.width,
  333. height = img.height,
  334. x, y, size;
  335. // calculate the width and height, constraining the proportions
  336. if (width > height) {
  337. y = 0;
  338. x = (width - height) / 2;
  339. } else {
  340. y = (height - width) / 2;
  341. x = 0;
  342. }
  343. size = Math.min(width, height);
  344. // resize the canvas and draw the image data into it
  345. canvas.width = 64;
  346. canvas.height = 64;
  347. var ctx = canvas.getContext("2d");
  348. ctx.drawImage(img, x, y, size, size, 0, 0, 64, 64);
  349. return canvas.toDataURL("image/png", 0.7);
  350. };
  351. var addConflict = function($conflicts, original, replacement) {
  352. var $conflict = $conflicts.find('.template').clone().removeClass('template').addClass('conflict');
  353. var $originalDiv = $conflict.find('.original');
  354. var $replacementDiv = $conflict.find('.replacement');
  355. $conflict.data('data',data);
  356. $conflict.find('.filename').text(original.name);
  357. $originalDiv.find('.size').text(humanFileSize(original.size));
  358. $originalDiv.find('.mtime').text(formatDate(original.mtime));
  359. // ie sucks
  360. if (replacement.size && replacement.lastModifiedDate) {
  361. $replacementDiv.find('.size').text(humanFileSize(replacement.size));
  362. $replacementDiv.find('.mtime').text(formatDate(replacement.lastModifiedDate));
  363. }
  364. var path = original.directory + '/' +original.name;
  365. Files.lazyLoadPreview(path, original.mimetype, function(previewpath){
  366. $originalDiv.find('.icon').css('background-image','url('+previewpath+')');
  367. }, 96, 96, original.etag);
  368. getCroppedPreview(replacement).then(
  369. function(path){
  370. $replacementDiv.find('.icon').css('background-image','url(' + path + ')');
  371. }, function(){
  372. Files.getMimeIcon(replacement.type,function(path){
  373. $replacementDiv.find('.icon').css('background-image','url(' + path + ')');
  374. });
  375. }
  376. );
  377. $conflicts.append($conflict);
  378. //set more recent mtime bold
  379. // ie sucks
  380. if (replacement.lastModifiedDate && replacement.lastModifiedDate.getTime() > original.mtime) {
  381. $replacementDiv.find('.mtime').css('font-weight', 'bold');
  382. } else if (replacement.lastModifiedDate && replacement.lastModifiedDate.getTime() < original.mtime) {
  383. $originalDiv.find('.mtime').css('font-weight', 'bold');
  384. } else {
  385. //TODO add to same mtime collection?
  386. }
  387. // set bigger size bold
  388. if (replacement.size && replacement.size > original.size) {
  389. $replacementDiv.find('.size').css('font-weight', 'bold');
  390. } else if (replacement.size && replacement.size < original.size) {
  391. $originalDiv.find('.size').css('font-weight', 'bold');
  392. } else {
  393. //TODO add to same size collection?
  394. }
  395. //TODO show skip action for files with same size and mtime in bottom row
  396. // always keep readonly files
  397. if (original.status === 'readonly') {
  398. $originalDiv
  399. .addClass('readonly')
  400. .find('input[type="checkbox"]')
  401. .prop('checked', true)
  402. .prop('disabled', true);
  403. $originalDiv.find('.message')
  404. .text(t('core','read-only'))
  405. }
  406. };
  407. //var selection = controller.getSelection(data.originalFiles);
  408. //if (selection.defaultAction) {
  409. // controller[selection.defaultAction](data);
  410. //} else {
  411. var dialogName = 'oc-dialog-fileexists-content';
  412. var dialogId = '#' + dialogName;
  413. if (this._fileexistsshown) {
  414. // add conflict
  415. var $conflicts = $(dialogId+ ' .conflicts');
  416. addConflict($conflicts, original, replacement);
  417. var count = $(dialogId+ ' .conflict').length;
  418. var title = n('core',
  419. '{count} file conflict',
  420. '{count} file conflicts',
  421. count,
  422. {count:count}
  423. );
  424. $(dialogId).parent().children('.oc-dialog-title').text(title);
  425. //recalculate dimensions
  426. $(window).trigger('resize');
  427. } else {
  428. //create dialog
  429. this._fileexistsshown = true;
  430. $.when(this._getFileExistsTemplate()).then(function($tmpl) {
  431. var title = t('core','One file conflict');
  432. var $dlg = $tmpl.octemplate({
  433. dialog_name: dialogName,
  434. title: title,
  435. type: 'fileexists',
  436. allnewfiles: t('core','New Files'),
  437. allexistingfiles: t('core','Already existing files'),
  438. why: t('core','Which files do you want to keep?'),
  439. what: t('core','If you select both versions, the copied file will have a number added to its name.')
  440. });
  441. $('body').append($dlg);
  442. var $conflicts = $dlg.find('.conflicts');
  443. addConflict($conflicts, original, replacement);
  444. var buttonlist = [{
  445. text: t('core', 'Cancel'),
  446. classes: 'cancel',
  447. click: function(){
  448. if ( typeof controller.onCancel !== 'undefined') {
  449. controller.onCancel(data);
  450. }
  451. $(dialogId).ocdialog('close');
  452. }
  453. },
  454. {
  455. text: t('core', 'Continue'),
  456. classes: 'continue',
  457. click: function(){
  458. if ( typeof controller.onContinue !== 'undefined') {
  459. controller.onContinue($(dialogId + ' .conflict'));
  460. }
  461. $(dialogId).ocdialog('close');
  462. }
  463. }];
  464. $(dialogId).ocdialog({
  465. width: 500,
  466. closeOnEscape: true,
  467. modal: true,
  468. buttons: buttonlist,
  469. closeButton: null,
  470. close: function() {
  471. self._fileexistsshown = false;
  472. $(this).ocdialog('destroy').remove();
  473. }
  474. });
  475. $(dialogId).css('height','auto');
  476. //add checkbox toggling actions
  477. $(dialogId).find('.allnewfiles').on('click', function() {
  478. var $checkboxes = $(dialogId).find('.conflict .replacement input[type="checkbox"]');
  479. $checkboxes.prop('checked', $(this).prop('checked'));
  480. });
  481. $(dialogId).find('.allexistingfiles').on('click', function() {
  482. var $checkboxes = $(dialogId).find('.conflict .original:not(.readonly) input[type="checkbox"]');
  483. $checkboxes.prop('checked', $(this).prop('checked'));
  484. });
  485. $(dialogId).find('.conflicts').on('click', '.replacement,.original:not(.readonly)', function() {
  486. var $checkbox = $(this).find('input[type="checkbox"]');
  487. $checkbox.prop('checked', !$checkbox.prop('checked'));
  488. });
  489. $(dialogId).find('.conflicts').on('click', '.replacement input[type="checkbox"],.original:not(.readonly) input[type="checkbox"]', function() {
  490. var $checkbox = $(this);
  491. $checkbox.prop('checked', !checkbox.prop('checked'));
  492. });
  493. //update counters
  494. $(dialogId).on('click', '.replacement,.allnewfiles', function() {
  495. var count = $(dialogId).find('.conflict .replacement input[type="checkbox"]:checked').length;
  496. if (count === $(dialogId+ ' .conflict').length) {
  497. $(dialogId).find('.allnewfiles').prop('checked', true);
  498. $(dialogId).find('.allnewfiles + .count').text(t('core','(all selected)'));
  499. } else if (count > 0) {
  500. $(dialogId).find('.allnewfiles').prop('checked', false);
  501. $(dialogId).find('.allnewfiles + .count').text(t('core','({count} selected)',{count:count}));
  502. } else {
  503. $(dialogId).find('.allnewfiles').prop('checked', false);
  504. $(dialogId).find('.allnewfiles + .count').text('');
  505. }
  506. });
  507. $(dialogId).on('click', '.original,.allexistingfiles', function(){
  508. var count = $(dialogId).find('.conflict .original input[type="checkbox"]:checked').length;
  509. if (count === $(dialogId+ ' .conflict').length) {
  510. $(dialogId).find('.allexistingfiles').prop('checked', true);
  511. $(dialogId).find('.allexistingfiles + .count').text(t('core','(all selected)'));
  512. } else if (count > 0) {
  513. $(dialogId).find('.allexistingfiles').prop('checked', false);
  514. $(dialogId).find('.allexistingfiles + .count')
  515. .text(t('core','({count} selected)',{count:count}));
  516. } else {
  517. $(dialogId).find('.allexistingfiles').prop('checked', false);
  518. $(dialogId).find('.allexistingfiles + .count').text('');
  519. }
  520. });
  521. })
  522. .fail(function() {
  523. alert(t('core', 'Error loading file exists template'));
  524. });
  525. }
  526. //}
  527. },
  528. _getFilePickerTemplate: function() {
  529. var defer = $.Deferred();
  530. if(!this.$filePickerTemplate) {
  531. var self = this;
  532. $.get(OC.filePath('core', 'templates', 'filepicker.html'), function(tmpl) {
  533. self.$filePickerTemplate = $(tmpl);
  534. self.$listTmpl = self.$filePickerTemplate.find('.filelist li:first-child').detach();
  535. defer.resolve(self.$filePickerTemplate);
  536. })
  537. .fail(function(jqXHR, textStatus, errorThrown) {
  538. defer.reject(jqXHR.status, errorThrown);
  539. });
  540. } else {
  541. defer.resolve(this.$filePickerTemplate);
  542. }
  543. return defer.promise();
  544. },
  545. _getMessageTemplate: function() {
  546. var defer = $.Deferred();
  547. if(!this.$messageTemplate) {
  548. var self = this;
  549. $.get(OC.filePath('core', 'templates', 'message.html'), function(tmpl) {
  550. self.$messageTemplate = $(tmpl);
  551. defer.resolve(self.$messageTemplate);
  552. })
  553. .fail(function(jqXHR, textStatus, errorThrown) {
  554. defer.reject(jqXHR.status, errorThrown);
  555. });
  556. } else {
  557. defer.resolve(this.$messageTemplate);
  558. }
  559. return defer.promise();
  560. },
  561. _getFileExistsTemplate: function () {
  562. var defer = $.Deferred();
  563. if (!this.$fileexistsTemplate) {
  564. var self = this;
  565. $.get(OC.filePath('files', 'templates', 'fileexists.html'), function (tmpl) {
  566. self.$fileexistsTemplate = $(tmpl);
  567. defer.resolve(self.$fileexistsTemplate);
  568. })
  569. .fail(function () {
  570. defer.reject();
  571. });
  572. } else {
  573. defer.resolve(this.$fileexistsTemplate);
  574. }
  575. return defer.promise();
  576. },
  577. _getFileList: function(dir, mimeType) {
  578. if (typeof(mimeType) === "string") {
  579. mimeType = [mimeType];
  580. }
  581. return $.getJSON(
  582. OC.filePath('files', 'ajax', 'list.php'),
  583. {
  584. dir: dir,
  585. mimetypes: JSON.stringify(mimeType)
  586. }
  587. );
  588. },
  589. /**
  590. * fills the filepicker with files
  591. */
  592. _fillFilePicker:function(dir) {
  593. var dirs = [];
  594. var others = [];
  595. var self = this;
  596. this.$filelist.empty().addClass('loading');
  597. this.$filePicker.data('path', dir);
  598. $.when(this._getFileList(dir, this.$filePicker.data('mimetype'))).then(function(response) {
  599. $.each(response.data.files, function(index, file) {
  600. if (file.type === 'dir') {
  601. dirs.push(file);
  602. } else {
  603. others.push(file);
  604. }
  605. });
  606. self._fillSlug();
  607. var sorted = dirs.concat(others);
  608. $.each(sorted, function(idx, entry) {
  609. var $li = self.$listTmpl.octemplate({
  610. type: entry.type,
  611. dir: dir,
  612. filename: entry.name,
  613. date: OC.Util.relativeModifiedDate(entry.mtime)
  614. });
  615. if (entry.isPreviewAvailable) {
  616. var urlSpec = {
  617. file: dir + '/' + entry.name
  618. };
  619. var previewUrl = OC.generateUrl('/core/preview.png?') + $.param(urlSpec);
  620. $li.find('img').attr('src', previewUrl);
  621. }
  622. else {
  623. $li.find('img').attr('src', OC.Util.replaceSVGIcon(entry.icon));
  624. }
  625. self.$filelist.append($li);
  626. });
  627. self.$filelist.removeClass('loading');
  628. if (!OC.Util.hasSVGSupport()) {
  629. OC.Util.replaceSVG(self.$filePicker.find('.dirtree'));
  630. }
  631. });
  632. },
  633. /**
  634. * fills the tree list with directories
  635. */
  636. _fillSlug: function() {
  637. this.$dirTree.empty();
  638. var self = this;
  639. var path = this.$filePicker.data('path');
  640. var $template = $('<span data-dir="{dir}">{name}</span>');
  641. if(path) {
  642. var paths = path.split('/');
  643. $.each(paths, function(index, dir) {
  644. dir = paths.pop();
  645. if(dir === '') {
  646. return false;
  647. }
  648. self.$dirTree.prepend($template.octemplate({
  649. dir: paths.join('/') + '/' + dir,
  650. name: dir
  651. }));
  652. });
  653. }
  654. $template.octemplate({
  655. dir: '',
  656. name: '&nbsp;&nbsp;&nbsp;&nbsp;' // Ugly but works ;)
  657. }, {escapeFunction: null}).addClass('home svg').prependTo(this.$dirTree);
  658. },
  659. /**
  660. * handle selection made in the tree list
  661. */
  662. _handleTreeListSelect:function(event) {
  663. var self = event.data;
  664. var dir = $(event.target).data('dir');
  665. self._fillFilePicker(dir);
  666. },
  667. /**
  668. * handle clicks made in the filepicker
  669. */
  670. _handlePickerClick:function(event, $element) {
  671. if ($element.data('type') === 'file') {
  672. if (this.$filePicker.data('multiselect') !== true || !event.ctrlKey) {
  673. this.$filelist.find('.filepicker_element_selected').removeClass('filepicker_element_selected');
  674. }
  675. $element.toggleClass('filepicker_element_selected');
  676. } else if ( $element.data('type') === 'dir' ) {
  677. this._fillFilePicker(this.$filePicker.data('path') + '/' + $element.data('entryname'));
  678. }
  679. }
  680. };