sharedialoglinkshareview.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2015
  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. /* globals Clipboard, Handlebars */
  11. (function() {
  12. if (!OC.Share) {
  13. OC.Share = {};
  14. }
  15. var PASSWORD_PLACEHOLDER = '**********';
  16. var PASSWORD_PLACEHOLDER_MESSAGE = t('core', 'Choose a password for the public link');
  17. var TEMPLATE =
  18. '{{#if shareAllowed}}' +
  19. '<span class="icon-loading-small hidden"></span>' +
  20. '<input type="checkbox" name="linkCheckbox" id="linkCheckbox-{{cid}}" class="checkbox linkCheckbox" value="1" {{#if isLinkShare}}checked="checked"{{/if}} />' +
  21. '<label for="linkCheckbox-{{cid}}">{{linkShareLabel}}</label>' +
  22. '<br />' +
  23. '<div class="oneline">' +
  24. '<label for="linkText-{{cid}}" class="hidden-visually">{{urlLabel}}</label>' +
  25. '<input id="linkText-{{cid}}" class="linkText {{#unless isLinkShare}}hidden{{/unless}}" type="text" readonly="readonly" value="{{shareLinkURL}}" />' +
  26. '<a class="{{#unless isLinkShare}}hidden-visually{{/unless}} clipboardButton icon icon-clippy" data-clipboard-target="#linkText-{{cid}}"></a>' +
  27. '</div>' +
  28. ' {{#if publicUpload}}' +
  29. '<div id="allowPublicUploadWrapper">' +
  30. ' <span class="icon-loading-small hidden"></span>' +
  31. ' <input type="checkbox" value="1" name="allowPublicUpload" id="sharingDialogAllowPublicUpload-{{cid}}" class="checkbox publicUploadCheckbox" {{{publicUploadChecked}}} />' +
  32. '<label for="sharingDialogAllowPublicUpload-{{cid}}">{{publicUploadLabel}}</label>' +
  33. '</div>' +
  34. ' {{#if hideFileList}}' +
  35. '<div id="hideFileListWrapper">' +
  36. ' <span class="icon-loading-small hidden"></span>' +
  37. ' <input type="checkbox" value="1" name="hideFileList" id="sharingDialogHideFileList-{{cid}}" class="checkbox hideFileListCheckbox" {{{hideFileListChecked}}} />' +
  38. '<label for="sharingDialogHideFileList-{{cid}}">{{hideFileListLabel}}</label>' +
  39. '</div>' +
  40. ' {{/if}}' +
  41. ' {{/if}}' +
  42. ' {{#if showPasswordCheckBox}}' +
  43. '<input type="checkbox" name="showPassword" id="showPassword-{{cid}}" class="checkbox showPasswordCheckbox" {{#if isPasswordSet}}checked="checked"{{/if}} value="1" />' +
  44. '<label for="showPassword-{{cid}}">{{enablePasswordLabel}}</label>' +
  45. ' {{/if}}' +
  46. '<div id="linkPass" class="linkPass {{#unless isPasswordSet}}hidden{{/unless}}">' +
  47. ' <label for="linkPassText-{{cid}}" class="hidden-visually">{{passwordLabel}}</label>' +
  48. ' <input id="linkPassText-{{cid}}" class="linkPassText" type="password" placeholder="{{passwordPlaceholder}}" />' +
  49. ' <span class="icon-loading-small hidden"></span>' +
  50. '</div>' +
  51. '{{else}}' +
  52. // FIXME: this doesn't belong in this view
  53. '{{#if noSharingPlaceholder}}<input id="shareWith-{{cid}}" class="shareWithField" type="text" placeholder="{{noSharingPlaceholder}}" disabled="disabled"/>{{/if}}' +
  54. '{{/if}}'
  55. ;
  56. /**
  57. * @class OCA.Share.ShareDialogLinkShareView
  58. * @member {OC.Share.ShareItemModel} model
  59. * @member {jQuery} $el
  60. * @memberof OCA.Sharing
  61. * @classdesc
  62. *
  63. * Represents the GUI of the share dialogue
  64. *
  65. */
  66. var ShareDialogLinkShareView = OC.Backbone.View.extend({
  67. /** @type {string} **/
  68. id: 'shareDialogLinkShare',
  69. /** @type {OC.Share.ShareConfigModel} **/
  70. configModel: undefined,
  71. /** @type {Function} **/
  72. _template: undefined,
  73. /** @type {boolean} **/
  74. showLink: true,
  75. events: {
  76. 'focusout input.linkPassText': 'onPasswordEntered',
  77. 'keyup input.linkPassText': 'onPasswordKeyUp',
  78. 'click .linkCheckbox': 'onLinkCheckBoxChange',
  79. 'click .linkText': 'onLinkTextClick',
  80. 'change .publicUploadCheckbox': 'onAllowPublicUploadChange',
  81. 'change .hideFileListCheckbox': 'onHideFileListChange',
  82. 'click .showPasswordCheckbox': 'onShowPasswordClick'
  83. },
  84. initialize: function(options) {
  85. var view = this;
  86. this.model.on('change:permissions', function() {
  87. view.render();
  88. });
  89. this.model.on('change:itemType', function() {
  90. view.render();
  91. });
  92. this.model.on('change:allowPublicUploadStatus', function() {
  93. view.render();
  94. });
  95. this.model.on('change:hideFileListStatus', function() {
  96. view.render();
  97. });
  98. this.model.on('change:linkShare', function() {
  99. view.render();
  100. });
  101. if(!_.isUndefined(options.configModel)) {
  102. this.configModel = options.configModel;
  103. } else {
  104. throw 'missing OC.Share.ShareConfigModel';
  105. }
  106. _.bindAll(
  107. this,
  108. 'onLinkCheckBoxChange',
  109. 'onPasswordEntered',
  110. 'onPasswordKeyUp',
  111. 'onLinkTextClick',
  112. 'onShowPasswordClick',
  113. 'onHideFileListChange',
  114. 'onAllowPublicUploadChange'
  115. );
  116. var clipboard = new Clipboard('.clipboardButton');
  117. clipboard.on('success', function(e) {
  118. var $input = $(e.trigger);
  119. $input.tooltip({placement: 'bottom', trigger: 'manual', title: t('core', 'Copied!')});
  120. $input.tooltip('show');
  121. _.delay(function() {
  122. $input.tooltip('hide');
  123. }, 3000);
  124. });
  125. clipboard.on('error', function (e) {
  126. var $input = $(e.trigger);
  127. var actionMsg = '';
  128. if (/iPhone|iPad/i.test(navigator.userAgent)) {
  129. actionMsg = t('core', 'Not supported!');
  130. } else if (/Mac/i.test(navigator.userAgent)) {
  131. actionMsg = t('core', 'Press ⌘-C to copy.');
  132. } else {
  133. actionMsg = t('core', 'Press Ctrl-C to copy.');
  134. }
  135. $input.tooltip({
  136. placement: 'bottom',
  137. trigger: 'manual',
  138. title: actionMsg
  139. });
  140. $input.tooltip('show');
  141. _.delay(function () {
  142. $input.tooltip('hide');
  143. }, 3000);
  144. });
  145. },
  146. onLinkCheckBoxChange: function() {
  147. var $checkBox = this.$el.find('.linkCheckbox');
  148. var $loading = $checkBox.siblings('.icon-loading-small');
  149. if(!$loading.hasClass('hidden')) {
  150. return false;
  151. }
  152. if($checkBox.is(':checked')) {
  153. if(this.configModel.get('enforcePasswordForPublicLink') === false) {
  154. $loading.removeClass('hidden');
  155. // this will create it
  156. this.model.saveLinkShare();
  157. } else {
  158. this.$el.find('.linkPass').slideToggle(OC.menuSpeed);
  159. this.$el.find('.linkPassText').focus();
  160. }
  161. } else {
  162. if (this.model.get('linkShare').isLinkShare) {
  163. $loading.removeClass('hidden');
  164. this.model.removeLinkShare();
  165. } else {
  166. this.$el.find('.linkPass').slideToggle(OC.menuSpeed);
  167. }
  168. }
  169. },
  170. onLinkTextClick: function() {
  171. var $el = this.$el.find('.linkText');
  172. $el.focus();
  173. $el.select();
  174. },
  175. onShowPasswordClick: function() {
  176. this.$el.find('.linkPass').slideToggle(OC.menuSpeed);
  177. if(!this.$el.find('.showPasswordCheckbox').is(':checked')) {
  178. this.model.saveLinkShare({
  179. password: ''
  180. });
  181. } else {
  182. this.$el.find('.linkPassText').focus();
  183. }
  184. },
  185. onPasswordKeyUp: function(event) {
  186. if(event.keyCode === 13) {
  187. this.onPasswordEntered();
  188. }
  189. },
  190. onPasswordEntered: function() {
  191. var $loading = this.$el.find('.linkPass .icon-loading-small');
  192. if (!$loading.hasClass('hidden')) {
  193. // still in process
  194. return;
  195. }
  196. var $input = this.$el.find('.linkPassText');
  197. $input.removeClass('error');
  198. var password = $input.val();
  199. // in IE9 the password might be the placeholder due to bugs in the placeholders polyfill
  200. if(password === '' || password === PASSWORD_PLACEHOLDER || password === PASSWORD_PLACEHOLDER_MESSAGE) {
  201. return;
  202. }
  203. $loading
  204. .removeClass('hidden')
  205. .addClass('inlineblock');
  206. this.model.saveLinkShare({
  207. password: password
  208. }, {
  209. error: function(model, msg) {
  210. // destroy old tooltips
  211. $input.tooltip('destroy');
  212. $loading.removeClass('inlineblock').addClass('hidden');
  213. $input.addClass('error');
  214. $input.attr('title', msg);
  215. $input.tooltip({placement: 'bottom', trigger: 'manual'});
  216. $input.tooltip('show');
  217. }
  218. });
  219. },
  220. onAllowPublicUploadChange: function() {
  221. var $checkbox = this.$('.publicUploadCheckbox');
  222. $checkbox.siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  223. var permissions = OC.PERMISSION_READ;
  224. if($checkbox.is(':checked')) {
  225. permissions = OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ | OC.PERMISSION_DELETE;
  226. }
  227. this.model.saveLinkShare({
  228. permissions: permissions
  229. });
  230. },
  231. onHideFileListChange: function () {
  232. var $checkbox = this.$('.hideFileListCheckbox');
  233. $checkbox.siblings('.icon-loading-small').removeClass('hidden').addClass('inlineblock');
  234. var permissions = OC.PERMISSION_UPDATE | OC.PERMISSION_CREATE | OC.PERMISSION_READ | OC.PERMISSION_DELETE;
  235. if ($checkbox.is(':checked')) {
  236. permissions = OC.PERMISSION_CREATE;
  237. }
  238. this.model.saveLinkShare({
  239. permissions: permissions
  240. });
  241. },
  242. render: function() {
  243. var linkShareTemplate = this.template();
  244. var resharingAllowed = this.model.sharePermissionPossible();
  245. if(!resharingAllowed
  246. || !this.showLink
  247. || !this.configModel.isShareWithLinkAllowed())
  248. {
  249. var templateData = {shareAllowed: false};
  250. if (!resharingAllowed) {
  251. // add message
  252. templateData.noSharingPlaceholder = t('core', 'Resharing is not allowed');
  253. }
  254. this.$el.html(linkShareTemplate(templateData));
  255. return this;
  256. }
  257. var publicUpload =
  258. this.model.isFolder()
  259. && this.model.createPermissionPossible()
  260. && this.configModel.isPublicUploadEnabled();
  261. var publicUploadChecked = '';
  262. if(this.model.isPublicUploadAllowed()) {
  263. publicUploadChecked = 'checked="checked"';
  264. }
  265. var hideFileList = publicUploadChecked;
  266. var hideFileListChecked = '';
  267. if(this.model.isHideFileListSet()) {
  268. hideFileListChecked = 'checked="checked"';
  269. }
  270. var isLinkShare = this.model.get('linkShare').isLinkShare;
  271. var isPasswordSet = !!this.model.get('linkShare').password;
  272. var showPasswordCheckBox = isLinkShare
  273. && ( !this.configModel.get('enforcePasswordForPublicLink')
  274. || !this.model.get('linkShare').password);
  275. this.$el.html(linkShareTemplate({
  276. cid: this.cid,
  277. shareAllowed: true,
  278. hideFileList: hideFileList,
  279. isLinkShare: isLinkShare,
  280. shareLinkURL: this.model.get('linkShare').link,
  281. linkShareLabel: t('core', 'Share link'),
  282. urlLabel: t('core', 'Link'),
  283. enablePasswordLabel: t('core', 'Password protect'),
  284. passwordLabel: t('core', 'Password'),
  285. passwordPlaceholder: isPasswordSet ? PASSWORD_PLACEHOLDER : PASSWORD_PLACEHOLDER_MESSAGE,
  286. isPasswordSet: isPasswordSet,
  287. showPasswordCheckBox: showPasswordCheckBox,
  288. publicUpload: publicUpload && isLinkShare,
  289. publicUploadChecked: publicUploadChecked,
  290. hideFileListChecked: hideFileListChecked,
  291. publicUploadLabel: t('core', 'Allow editing'),
  292. hideFileListLabel: t('core', 'Hide file listing'),
  293. mailPublicNotificationEnabled: isLinkShare && this.configModel.isMailPublicNotificationEnabled(),
  294. mailPrivatePlaceholder: t('core', 'Email link to person'),
  295. mailButtonText: t('core', 'Send')
  296. }));
  297. this.delegateEvents();
  298. return this;
  299. },
  300. /**
  301. * @returns {Function} from Handlebars
  302. * @private
  303. */
  304. template: function () {
  305. if (!this._template) {
  306. this._template = Handlebars.compile(TEMPLATE);
  307. }
  308. return this._template;
  309. }
  310. });
  311. OC.Share.ShareDialogLinkShareView = ShareDialogLinkShareView;
  312. })();