1
0

newfilemenu.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2014
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. /* global Files */
  11. (function() {
  12. /**
  13. * Construct a new NewFileMenu instance
  14. * @constructs NewFileMenu
  15. *
  16. * @memberof OCA.Files
  17. */
  18. var NewFileMenu = OC.Backbone.View.extend({
  19. tagName: 'div',
  20. // Menu is opened by default because it's rendered on "add-button" click
  21. className: 'newFileMenu popovermenu bubble menu open menu-left',
  22. events: {
  23. 'click .menuitem': '_onClickAction'
  24. },
  25. /**
  26. * @type OCA.Files.FileList
  27. */
  28. fileList: null,
  29. initialize: function(options) {
  30. var self = this;
  31. var $uploadEl = $('#file_upload_start');
  32. if ($uploadEl.length) {
  33. $uploadEl.on('fileuploadstart', function() {
  34. self.trigger('actionPerformed', 'upload');
  35. });
  36. } else {
  37. console.warn('Missing upload element "file_upload_start"');
  38. }
  39. this.fileList = options && options.fileList;
  40. this._menuItems = [{
  41. id: 'folder',
  42. displayName: t('files', 'New folder'),
  43. templateName: t('files', 'New folder'),
  44. iconClass: 'icon-folder',
  45. fileType: 'folder',
  46. actionHandler: function(name) {
  47. self.fileList.createDirectory(name);
  48. }
  49. }];
  50. OC.Plugins.attach('OCA.Files.NewFileMenu', this);
  51. },
  52. template: function(data) {
  53. return OCA.Files.Templates['newfilemenu'](data);
  54. },
  55. /**
  56. * Event handler whenever an action has been clicked within the menu
  57. *
  58. * @param {Object} event event object
  59. */
  60. _onClickAction: function(event) {
  61. var $target = $(event.target);
  62. if (!$target.hasClass('menuitem')) {
  63. $target = $target.closest('.menuitem');
  64. }
  65. var action = $target.attr('data-action');
  66. // note: clicking the upload label will automatically
  67. // set the focus on the "file_upload_start" hidden field
  68. // which itself triggers the upload dialog.
  69. // Currently the upload logic is still in file-upload.js and filelist.js
  70. if (action === 'upload') {
  71. OC.hideMenus();
  72. } else {
  73. event.preventDefault();
  74. this.$el.find('.menuitem.active').removeClass('active');
  75. $target.addClass('active');
  76. this._promptFileName($target);
  77. }
  78. },
  79. _promptFileName: function($target) {
  80. var self = this;
  81. if ($target.find('form').length) {
  82. $target.find('input[type=\'text\']').focus();
  83. return;
  84. }
  85. // discard other forms
  86. this.$el.find('form').remove();
  87. this.$el.find('.displayname').removeClass('hidden');
  88. $target.find('.displayname').addClass('hidden');
  89. var newName = $target.attr('data-templatename');
  90. var fileType = $target.attr('data-filetype');
  91. var $form = $(OCA.Files.Templates['newfilemenu_filename_form']({
  92. fileName: newName,
  93. cid: this.cid,
  94. fileType: fileType
  95. }));
  96. //this.trigger('actionPerformed', action);
  97. $target.append($form);
  98. // here comes the OLD code
  99. var $input = $form.find('input[type=\'text\']');
  100. var $submit = $form.find('input[type=\'submit\']');
  101. var lastPos;
  102. var checkInput = function () {
  103. // Special handling for the setup template directory
  104. if ($target.attr('data-action') === 'template-init') {
  105. return true;
  106. }
  107. var filename = $input.val();
  108. try {
  109. if (!Files.isFileNameValid(filename)) {
  110. // Files.isFileNameValid(filename) throws an exception itself
  111. } else if (self.fileList.inList(filename)) {
  112. throw t('files', '{newName} already exists', {newName: filename}, undefined, {
  113. escape: false
  114. });
  115. } else {
  116. return true;
  117. }
  118. } catch (error) {
  119. $input.attr('title', error);
  120. $input.addClass('error');
  121. }
  122. return false;
  123. };
  124. // verify filename on typing
  125. $input.keyup(function() {
  126. if (checkInput()) {
  127. $input.removeClass('error');
  128. }
  129. });
  130. $submit.click(function(event) {
  131. event.stopPropagation();
  132. event.preventDefault();
  133. $form.submit();
  134. });
  135. $input.focus();
  136. // pre select name up to the extension
  137. lastPos = newName.lastIndexOf('.');
  138. if (lastPos === -1) {
  139. lastPos = newName.length;
  140. }
  141. $input.selectRange(0, lastPos);
  142. $form.submit(function(event) {
  143. event.stopPropagation();
  144. event.preventDefault();
  145. if (checkInput()) {
  146. var newname = $input.val().trim();
  147. /* Find the right actionHandler that should be called.
  148. * Actions is retrieved by using `actionSpec.id` */
  149. var action = _.filter(self._menuItems, function(item) {
  150. return item.id == $target.attr('data-action');
  151. }).pop();
  152. action.actionHandler(newname);
  153. $form.remove();
  154. $target.find('.displayname').removeClass('hidden');
  155. OC.hideMenus();
  156. }
  157. });
  158. },
  159. /**
  160. * Add a new item menu entry in the “New” file menu (in
  161. * last position). By clicking on the item, the
  162. * `actionHandler` function is called.
  163. *
  164. * @param {Object} actionSpec item’s properties
  165. */
  166. addMenuEntry: function(actionSpec) {
  167. this._menuItems.push({
  168. id: actionSpec.id,
  169. displayName: actionSpec.displayName,
  170. templateName: actionSpec.templateName,
  171. iconClass: actionSpec.iconClass,
  172. fileType: actionSpec.fileType,
  173. actionHandler: actionSpec.actionHandler,
  174. checkFilename: actionSpec.checkFilename
  175. });
  176. },
  177. /**
  178. * Remove a menu item from the "New" file menu
  179. * @param {string} actionId
  180. */
  181. removeMenuEntry: function(actionId) {
  182. var index = this._menuItems.findIndex(function (actionSpec) {
  183. return actionSpec.id === actionId;
  184. });
  185. if (index > -1) {
  186. this._menuItems.splice(index, 1);
  187. }
  188. },
  189. /**
  190. * Renders the menu with the currently set items
  191. */
  192. render: function() {
  193. this.$el.html(this.template({
  194. uploadMaxHumanFileSize: 'TODO',
  195. uploadLabel: t('files', 'Upload file'),
  196. items: this._menuItems
  197. }));
  198. // Trigger upload action also with keyboard navigation on enter
  199. this.$el.find('[for="file_upload_start"]').on('keyup', function(event) {
  200. if (event.key === " " || event.key === "Enter") {
  201. $('#file_upload_start').trigger('click');
  202. }
  203. });
  204. },
  205. /**
  206. * Displays the menu under the given element
  207. *
  208. * @param {Object} $target target element
  209. */
  210. showAt: function($target) {
  211. this.render();
  212. OC.showMenu($target, this.$el);
  213. }
  214. });
  215. OCA.Files.NewFileMenu = NewFileMenu;
  216. })();