settings.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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. (function(){
  11. // TODO: move to a separate file
  12. var MOUNT_OPTIONS_DROPDOWN_TEMPLATE =
  13. '<div class="popovermenu open">'+
  14. // FIXME: options are hard-coded for now
  15. ' <ul>'+
  16. ' <li class="optionRow">'+
  17. ' <span class="menuitem">'+
  18. ' <input id="mountOptionsEncrypt" class="checkbox" name="encrypt" type="checkbox" value="true" checked="checked"/>'+
  19. ' <label for="mountOptionsEncrypt">{{t "files_external" "Enable encryption"}}</label>'+
  20. ' </span>'+
  21. ' </li>'+
  22. ' <li class="optionRow">'+
  23. ' <span class="menuitem">'+
  24. ' <input id="mountOptionsPreviews" class="checkbox" name="previews" type="checkbox" value="true" checked="checked"/>'+
  25. ' <label for="mountOptionsPreviews">{{t "files_external" "Enable previews"}}</label>'+
  26. ' </span>'+
  27. ' </li>'+
  28. ' <li class="optionRow">'+
  29. ' <span class="menuitem">'+
  30. ' <input id="mountOptionsSharing" class="checkbox" name="enable_sharing" type="checkbox" value="true"/>'+
  31. ' <label for="mountOptionsSharing">{{t "files_external" "Enable sharing"}}</label>'+
  32. ' </span>'+
  33. ' </li>'+
  34. ' <li class="optionRow">'+
  35. ' <span class="menuitem icon-search">'+
  36. ' <label for="mountOptionsFilesystemCheck">{{t "files_external" "Check for changes"}}</label>'+
  37. ' <select id="mountOptionsFilesystemCheck" name="filesystem_check_changes" data-type="int">'+
  38. ' <option value="0">{{t "files_external" "Never"}}</option>'+
  39. ' <option value="1" selected="selected">{{t "files_external" "Once every direct access"}}</option>'+
  40. ' </select>'+
  41. ' </span>'+
  42. ' </li>'+
  43. ' <li class="optionRow">'+
  44. ' <span class="menuitem">'+
  45. ' <input id="mountOptionsEncoding" class="checkbox" name="encoding_compatibility" type="checkbox" value="true"/>'+
  46. ' <label for="mountOptionsEncoding">{{mountOptionsEncodingLabel}}</label>'+
  47. ' </span>'+
  48. ' </li>'+
  49. ' <li class="optionRow">' +
  50. ' <span class="menuitem">' +
  51. ' <input id="mountOptionsReadOnly" class="checkbox" name="readonly" type="checkbox" value="true"/>' +
  52. ' <label for="mountOptionsReadOnly">{{t "files_external" "Read only"}}</label>' +
  53. ' </span>' +
  54. ' </li>' +
  55. ' </ul>'+
  56. '</div>';
  57. /**
  58. * Returns the selection of applicable users in the given configuration row
  59. *
  60. * @param $row configuration row
  61. * @return array array of user names
  62. */
  63. function getSelection($row) {
  64. var values = $row.find('.applicableUsers').select2('val');
  65. if (!values || values.length === 0) {
  66. values = [];
  67. }
  68. return values;
  69. }
  70. function highlightBorder($element, highlight) {
  71. $element.toggleClass('warning-input', highlight);
  72. return highlight;
  73. }
  74. function isInputValid($input) {
  75. var optional = $input.hasClass('optional');
  76. switch ($input.attr('type')) {
  77. case 'text':
  78. case 'password':
  79. if ($input.val() === '' && !optional) {
  80. return false;
  81. }
  82. break;
  83. }
  84. return true;
  85. }
  86. function highlightInput($input) {
  87. switch ($input.attr('type')) {
  88. case 'text':
  89. case 'password':
  90. return highlightBorder($input, !isInputValid($input));
  91. }
  92. }
  93. /**
  94. * Initialize select2 plugin on the given elements
  95. *
  96. * @param {Array<Object>} array of jQuery elements
  97. * @param {int} userListLimit page size for result list
  98. */
  99. function addSelect2 ($elements, userListLimit) {
  100. if (!$elements.length) {
  101. return;
  102. }
  103. $elements.select2({
  104. placeholder: t('files_external', 'All users. Type to select user or group.'),
  105. allowClear: true,
  106. multiple: true,
  107. toggleSelect: true,
  108. dropdownCssClass: 'files-external-select2',
  109. //minimumInputLength: 1,
  110. ajax: {
  111. url: OC.generateUrl('apps/files_external/applicable'),
  112. dataType: 'json',
  113. quietMillis: 100,
  114. data: function (term, page) { // page is the one-based page number tracked by Select2
  115. return {
  116. pattern: term, //search term
  117. limit: userListLimit, // page size
  118. offset: userListLimit*(page-1) // page number starts with 0
  119. };
  120. },
  121. results: function (data) {
  122. if (data.status === 'success') {
  123. var results = [];
  124. var userCount = 0; // users is an object
  125. // add groups
  126. $.each(data.groups, function(i, group) {
  127. results.push({name:group+'(group)', displayname:group, type:'group' });
  128. });
  129. // add users
  130. $.each(data.users, function(id, user) {
  131. userCount++;
  132. results.push({name:id, displayname:user, type:'user' });
  133. });
  134. var more = (userCount >= userListLimit) || (data.groups.length >= userListLimit);
  135. return {results: results, more: more};
  136. } else {
  137. //FIXME add error handling
  138. }
  139. }
  140. },
  141. initSelection: function(element, callback) {
  142. var users = {};
  143. users['users'] = [];
  144. var toSplit = element.val().split(",");
  145. for (var i = 0; i < toSplit.length; i++) {
  146. users['users'].push(toSplit[i]);
  147. }
  148. $.ajax(OC.generateUrl('displaynames'), {
  149. type: 'POST',
  150. contentType: 'application/json',
  151. data: JSON.stringify(users),
  152. dataType: 'json'
  153. }).done(function(data) {
  154. var results = [];
  155. if (data.status === 'success') {
  156. $.each(data.users, function(user, displayname) {
  157. if (displayname !== false) {
  158. results.push({name:user, displayname:displayname, type:'user'});
  159. }
  160. });
  161. callback(results);
  162. } else {
  163. //FIXME add error handling
  164. }
  165. });
  166. },
  167. id: function(element) {
  168. return element.name;
  169. },
  170. formatResult: function (element) {
  171. var $result = $('<span><div class="avatardiv"/><span>'+escapeHTML(element.displayname)+'</span></span>');
  172. var $div = $result.find('.avatardiv')
  173. .attr('data-type', element.type)
  174. .attr('data-name', element.name)
  175. .attr('data-displayname', element.displayname);
  176. if (element.type === 'group') {
  177. var url = OC.imagePath('core','places/contacts-dark'); // TODO better group icon
  178. $div.html('<img width="32" height="32" src="'+url+'">');
  179. }
  180. return $result.get(0).outerHTML;
  181. },
  182. formatSelection: function (element) {
  183. if (element.type === 'group') {
  184. return '<span title="'+escapeHTML(element.name)+'" class="group">'+escapeHTML(element.displayname+' '+t('files_external', '(group)'))+'</span>';
  185. } else {
  186. return '<span title="'+escapeHTML(element.name)+'" class="user">'+escapeHTML(element.displayname)+'</span>';
  187. }
  188. },
  189. escapeMarkup: function (m) { return m; } // we escape the markup in formatResult and formatSelection
  190. }).on('select2-loaded', function() {
  191. $.each($('.avatardiv'), function(i, div) {
  192. var $div = $(div);
  193. if ($div.data('type') === 'user') {
  194. $div.avatar($div.data('name'),32);
  195. }
  196. });
  197. });
  198. }
  199. /**
  200. * @class OCA.External.Settings.StorageConfig
  201. *
  202. * @classdesc External storage config
  203. */
  204. var StorageConfig = function(id) {
  205. this.id = id;
  206. this.backendOptions = {};
  207. };
  208. // Keep this in sync with \OC_Mount_Config::STATUS_*
  209. StorageConfig.Status = {
  210. IN_PROGRESS: -1,
  211. SUCCESS: 0,
  212. ERROR: 1,
  213. INDETERMINATE: 2
  214. };
  215. StorageConfig.Visibility = {
  216. NONE: 0,
  217. PERSONAL: 1,
  218. ADMIN: 2,
  219. DEFAULT: 3
  220. };
  221. /**
  222. * @memberof OCA.External.Settings
  223. */
  224. StorageConfig.prototype = {
  225. _url: null,
  226. /**
  227. * Storage id
  228. *
  229. * @type int
  230. */
  231. id: null,
  232. /**
  233. * Mount point
  234. *
  235. * @type string
  236. */
  237. mountPoint: '',
  238. /**
  239. * Backend
  240. *
  241. * @type string
  242. */
  243. backend: null,
  244. /**
  245. * Authentication mechanism
  246. *
  247. * @type string
  248. */
  249. authMechanism: null,
  250. /**
  251. * Backend-specific configuration
  252. *
  253. * @type Object.<string,object>
  254. */
  255. backendOptions: null,
  256. /**
  257. * Mount-specific options
  258. *
  259. * @type Object.<string,object>
  260. */
  261. mountOptions: null,
  262. /**
  263. * Creates or saves the storage.
  264. *
  265. * @param {Function} [options.success] success callback, receives result as argument
  266. * @param {Function} [options.error] error callback
  267. */
  268. save: function(options) {
  269. var self = this;
  270. var url = OC.generateUrl(this._url);
  271. var method = 'POST';
  272. if (_.isNumber(this.id)) {
  273. method = 'PUT';
  274. url = OC.generateUrl(this._url + '/{id}', {id: this.id});
  275. }
  276. $.ajax({
  277. type: method,
  278. url: url,
  279. contentType: 'application/json',
  280. data: JSON.stringify(this.getData()),
  281. success: function(result) {
  282. self.id = result.id;
  283. if (_.isFunction(options.success)) {
  284. options.success(result);
  285. }
  286. },
  287. error: options.error
  288. });
  289. },
  290. /**
  291. * Returns the data from this object
  292. *
  293. * @return {Array} JSON array of the data
  294. */
  295. getData: function() {
  296. var data = {
  297. mountPoint: this.mountPoint,
  298. backend: this.backend,
  299. authMechanism: this.authMechanism,
  300. backendOptions: this.backendOptions,
  301. testOnly: true
  302. };
  303. if (this.id) {
  304. data.id = this.id;
  305. }
  306. if (this.mountOptions) {
  307. data.mountOptions = this.mountOptions;
  308. }
  309. return data;
  310. },
  311. /**
  312. * Recheck the storage
  313. *
  314. * @param {Function} [options.success] success callback, receives result as argument
  315. * @param {Function} [options.error] error callback
  316. */
  317. recheck: function(options) {
  318. if (!_.isNumber(this.id)) {
  319. if (_.isFunction(options.error)) {
  320. options.error();
  321. }
  322. return;
  323. }
  324. $.ajax({
  325. type: 'GET',
  326. url: OC.generateUrl(this._url + '/{id}', {id: this.id}),
  327. data: {'testOnly': true},
  328. success: options.success,
  329. error: options.error
  330. });
  331. },
  332. /**
  333. * Deletes the storage
  334. *
  335. * @param {Function} [options.success] success callback
  336. * @param {Function} [options.error] error callback
  337. */
  338. destroy: function(options) {
  339. if (!_.isNumber(this.id)) {
  340. // the storage hasn't even been created => success
  341. if (_.isFunction(options.success)) {
  342. options.success();
  343. }
  344. return;
  345. }
  346. $.ajax({
  347. type: 'DELETE',
  348. url: OC.generateUrl(this._url + '/{id}', {id: this.id}),
  349. success: options.success,
  350. error: options.error
  351. });
  352. },
  353. /**
  354. * Validate this model
  355. *
  356. * @return {boolean} false if errors exist, true otherwise
  357. */
  358. validate: function() {
  359. if (this.mountPoint === '') {
  360. return false;
  361. }
  362. if (!this.backend) {
  363. return false;
  364. }
  365. if (this.errors) {
  366. return false;
  367. }
  368. return true;
  369. }
  370. };
  371. /**
  372. * @class OCA.External.Settings.GlobalStorageConfig
  373. * @augments OCA.External.Settings.StorageConfig
  374. *
  375. * @classdesc Global external storage config
  376. */
  377. var GlobalStorageConfig = function(id) {
  378. this.id = id;
  379. this.applicableUsers = [];
  380. this.applicableGroups = [];
  381. };
  382. /**
  383. * @memberOf OCA.External.Settings
  384. */
  385. GlobalStorageConfig.prototype = _.extend({}, StorageConfig.prototype,
  386. /** @lends OCA.External.Settings.GlobalStorageConfig.prototype */ {
  387. _url: 'apps/files_external/globalstorages',
  388. /**
  389. * Applicable users
  390. *
  391. * @type Array.<string>
  392. */
  393. applicableUsers: null,
  394. /**
  395. * Applicable groups
  396. *
  397. * @type Array.<string>
  398. */
  399. applicableGroups: null,
  400. /**
  401. * Storage priority
  402. *
  403. * @type int
  404. */
  405. priority: null,
  406. /**
  407. * Returns the data from this object
  408. *
  409. * @return {Array} JSON array of the data
  410. */
  411. getData: function() {
  412. var data = StorageConfig.prototype.getData.apply(this, arguments);
  413. return _.extend(data, {
  414. applicableUsers: this.applicableUsers,
  415. applicableGroups: this.applicableGroups,
  416. priority: this.priority,
  417. });
  418. }
  419. });
  420. /**
  421. * @class OCA.External.Settings.UserStorageConfig
  422. * @augments OCA.External.Settings.StorageConfig
  423. *
  424. * @classdesc User external storage config
  425. */
  426. var UserStorageConfig = function(id) {
  427. this.id = id;
  428. };
  429. UserStorageConfig.prototype = _.extend({}, StorageConfig.prototype,
  430. /** @lends OCA.External.Settings.UserStorageConfig.prototype */ {
  431. _url: 'apps/files_external/userstorages'
  432. });
  433. /**
  434. * @class OCA.External.Settings.UserGlobalStorageConfig
  435. * @augments OCA.External.Settings.StorageConfig
  436. *
  437. * @classdesc User external storage config
  438. */
  439. var UserGlobalStorageConfig = function (id) {
  440. this.id = id;
  441. };
  442. UserGlobalStorageConfig.prototype = _.extend({}, StorageConfig.prototype,
  443. /** @lends OCA.External.Settings.UserStorageConfig.prototype */ {
  444. _url: 'apps/files_external/userglobalstorages'
  445. });
  446. /**
  447. * @class OCA.External.Settings.MountOptionsDropdown
  448. *
  449. * @classdesc Dropdown for mount options
  450. *
  451. * @param {Object} $container container DOM object
  452. */
  453. var MountOptionsDropdown = function() {
  454. };
  455. /**
  456. * @memberof OCA.External.Settings
  457. */
  458. MountOptionsDropdown.prototype = {
  459. /**
  460. * Dropdown element
  461. *
  462. * @var Object
  463. */
  464. $el: null,
  465. /**
  466. * Show dropdown
  467. *
  468. * @param {Object} $container container
  469. * @param {Object} mountOptions mount options
  470. * @param {Array} visibleOptions enabled mount options
  471. */
  472. show: function($container, mountOptions, visibleOptions) {
  473. if (MountOptionsDropdown._last) {
  474. MountOptionsDropdown._last.hide();
  475. }
  476. var template = MountOptionsDropdown._template;
  477. if (!template) {
  478. template = Handlebars.compile(MOUNT_OPTIONS_DROPDOWN_TEMPLATE);
  479. MountOptionsDropdown._template = template;
  480. }
  481. var $el = $(template({
  482. mountOptionsEncodingLabel: t('files_external', 'Compatibility with Mac NFD encoding (slow)')
  483. }));
  484. this.$el = $el;
  485. this.setOptions(mountOptions, visibleOptions);
  486. this.$el.appendTo($container);
  487. MountOptionsDropdown._last = this;
  488. this.$el.trigger('show');
  489. },
  490. hide: function() {
  491. if (this.$el) {
  492. this.$el.trigger('hide');
  493. this.$el.remove();
  494. this.$el = null;
  495. MountOptionsDropdown._last = null;
  496. }
  497. },
  498. /**
  499. * Returns the mount options from the dropdown controls
  500. *
  501. * @return {Object} options mount options
  502. */
  503. getOptions: function() {
  504. var options = {};
  505. this.$el.find('input, select').each(function() {
  506. var $this = $(this);
  507. var key = $this.attr('name');
  508. var value = null;
  509. if ($this.attr('type') === 'checkbox') {
  510. value = $this.prop('checked');
  511. } else {
  512. value = $this.val();
  513. }
  514. if ($this.attr('data-type') === 'int') {
  515. value = parseInt(value, 10);
  516. }
  517. options[key] = value;
  518. });
  519. return options;
  520. },
  521. /**
  522. * Sets the mount options to the dropdown controls
  523. *
  524. * @param {Object} options mount options
  525. * @param {Array} visibleOptions enabled mount options
  526. */
  527. setOptions: function(options, visibleOptions) {
  528. var $el = this.$el;
  529. _.each(options, function(value, key) {
  530. var $optionEl = $el.find('input, select').filterAttr('name', key);
  531. if ($optionEl.attr('type') === 'checkbox') {
  532. if (_.isString(value)) {
  533. value = (value === 'true');
  534. }
  535. $optionEl.prop('checked', !!value);
  536. } else {
  537. $optionEl.val(value);
  538. }
  539. });
  540. $el.find('.optionRow').each(function(i, row){
  541. var $row = $(row);
  542. var optionId = $row.find('input, select').attr('name');
  543. if (visibleOptions.indexOf(optionId) === -1) {
  544. $row.hide();
  545. } else {
  546. $row.show();
  547. }
  548. });
  549. }
  550. };
  551. /**
  552. * @class OCA.External.Settings.MountConfigListView
  553. *
  554. * @classdesc Mount configuration list view
  555. *
  556. * @param {Object} $el DOM object containing the list
  557. * @param {Object} [options]
  558. * @param {int} [options.userListLimit] page size in applicable users dropdown
  559. */
  560. var MountConfigListView = function($el, options) {
  561. this.initialize($el, options);
  562. };
  563. MountConfigListView.ParameterFlags = {
  564. OPTIONAL: 1,
  565. USER_PROVIDED: 2
  566. };
  567. MountConfigListView.ParameterTypes = {
  568. TEXT: 0,
  569. BOOLEAN: 1,
  570. PASSWORD: 2,
  571. HIDDEN: 3
  572. };
  573. /**
  574. * @memberOf OCA.External.Settings
  575. */
  576. MountConfigListView.prototype = _.extend({
  577. /**
  578. * jQuery element containing the config list
  579. *
  580. * @type Object
  581. */
  582. $el: null,
  583. /**
  584. * Storage config class
  585. *
  586. * @type Class
  587. */
  588. _storageConfigClass: null,
  589. /**
  590. * Flag whether the list is about user storage configs (true)
  591. * or global storage configs (false)
  592. *
  593. * @type bool
  594. */
  595. _isPersonal: false,
  596. /**
  597. * Page size in applicable users dropdown
  598. *
  599. * @type int
  600. */
  601. _userListLimit: 30,
  602. /**
  603. * List of supported backends
  604. *
  605. * @type Object.<string,Object>
  606. */
  607. _allBackends: null,
  608. /**
  609. * List of all supported authentication mechanisms
  610. *
  611. * @type Object.<string,Object>
  612. */
  613. _allAuthMechanisms: null,
  614. _encryptionEnabled: false,
  615. /**
  616. * @param {Object} $el DOM object containing the list
  617. * @param {Object} [options]
  618. * @param {int} [options.userListLimit] page size in applicable users dropdown
  619. */
  620. initialize: function($el, options) {
  621. var self = this;
  622. this.$el = $el;
  623. this._isPersonal = ($el.data('admin') !== true);
  624. if (this._isPersonal) {
  625. this._storageConfigClass = OCA.External.Settings.UserStorageConfig;
  626. } else {
  627. this._storageConfigClass = OCA.External.Settings.GlobalStorageConfig;
  628. }
  629. if (options && !_.isUndefined(options.userListLimit)) {
  630. this._userListLimit = options.userListLimit;
  631. }
  632. this._encryptionEnabled = options.encryptionEnabled;
  633. // read the backend config that was carefully crammed
  634. // into the data-configurations attribute of the select
  635. this._allBackends = this.$el.find('.selectBackend').data('configurations');
  636. this._allAuthMechanisms = this.$el.find('#addMountPoint .authentication').data('mechanisms');
  637. this._initEvents();
  638. },
  639. /**
  640. * Custom JS event handlers
  641. * Trigger callback for all existing configurations
  642. */
  643. whenSelectBackend: function(callback) {
  644. this.$el.find('tbody tr:not(#addMountPoint)').each(function(i, tr) {
  645. var backend = $(tr).find('.backend').data('identifier');
  646. callback($(tr), backend);
  647. });
  648. this.on('selectBackend', callback);
  649. },
  650. whenSelectAuthMechanism: function(callback) {
  651. var self = this;
  652. this.$el.find('tbody tr:not(#addMountPoint)').each(function(i, tr) {
  653. var authMechanism = $(tr).find('.selectAuthMechanism').val();
  654. callback($(tr), authMechanism, self._allAuthMechanisms[authMechanism]['scheme']);
  655. });
  656. this.on('selectAuthMechanism', callback);
  657. },
  658. /**
  659. * Initialize DOM event handlers
  660. */
  661. _initEvents: function() {
  662. var self = this;
  663. var onChangeHandler = _.bind(this._onChange, this);
  664. //this.$el.on('input', 'td input', onChangeHandler);
  665. this.$el.on('keyup', 'td input', onChangeHandler);
  666. this.$el.on('paste', 'td input', onChangeHandler);
  667. this.$el.on('change', 'td input:checkbox', onChangeHandler);
  668. this.$el.on('change', '.applicable', onChangeHandler);
  669. this.$el.on('click', '.status>span', function() {
  670. self.recheckStorageConfig($(this).closest('tr'));
  671. });
  672. this.$el.on('click', 'td.remove>.icon-delete', function() {
  673. self.deleteStorageConfig($(this).closest('tr'));
  674. });
  675. this.$el.on('click', 'td.save>.icon-checkmark', function () {
  676. self.saveStorageConfig($(this).closest('tr'));
  677. });
  678. this.$el.on('click', 'td.mountOptionsToggle>.icon-settings-dark', function() {
  679. self._showMountOptionsDropdown($(this).closest('tr'));
  680. });
  681. this.$el.on('change', '.selectBackend', _.bind(this._onSelectBackend, this));
  682. this.$el.on('change', '.selectAuthMechanism', _.bind(this._onSelectAuthMechanism, this));
  683. },
  684. _onChange: function(event) {
  685. var self = this;
  686. var $target = $(event.target);
  687. if ($target.closest('.dropdown').length) {
  688. // ignore dropdown events
  689. return;
  690. }
  691. highlightInput($target);
  692. var $tr = $target.closest('tr');
  693. this.updateStatus($tr, null);
  694. },
  695. _onSelectBackend: function(event) {
  696. var $target = $(event.target);
  697. var $tr = $target.closest('tr');
  698. var storageConfig = new this._storageConfigClass();
  699. storageConfig.mountPoint = $tr.find('.mountPoint input').val();
  700. storageConfig.backend = $target.val();
  701. $tr.find('.mountPoint input').val('');
  702. var onCompletion = jQuery.Deferred();
  703. $tr = this.newStorage(storageConfig, onCompletion);
  704. onCompletion.resolve();
  705. $tr.find('td.configuration').children().not('[type=hidden]').first().focus();
  706. this.saveStorageConfig($tr);
  707. },
  708. _onSelectAuthMechanism: function(event) {
  709. var $target = $(event.target);
  710. var $tr = $target.closest('tr');
  711. var authMechanism = $target.val();
  712. var onCompletion = jQuery.Deferred();
  713. this.configureAuthMechanism($tr, authMechanism, onCompletion);
  714. onCompletion.resolve();
  715. this.saveStorageConfig($tr);
  716. },
  717. /**
  718. * Configure the storage config with a new authentication mechanism
  719. *
  720. * @param {jQuery} $tr config row
  721. * @param {string} authMechanism
  722. * @param {jQuery.Deferred} onCompletion
  723. */
  724. configureAuthMechanism: function($tr, authMechanism, onCompletion) {
  725. var authMechanismConfiguration = this._allAuthMechanisms[authMechanism];
  726. var $td = $tr.find('td.configuration');
  727. $td.find('.auth-param').remove();
  728. $.each(authMechanismConfiguration['configuration'], _.partial(
  729. this.writeParameterInput, $td, _, _, ['auth-param']
  730. ).bind(this));
  731. this.trigger('selectAuthMechanism',
  732. $tr, authMechanism, authMechanismConfiguration['scheme'], onCompletion
  733. );
  734. },
  735. /**
  736. * Create a config row for a new storage
  737. *
  738. * @param {StorageConfig} storageConfig storage config to pull values from
  739. * @param {jQuery.Deferred} onCompletion
  740. * @return {jQuery} created row
  741. */
  742. newStorage: function(storageConfig, onCompletion) {
  743. var mountPoint = storageConfig.mountPoint;
  744. var backend = this._allBackends[storageConfig.backend];
  745. if (!backend) {
  746. backend = {
  747. name: 'Unknown: ' + storageConfig.backend,
  748. invalid: true
  749. };
  750. }
  751. // FIXME: Replace with a proper Handlebar template
  752. var $tr = this.$el.find('tr#addMountPoint');
  753. this.$el.find('tbody').append($tr.clone());
  754. $tr.data('storageConfig', storageConfig);
  755. $tr.show();
  756. $tr.find('td.mountOptionsToggle, td.save, td.remove').removeClass('hidden');
  757. $tr.find('td').last().removeAttr('style');
  758. $tr.removeAttr('id');
  759. $tr.find('select#selectBackend');
  760. addSelect2($tr.find('.applicableUsers'), this._userListLimit);
  761. if (storageConfig.id) {
  762. $tr.data('id', storageConfig.id);
  763. }
  764. $tr.find('.backend').text(backend.name);
  765. if (mountPoint === '') {
  766. mountPoint = this._suggestMountPoint(backend.name);
  767. }
  768. $tr.find('.mountPoint input').val(mountPoint);
  769. $tr.addClass(backend.identifier);
  770. $tr.find('.backend').data('identifier', backend.identifier);
  771. if (backend.invalid) {
  772. $tr.find('[name=mountPoint]').prop('disabled', true);
  773. $tr.find('.applicable,.mountOptionsToggle').empty();
  774. this.updateStatus($tr, false, 'Unknown backend: ' + backend.name);
  775. return $tr;
  776. }
  777. var selectAuthMechanism = $('<select class="selectAuthMechanism"></select>');
  778. var neededVisibility = (this._isPersonal) ? StorageConfig.Visibility.PERSONAL : StorageConfig.Visibility.ADMIN;
  779. $.each(this._allAuthMechanisms, function(authIdentifier, authMechanism) {
  780. if (backend.authSchemes[authMechanism.scheme] && (authMechanism.visibility & neededVisibility)) {
  781. selectAuthMechanism.append(
  782. $('<option value="'+authMechanism.identifier+'" data-scheme="'+authMechanism.scheme+'">'+authMechanism.name+'</option>')
  783. );
  784. }
  785. });
  786. if (storageConfig.authMechanism) {
  787. selectAuthMechanism.val(storageConfig.authMechanism);
  788. } else {
  789. storageConfig.authMechanism = selectAuthMechanism.val();
  790. }
  791. $tr.find('td.authentication').append(selectAuthMechanism);
  792. var $td = $tr.find('td.configuration');
  793. $.each(backend.configuration, _.partial(this.writeParameterInput, $td).bind(this));
  794. this.trigger('selectBackend', $tr, backend.identifier, onCompletion);
  795. this.configureAuthMechanism($tr, storageConfig.authMechanism, onCompletion);
  796. if (storageConfig.backendOptions) {
  797. $td.find('input, select').each(function() {
  798. var input = $(this);
  799. var val = storageConfig.backendOptions[input.data('parameter')];
  800. if (val !== undefined) {
  801. if(input.is('input:checkbox')) {
  802. input.prop('checked', val);
  803. }
  804. input.val(storageConfig.backendOptions[input.data('parameter')]);
  805. highlightInput(input);
  806. }
  807. });
  808. }
  809. var applicable = [];
  810. if (storageConfig.applicableUsers) {
  811. applicable = applicable.concat(storageConfig.applicableUsers);
  812. }
  813. if (storageConfig.applicableGroups) {
  814. applicable = applicable.concat(
  815. _.map(storageConfig.applicableGroups, function(group) {
  816. return group+'(group)';
  817. })
  818. );
  819. }
  820. $tr.find('.applicableUsers').val(applicable).trigger('change');
  821. var priorityEl = $('<input type="hidden" class="priority" value="' + backend.priority + '" />');
  822. $tr.append(priorityEl);
  823. if (storageConfig.mountOptions) {
  824. $tr.find('input.mountOptions').val(JSON.stringify(storageConfig.mountOptions));
  825. } else {
  826. // FIXME default backend mount options
  827. $tr.find('input.mountOptions').val(JSON.stringify({
  828. 'encrypt': true,
  829. 'previews': true,
  830. 'enable_sharing': false,
  831. 'filesystem_check_changes': 1,
  832. 'encoding_compatibility': false,
  833. 'readonly': false,
  834. }));
  835. }
  836. return $tr;
  837. },
  838. /**
  839. * Load storages into config rows
  840. */
  841. loadStorages: function() {
  842. var self = this;
  843. if (this._isPersonal) {
  844. // load userglobal storages
  845. $.ajax({
  846. type: 'GET',
  847. url: OC.generateUrl('apps/files_external/userglobalstorages'),
  848. data: {'testOnly' : true},
  849. contentType: 'application/json',
  850. success: function(result) {
  851. var onCompletion = jQuery.Deferred();
  852. $.each(result, function(i, storageParams) {
  853. var storageConfig;
  854. var isUserGlobal = storageParams.type === 'system' && self._isPersonal;
  855. storageParams.mountPoint = storageParams.mountPoint.substr(1); // trim leading slash
  856. if (isUserGlobal) {
  857. storageConfig = new UserGlobalStorageConfig();
  858. } else {
  859. storageConfig = new self._storageConfigClass();
  860. }
  861. _.extend(storageConfig, storageParams);
  862. var $tr = self.newStorage(storageConfig, onCompletion);
  863. // userglobal storages must be at the top of the list
  864. $tr.detach();
  865. self.$el.prepend($tr);
  866. var $authentication = $tr.find('.authentication');
  867. $authentication.text($authentication.find('select option:selected').text());
  868. // disable any other inputs
  869. $tr.find('.mountOptionsToggle, .remove').empty();
  870. $tr.find('input:not(.user_provided), select:not(.user_provided)').attr('disabled', 'disabled');
  871. if (isUserGlobal) {
  872. $tr.find('.configuration').find(':not(.user_provided)').remove();
  873. } else {
  874. // userglobal storages do not expose configuration data
  875. $tr.find('.configuration').text(t('files_external', 'Admin defined'));
  876. }
  877. });
  878. var mainForm = $('#files_external');
  879. if (result.length === 0 && mainForm.attr('data-can-create') === 'false') {
  880. mainForm.hide();
  881. $('a[href="#external-storage"]').parent().hide();
  882. $('#emptycontent').show();
  883. }
  884. onCompletion.resolve();
  885. }
  886. });
  887. }
  888. var url = this._storageConfigClass.prototype._url;
  889. $.ajax({
  890. type: 'GET',
  891. url: OC.generateUrl(url),
  892. contentType: 'application/json',
  893. success: function(result) {
  894. var onCompletion = jQuery.Deferred();
  895. $.each(result, function(i, storageParams) {
  896. storageParams.mountPoint = (storageParams.mountPoint === '/')? '/' : storageParams.mountPoint.substr(1); // trim leading slash
  897. var storageConfig = new self._storageConfigClass();
  898. _.extend(storageConfig, storageParams);
  899. var $tr = self.newStorage(storageConfig, onCompletion);
  900. self.recheckStorageConfig($tr);
  901. });
  902. onCompletion.resolve();
  903. }
  904. });
  905. },
  906. /**
  907. * @param {jQuery} $td
  908. * @param {string} parameter
  909. * @param {string} placeholder
  910. * @param {Array} classes
  911. * @return {jQuery} newly created input
  912. */
  913. writeParameterInput: function($td, parameter, placeholder, classes) {
  914. var hasFlag = function(flag) {
  915. return (placeholder.flags & flag) === flag;
  916. };
  917. classes = $.isArray(classes) ? classes : [];
  918. classes.push('added');
  919. if (hasFlag(MountConfigListView.ParameterFlags.OPTIONAL)) {
  920. classes.push('optional');
  921. }
  922. if (hasFlag(MountConfigListView.ParameterFlags.USER_PROVIDED)) {
  923. if (this._isPersonal) {
  924. classes.push('user_provided');
  925. } else {
  926. return;
  927. }
  928. }
  929. var newElement;
  930. var trimmedPlaceholder = placeholder.value;
  931. if (placeholder.type === MountConfigListView.ParameterTypes.PASSWORD) {
  932. newElement = $('<input type="password" class="'+classes.join(' ')+'" data-parameter="'+parameter+'" placeholder="'+ trimmedPlaceholder+'" />');
  933. } else if (placeholder.type === MountConfigListView.ParameterTypes.BOOLEAN) {
  934. var checkboxId = _.uniqueId('checkbox_');
  935. newElement = $('<div><label><input type="checkbox" id="'+checkboxId+'" class="'+classes.join(' ')+'" data-parameter="'+parameter+'" />'+ trimmedPlaceholder+'</label></div>');
  936. } else if (placeholder.type === MountConfigListView.ParameterTypes.HIDDEN) {
  937. newElement = $('<input type="hidden" class="'+classes.join(' ')+'" data-parameter="'+parameter+'" />');
  938. } else {
  939. newElement = $('<input type="text" class="'+classes.join(' ')+'" data-parameter="'+parameter+'" placeholder="'+ trimmedPlaceholder+'" />');
  940. }
  941. highlightInput(newElement);
  942. $td.append(newElement);
  943. return newElement;
  944. },
  945. /**
  946. * Gets the storage model from the given row
  947. *
  948. * @param $tr row element
  949. * @return {OCA.External.StorageConfig} storage model instance
  950. */
  951. getStorageConfig: function($tr) {
  952. var storageId = $tr.data('id');
  953. if (!storageId) {
  954. // new entry
  955. storageId = null;
  956. }
  957. var storage = $tr.data('storageConfig');
  958. if (!storage) {
  959. storage = new this._storageConfigClass(storageId);
  960. }
  961. storage.errors = null;
  962. storage.mountPoint = $tr.find('.mountPoint input').val();
  963. storage.backend = $tr.find('.backend').data('identifier');
  964. storage.authMechanism = $tr.find('.selectAuthMechanism').val();
  965. var classOptions = {};
  966. var configuration = $tr.find('.configuration input');
  967. var missingOptions = [];
  968. $.each(configuration, function(index, input) {
  969. var $input = $(input);
  970. var parameter = $input.data('parameter');
  971. if ($input.attr('type') === 'button') {
  972. return;
  973. }
  974. if (!isInputValid($input) && !$input.hasClass('optional')) {
  975. missingOptions.push(parameter);
  976. return;
  977. }
  978. if ($(input).is(':checkbox')) {
  979. if ($(input).is(':checked')) {
  980. classOptions[parameter] = true;
  981. } else {
  982. classOptions[parameter] = false;
  983. }
  984. } else {
  985. classOptions[parameter] = $(input).val();
  986. }
  987. });
  988. storage.backendOptions = classOptions;
  989. if (missingOptions.length) {
  990. storage.errors = {
  991. backendOptions: missingOptions
  992. };
  993. }
  994. // gather selected users and groups
  995. if (!this._isPersonal) {
  996. var groups = [];
  997. var users = [];
  998. var multiselect = getSelection($tr);
  999. $.each(multiselect, function(index, value) {
  1000. var pos = (value.indexOf)?value.indexOf('(group)'): -1;
  1001. if (pos !== -1) {
  1002. groups.push(value.substr(0, pos));
  1003. } else {
  1004. users.push(value);
  1005. }
  1006. });
  1007. // FIXME: this should be done in the multiselect change event instead
  1008. $tr.find('.applicable')
  1009. .data('applicable-groups', groups)
  1010. .data('applicable-users', users);
  1011. storage.applicableUsers = users;
  1012. storage.applicableGroups = groups;
  1013. storage.priority = parseInt($tr.find('input.priority').val() || '100', 10);
  1014. }
  1015. var mountOptions = $tr.find('input.mountOptions').val();
  1016. if (mountOptions) {
  1017. storage.mountOptions = JSON.parse(mountOptions);
  1018. }
  1019. return storage;
  1020. },
  1021. /**
  1022. * Deletes the storage from the given tr
  1023. *
  1024. * @param $tr storage row
  1025. * @param Function callback callback to call after save
  1026. */
  1027. deleteStorageConfig: function($tr) {
  1028. var self = this;
  1029. var configId = $tr.data('id');
  1030. if (!_.isNumber(configId)) {
  1031. // deleting unsaved storage
  1032. $tr.remove();
  1033. return;
  1034. }
  1035. var storage = new this._storageConfigClass(configId);
  1036. OC.dialogs.confirm(t('files_external', 'Are you sure you want to delete this external storage?', {
  1037. storage: this.mountPoint
  1038. }), t('files_external', 'Delete storage?'), function(confirm) {
  1039. if (confirm) {
  1040. self.updateStatus($tr, StorageConfig.Status.IN_PROGRESS);
  1041. storage.destroy({
  1042. success: function () {
  1043. $tr.remove();
  1044. },
  1045. error: function () {
  1046. self.updateStatus($tr, StorageConfig.Status.ERROR);
  1047. }
  1048. });
  1049. }
  1050. });
  1051. },
  1052. /**
  1053. * Saves the storage from the given tr
  1054. *
  1055. * @param $tr storage row
  1056. * @param Function callback callback to call after save
  1057. * @param concurrentTimer only update if the timer matches this
  1058. */
  1059. saveStorageConfig:function($tr, callback, concurrentTimer) {
  1060. var self = this;
  1061. var storage = this.getStorageConfig($tr);
  1062. if (!storage || !storage.validate()) {
  1063. return false;
  1064. }
  1065. this.updateStatus($tr, StorageConfig.Status.IN_PROGRESS);
  1066. storage.save({
  1067. success: function(result) {
  1068. if (concurrentTimer === undefined
  1069. || $tr.data('save-timer') === concurrentTimer
  1070. ) {
  1071. self.updateStatus($tr, result.status);
  1072. $tr.data('id', result.id);
  1073. if (_.isFunction(callback)) {
  1074. callback(storage);
  1075. }
  1076. }
  1077. },
  1078. error: function() {
  1079. if (concurrentTimer === undefined
  1080. || $tr.data('save-timer') === concurrentTimer
  1081. ) {
  1082. self.updateStatus($tr, StorageConfig.Status.ERROR);
  1083. }
  1084. }
  1085. });
  1086. },
  1087. /**
  1088. * Recheck storage availability
  1089. *
  1090. * @param {jQuery} $tr storage row
  1091. * @return {boolean} success
  1092. */
  1093. recheckStorageConfig: function($tr) {
  1094. var self = this;
  1095. var storage = this.getStorageConfig($tr);
  1096. if (!storage.validate()) {
  1097. return false;
  1098. }
  1099. this.updateStatus($tr, StorageConfig.Status.IN_PROGRESS);
  1100. storage.recheck({
  1101. success: function(result) {
  1102. self.updateStatus($tr, result.status, result.statusMessage);
  1103. },
  1104. error: function() {
  1105. self.updateStatus($tr, StorageConfig.Status.ERROR);
  1106. }
  1107. });
  1108. },
  1109. /**
  1110. * Update status display
  1111. *
  1112. * @param {jQuery} $tr
  1113. * @param {int} status
  1114. * @param {string} message
  1115. */
  1116. updateStatus: function($tr, status, message) {
  1117. var $statusSpan = $tr.find('.status span');
  1118. switch (status) {
  1119. case null:
  1120. // remove status
  1121. break;
  1122. case StorageConfig.Status.IN_PROGRESS:
  1123. $statusSpan.attr('class', 'icon-loading-small');
  1124. break;
  1125. case StorageConfig.Status.SUCCESS:
  1126. $statusSpan.attr('class', 'success icon-checkmark-white');
  1127. break;
  1128. case StorageConfig.Status.INDETERMINATE:
  1129. $statusSpan.attr('class', 'indeterminate icon-info-white');
  1130. break;
  1131. default:
  1132. $statusSpan.attr('class', 'error icon-error-white');
  1133. }
  1134. if (typeof message === 'string') {
  1135. $statusSpan.attr('title', message);
  1136. $statusSpan.tooltip();
  1137. } else {
  1138. $statusSpan.tooltip('destroy');
  1139. }
  1140. },
  1141. /**
  1142. * Suggest mount point name that doesn't conflict with the existing names in the list
  1143. *
  1144. * @param {string} defaultMountPoint default name
  1145. */
  1146. _suggestMountPoint: function(defaultMountPoint) {
  1147. var $el = this.$el;
  1148. var pos = defaultMountPoint.indexOf('/');
  1149. if (pos !== -1) {
  1150. defaultMountPoint = defaultMountPoint.substring(0, pos);
  1151. }
  1152. defaultMountPoint = defaultMountPoint.replace(/\s+/g, '');
  1153. var i = 1;
  1154. var append = '';
  1155. var match = true;
  1156. while (match && i < 20) {
  1157. match = false;
  1158. $el.find('tbody td.mountPoint input').each(function(index, mountPoint) {
  1159. if ($(mountPoint).val() === defaultMountPoint+append) {
  1160. match = true;
  1161. return false;
  1162. }
  1163. });
  1164. if (match) {
  1165. append = i;
  1166. i++;
  1167. } else {
  1168. break;
  1169. }
  1170. }
  1171. return defaultMountPoint + append;
  1172. },
  1173. /**
  1174. * Toggles the mount options dropdown
  1175. *
  1176. * @param {Object} $tr configuration row
  1177. */
  1178. _showMountOptionsDropdown: function($tr) {
  1179. var self = this;
  1180. var storage = this.getStorageConfig($tr);
  1181. var $toggle = $tr.find('.mountOptionsToggle');
  1182. var dropDown = new MountOptionsDropdown();
  1183. var visibleOptions = [
  1184. 'previews',
  1185. 'filesystem_check_changes',
  1186. 'enable_sharing',
  1187. 'encoding_compatibility',
  1188. 'readonly'
  1189. ];
  1190. if (this._encryptionEnabled) {
  1191. visibleOptions.push('encrypt');
  1192. }
  1193. dropDown.show($toggle, storage.mountOptions || [], visibleOptions);
  1194. $('body').on('mouseup.mountOptionsDropdown', function(event) {
  1195. var $target = $(event.target);
  1196. if ($target.closest('.popovermenu').length) {
  1197. return;
  1198. }
  1199. dropDown.hide();
  1200. });
  1201. dropDown.$el.on('hide', function() {
  1202. var mountOptions = dropDown.getOptions();
  1203. $('body').off('mouseup.mountOptionsDropdown');
  1204. $tr.find('input.mountOptions').val(JSON.stringify(mountOptions));
  1205. self.saveStorageConfig($tr);
  1206. });
  1207. }
  1208. }, OC.Backbone.Events);
  1209. $(document).ready(function() {
  1210. var enabled = $('#files_external').attr('data-encryption-enabled');
  1211. var encryptionEnabled = (enabled ==='true')? true: false;
  1212. var mountConfigListView = new MountConfigListView($('#externalStorage'), {
  1213. encryptionEnabled: encryptionEnabled
  1214. });
  1215. mountConfigListView.loadStorages();
  1216. // TODO: move this into its own View class
  1217. var $allowUserMounting = $('#allowUserMounting');
  1218. $allowUserMounting.bind('change', function() {
  1219. OC.msg.startSaving('#userMountingMsg');
  1220. if (this.checked) {
  1221. OCP.AppConfig.setValue('files_external', 'allow_user_mounting', 'yes');
  1222. $('input[name="allowUserMountingBackends\\[\\]"]').prop('checked', true);
  1223. $('#userMountingBackends').removeClass('hidden');
  1224. $('input[name="allowUserMountingBackends\\[\\]"]').eq(0).trigger('change');
  1225. } else {
  1226. OCP.AppConfig.setValue('files_external', 'allow_user_mounting', 'no');
  1227. $('#userMountingBackends').addClass('hidden');
  1228. }
  1229. OC.msg.finishedSaving('#userMountingMsg', {status: 'success', data: {message: t('files_external', 'Saved')}});
  1230. });
  1231. $('input[name="allowUserMountingBackends\\[\\]"]').bind('change', function() {
  1232. OC.msg.startSaving('#userMountingMsg');
  1233. var userMountingBackends = $('input[name="allowUserMountingBackends\\[\\]"]:checked').map(function(){
  1234. return $(this).val();
  1235. }).get();
  1236. var deprecatedBackends = $('input[name="allowUserMountingBackends\\[\\]"][data-deprecate-to]').map(function(){
  1237. if ($.inArray($(this).data('deprecate-to'), userMountingBackends) !== -1) {
  1238. return $(this).val();
  1239. }
  1240. return null;
  1241. }).get();
  1242. userMountingBackends = userMountingBackends.concat(deprecatedBackends);
  1243. OCP.AppConfig.setValue('files_external', 'user_mounting_backends', userMountingBackends.join());
  1244. OC.msg.finishedSaving('#userMountingMsg', {status: 'success', data: {message: t('files_external', 'Saved')}});
  1245. // disable allowUserMounting
  1246. if(userMountingBackends.length === 0) {
  1247. $allowUserMounting.prop('checked', false);
  1248. $allowUserMounting.trigger('change');
  1249. }
  1250. });
  1251. $('#global_credentials').on('submit', function() {
  1252. var $form = $(this);
  1253. var uid = $form.find('[name=uid]').val();
  1254. var user = $form.find('[name=username]').val();
  1255. var password = $form.find('[name=password]').val();
  1256. var $submit = $form.find('[type=submit]');
  1257. $submit.val(t('files_external', 'Saving...'));
  1258. $.ajax({
  1259. type: 'POST',
  1260. contentType: 'application/json',
  1261. data: JSON.stringify({
  1262. uid: uid,
  1263. user: user,
  1264. password: password
  1265. }),
  1266. url: OC.generateUrl('apps/files_external/globalcredentials'),
  1267. dataType: 'json',
  1268. success: function() {
  1269. $submit.val(t('files_external', 'Saved'));
  1270. setTimeout(function(){
  1271. $submit.val(t('files_external', 'Save'));
  1272. }, 2500);
  1273. }
  1274. });
  1275. return false;
  1276. });
  1277. // global instance
  1278. OCA.External.Settings.mountConfig = mountConfigListView;
  1279. /**
  1280. * Legacy
  1281. *
  1282. * @namespace
  1283. * @deprecated use OCA.External.Settings.mountConfig instead
  1284. */
  1285. OC.MountConfig = {
  1286. saveStorage: _.bind(mountConfigListView.saveStorageConfig, mountConfigListView)
  1287. };
  1288. });
  1289. // export
  1290. OCA.External = OCA.External || {};
  1291. /**
  1292. * @namespace
  1293. */
  1294. OCA.External.Settings = OCA.External.Settings || {};
  1295. OCA.External.Settings.GlobalStorageConfig = GlobalStorageConfig;
  1296. OCA.External.Settings.UserStorageConfig = UserStorageConfig;
  1297. OCA.External.Settings.MountConfigListView = MountConfigListView;
  1298. })();