You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  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. if (!OCA.Sharing) {
  11. /**
  12. * @namespace OCA.Sharing
  13. */
  14. OCA.Sharing = {}
  15. }
  16. /**
  17. * @namespace
  18. */
  19. OCA.Sharing.App = {
  20. _inFileList: null,
  21. _outFileList: null,
  22. _overviewFileList: null,
  23. _pendingFileList: null,
  24. initSharingIn($el) {
  25. if (this._inFileList) {
  26. return this._inFileList
  27. }
  28. this._inFileList = new OCA.Sharing.FileList(
  29. $el,
  30. {
  31. id: 'shares.self',
  32. sharedWithUser: true,
  33. fileActions: this._createFileActions(),
  34. config: OCA.Files.App.getFilesConfig(),
  35. // The file list is created when a "show" event is handled, so
  36. // it should be marked as "shown" like it would have been done
  37. // if handling the event with the file list already created.
  38. shown: true,
  39. }
  40. )
  41. this._extendFileList(this._inFileList)
  42. this._inFileList.appName = t('files_sharing', 'Shared with you')
  43. this._inFileList.$el.find('#emptycontent').html('<div class="icon-shared"></div>'
  44. + '<h2>' + t('files_sharing', 'Nothing shared with you yet') + '</h2>'
  45. + '<p>' + t('files_sharing', 'Files and folders others share with you will show up here') + '</p>')
  46. return this._inFileList
  47. },
  48. initSharingOut($el) {
  49. if (this._outFileList) {
  50. return this._outFileList
  51. }
  52. this._outFileList = new OCA.Sharing.FileList(
  53. $el,
  54. {
  55. id: 'shares.others',
  56. sharedWithUser: false,
  57. fileActions: this._createFileActions(),
  58. config: OCA.Files.App.getFilesConfig(),
  59. // The file list is created when a "show" event is handled, so
  60. // it should be marked as "shown" like it would have been done
  61. // if handling the event with the file list already created.
  62. shown: true,
  63. }
  64. )
  65. this._extendFileList(this._outFileList)
  66. this._outFileList.appName = t('files_sharing', 'Shared with others')
  67. this._outFileList.$el.find('#emptycontent').html('<div class="icon-shared"></div>'
  68. + '<h2>' + t('files_sharing', 'Nothing shared yet') + '</h2>'
  69. + '<p>' + t('files_sharing', 'Files and folders you share will show up here') + '</p>')
  70. return this._outFileList
  71. },
  72. initSharingLinks($el) {
  73. if (this._linkFileList) {
  74. return this._linkFileList
  75. }
  76. this._linkFileList = new OCA.Sharing.FileList(
  77. $el,
  78. {
  79. id: 'shares.link',
  80. linksOnly: true,
  81. fileActions: this._createFileActions(),
  82. config: OCA.Files.App.getFilesConfig(),
  83. // The file list is created when a "show" event is handled, so
  84. // it should be marked as "shown" like it would have been done
  85. // if handling the event with the file list already created.
  86. shown: true,
  87. }
  88. )
  89. this._extendFileList(this._linkFileList)
  90. this._linkFileList.appName = t('files_sharing', 'Shared by link')
  91. this._linkFileList.$el.find('#emptycontent').html('<div class="icon-public"></div>'
  92. + '<h2>' + t('files_sharing', 'No shared links') + '</h2>'
  93. + '<p>' + t('files_sharing', 'Files and folders you share by link will show up here') + '</p>')
  94. return this._linkFileList
  95. },
  96. initSharingDeleted($el) {
  97. if (this._deletedFileList) {
  98. return this._deletedFileList
  99. }
  100. this._deletedFileList = new OCA.Sharing.FileList(
  101. $el,
  102. {
  103. id: 'shares.deleted',
  104. defaultFileActionsDisabled: true,
  105. showDeleted: true,
  106. sharedWithUser: true,
  107. fileActions: this._restoreShareAction(),
  108. config: OCA.Files.App.getFilesConfig(),
  109. // The file list is created when a "show" event is handled, so
  110. // it should be marked as "shown" like it would have been done
  111. // if handling the event with the file list already created.
  112. shown: true,
  113. }
  114. )
  115. this._extendFileList(this._deletedFileList)
  116. this._deletedFileList.appName = t('files_sharing', 'Deleted shares')
  117. this._deletedFileList.$el.find('#emptycontent').html('<div class="icon-share"></div>'
  118. + '<h2>' + t('files_sharing', 'No deleted shares') + '</h2>'
  119. + '<p>' + t('files_sharing', 'Shares you deleted will show up here') + '</p>')
  120. return this._deletedFileList
  121. },
  122. initSharingPening($el) {
  123. if (this._pendingFileList) {
  124. return this._pendingFileList
  125. }
  126. this._pendingFileList = new OCA.Sharing.FileList(
  127. $el,
  128. {
  129. id: 'shares.pending',
  130. showPending: true,
  131. sharedWithUser: true,
  132. fileActions: this._acceptShareAction(),
  133. config: OCA.Files.App.getFilesConfig(),
  134. // The file list is created when a "show" event is handled, so
  135. // it should be marked as "shown" like it would have been done
  136. // if handling the event with the file list already created.
  137. shown: true,
  138. }
  139. )
  140. this._extendFileList(this._pendingFileList)
  141. this._pendingFileList.appName = t('files_sharing', 'Pending shares')
  142. this._pendingFileList.$el.find('#emptycontent').html('<div class="icon-share"></div>'
  143. + '<h2>' + t('files_sharing', 'No pending shares') + '</h2>'
  144. + '<p>' + t('files_sharing', 'Shares you have received but not confirmed will show up here') + '</p>')
  145. return this._pendingFileList
  146. },
  147. initShareingOverview($el) {
  148. if (this._overviewFileList) {
  149. return this._overviewFileList
  150. }
  151. this._overviewFileList = new OCA.Sharing.FileList(
  152. $el,
  153. {
  154. id: 'shares.overview',
  155. config: OCA.Files.App.getFilesConfig(),
  156. isOverview: true,
  157. // The file list is created when a "show" event is handled, so
  158. // it should be marked as "shown" like it would have been done
  159. // if handling the event with the file list already created.
  160. shown: true,
  161. }
  162. )
  163. this._extendFileList(this._overviewFileList)
  164. this._overviewFileList.appName = t('files_sharing', 'Shares')
  165. this._overviewFileList.$el.find('#emptycontent').html('<div class="icon-share"></div>'
  166. + '<h2>' + t('files_sharing', 'No shares') + '</h2>'
  167. + '<p>' + t('files_sharing', 'Shares will show up here') + '</p>')
  168. return this._overviewFileList
  169. },
  170. removeSharingIn() {
  171. if (this._inFileList) {
  172. this._inFileList.$fileList.empty()
  173. }
  174. },
  175. removeSharingOut() {
  176. if (this._outFileList) {
  177. this._outFileList.$fileList.empty()
  178. }
  179. },
  180. removeSharingLinks() {
  181. if (this._linkFileList) {
  182. this._linkFileList.$fileList.empty()
  183. }
  184. },
  185. removeSharingDeleted() {
  186. if (this._deletedFileList) {
  187. this._deletedFileList.$fileList.empty()
  188. }
  189. },
  190. removeSharingPending() {
  191. if (this._pendingFileList) {
  192. this._pendingFileList.$fileList.empty()
  193. }
  194. },
  195. removeSharingOverview() {
  196. if (this._overviewFileList) {
  197. this._overviewFileList.$fileList.empty()
  198. }
  199. },
  200. /**
  201. * Destroy the app
  202. */
  203. destroy() {
  204. OCA.Files.fileActions.off('setDefault.app-sharing', this._onActionsUpdated)
  205. OCA.Files.fileActions.off('registerAction.app-sharing', this._onActionsUpdated)
  206. this.removeSharingIn()
  207. this.removeSharingOut()
  208. this.removeSharingLinks()
  209. this._inFileList = null
  210. this._outFileList = null
  211. this._linkFileList = null
  212. this._overviewFileList = null
  213. delete this._globalActionsInitialized
  214. },
  215. _createFileActions() {
  216. // inherit file actions from the files app
  217. const fileActions = new OCA.Files.FileActions()
  218. // note: not merging the legacy actions because legacy apps are not
  219. // compatible with the sharing overview and need to be adapted first
  220. fileActions.registerDefaultActions()
  221. fileActions.merge(OCA.Files.fileActions)
  222. if (!this._globalActionsInitialized) {
  223. // in case actions are registered later
  224. this._onActionsUpdated = _.bind(this._onActionsUpdated, this)
  225. OCA.Files.fileActions.on('setDefault.app-sharing', this._onActionsUpdated)
  226. OCA.Files.fileActions.on('registerAction.app-sharing', this._onActionsUpdated)
  227. this._globalActionsInitialized = true
  228. }
  229. // when the user clicks on a folder, redirect to the corresponding
  230. // folder in the files app instead of opening it directly
  231. fileActions.register('dir', 'Open', OC.PERMISSION_READ, '', function(filename, context) {
  232. OCA.Files.App.setActiveView('files', { silent: true })
  233. OCA.Files.App.fileList.changeDirectory(OC.joinPaths(context.$file.attr('data-path'), filename), true, true)
  234. })
  235. fileActions.setDefault('dir', 'Open')
  236. return fileActions
  237. },
  238. _restoreShareAction() {
  239. const fileActions = new OCA.Files.FileActions()
  240. fileActions.registerAction({
  241. name: 'Restore',
  242. displayName: t('files_sharing', 'Restore'),
  243. altText: t('files_sharing', 'Restore share'),
  244. mime: 'all',
  245. permissions: OC.PERMISSION_ALL,
  246. iconClass: 'icon-history',
  247. type: OCA.Files.FileActions.TYPE_INLINE,
  248. actionHandler(fileName, context) {
  249. const shareId = context.$file.data('shareId')
  250. $.post(OC.linkToOCS('apps/files_sharing/api/v1/deletedshares', 2) + shareId)
  251. .success(function(result) {
  252. context.fileList.remove(context.fileInfoModel.attributes.name)
  253. }).fail(function() {
  254. OC.Notification.showTemporary(t('files_sharing', 'Something happened. Unable to restore the share.'))
  255. })
  256. },
  257. })
  258. return fileActions
  259. },
  260. _acceptShareAction() {
  261. const fileActions = new OCA.Files.FileActions()
  262. fileActions.registerAction({
  263. name: 'Accept share',
  264. displayName: t('files_sharing', 'Accept share'),
  265. mime: 'all',
  266. permissions: OC.PERMISSION_ALL,
  267. iconClass: 'icon-checkmark',
  268. type: OCA.Files.FileActions.TYPE_INLINE,
  269. actionHandler(fileName, context) {
  270. const shareId = context.$file.data('shareId')
  271. $.post(OC.linkToOCS('apps/files_sharing/api/v1/shares/pending', 2) + shareId)
  272. .success(function(result) {
  273. context.fileList.remove(context.fileInfoModel.attributes.name)
  274. }).fail(function() {
  275. OC.Notification.showTemporary(t('files_sharing', 'Something happened. Unable to accept the share.'))
  276. })
  277. },
  278. })
  279. fileActions.registerAction({
  280. name: 'Reject share',
  281. displayName: t('files_sharing', 'Reject share'),
  282. mime: 'all',
  283. permissions: OC.PERMISSION_ALL,
  284. iconClass: 'icon-close',
  285. type: OCA.Files.FileActions.TYPE_INLINE,
  286. actionHandler(fileName, context) {
  287. const shareId = context.$file.data('shareId')
  288. $.ajax({
  289. url: OC.linkToOCS('apps/files_sharing/api/v1/shares', 2) + shareId,
  290. type: 'DELETE',
  291. }).success(function(result) {
  292. context.fileList.remove(context.fileInfoModel.attributes.name)
  293. }).fail(function() {
  294. OC.Notification.showTemporary(t('files_sharing', 'Something happened. Unable to reject the share.'))
  295. })
  296. },
  297. })
  298. return fileActions
  299. },
  300. _onActionsUpdated(ev) {
  301. _.each([this._inFileList, this._outFileList, this._linkFileList], function(list) {
  302. if (!list) {
  303. return
  304. }
  305. if (ev.action) {
  306. list.fileActions.registerAction(ev.action)
  307. } else if (ev.defaultAction) {
  308. list.fileActions.setDefault(
  309. ev.defaultAction.mime,
  310. ev.defaultAction.name
  311. )
  312. }
  313. })
  314. },
  315. _extendFileList(fileList) {
  316. // remove size column from summary
  317. fileList.fileSummary.$el.find('.filesize').remove()
  318. },
  319. }
  320. window.addEventListener('DOMContentLoaded', function() {
  321. $('#app-content-sharingin').on('show', function(e) {
  322. OCA.Sharing.App.initSharingIn($(e.target))
  323. })
  324. $('#app-content-sharingin').on('hide', function() {
  325. OCA.Sharing.App.removeSharingIn()
  326. })
  327. $('#app-content-sharingout').on('show', function(e) {
  328. OCA.Sharing.App.initSharingOut($(e.target))
  329. })
  330. $('#app-content-sharingout').on('hide', function() {
  331. OCA.Sharing.App.removeSharingOut()
  332. })
  333. $('#app-content-sharinglinks').on('show', function(e) {
  334. OCA.Sharing.App.initSharingLinks($(e.target))
  335. })
  336. $('#app-content-sharinglinks').on('hide', function() {
  337. OCA.Sharing.App.removeSharingLinks()
  338. })
  339. $('#app-content-deletedshares').on('show', function(e) {
  340. OCA.Sharing.App.initSharingDeleted($(e.target))
  341. })
  342. $('#app-content-deletedshares').on('hide', function() {
  343. OCA.Sharing.App.removeSharingDeleted()
  344. })
  345. $('#app-content-pendingshares').on('show', function(e) {
  346. OCA.Sharing.App.initSharingPening($(e.target))
  347. })
  348. $('#app-content-pendingshares').on('hide', function() {
  349. OCA.Sharing.App.removeSharingPending()
  350. })
  351. $('#app-content-shareoverview').on('show', function(e) {
  352. OCA.Sharing.App.initShareingOverview($(e.target))
  353. })
  354. $('#app-content-shareoverview').on('hide', function() {
  355. OCA.Sharing.App.removeSharingOverview()
  356. })
  357. })