settings.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. OC.MountConfig={
  2. saveStorage:function(tr) {
  3. var mountPoint = $(tr).find('.mountPoint input').val();
  4. if (mountPoint == '') {
  5. return false;
  6. }
  7. var statusSpan = $(tr).find('.status span');
  8. var backendClass = $(tr).find('.backend').data('class');
  9. var configuration = $(tr).find('.configuration input');
  10. var addMountPoint = true;
  11. if (configuration.length < 1) {
  12. return false;
  13. }
  14. var classOptions = {};
  15. $.each(configuration, function(index, input) {
  16. if ($(input).val() == '' && !$(input).hasClass('optional')) {
  17. addMountPoint = false;
  18. return false;
  19. }
  20. if ($(input).is(':checkbox')) {
  21. if ($(input).is(':checked')) {
  22. classOptions[$(input).data('parameter')] = true;
  23. } else {
  24. classOptions[$(input).data('parameter')] = false;
  25. }
  26. } else {
  27. classOptions[$(input).data('parameter')] = $(input).val();
  28. }
  29. });
  30. if ($('#externalStorage').data('admin') === true) {
  31. var multiselect = $(tr).find('.chzn-select').val();
  32. if (multiselect == null) {
  33. return false;
  34. }
  35. }
  36. if (addMountPoint) {
  37. var status = false;
  38. if ($('#externalStorage').data('admin') === true) {
  39. var isPersonal = false;
  40. var oldGroups = $(tr).find('.applicable').data('applicable-groups');
  41. var oldUsers = $(tr).find('.applicable').data('applicable-users');
  42. var groups = [];
  43. var users = [];
  44. $.each(multiselect, function(index, value) {
  45. var pos = value.indexOf('(group)');
  46. if (pos != -1) {
  47. var mountType = 'group';
  48. var applicable = value.substr(0, pos);
  49. if ($.inArray(applicable, oldGroups) != -1) {
  50. oldGroups.splice($.inArray(applicable, oldGroups), 1);
  51. }
  52. groups.push(applicable);
  53. } else {
  54. var mountType = 'user';
  55. var applicable = value;
  56. if ($.inArray(applicable, oldUsers) != -1) {
  57. oldUsers.splice($.inArray(applicable, oldUsers), 1);
  58. }
  59. users.push(applicable);
  60. }
  61. $.ajax({type: 'POST',
  62. url: OC.filePath('files_external', 'ajax', 'addMountPoint.php'),
  63. data: {
  64. mountPoint: mountPoint,
  65. 'class': backendClass,
  66. classOptions: classOptions,
  67. mountType: mountType,
  68. applicable: applicable,
  69. isPersonal: isPersonal
  70. },
  71. async: false,
  72. success: function(result) {
  73. statusSpan.removeClass();
  74. if (result && result.status == 'success' && result.data.message) {
  75. status = true;
  76. statusSpan.addClass('success');
  77. } else {
  78. statusSpan.addClass('error');
  79. }
  80. }
  81. });
  82. });
  83. $(tr).find('.applicable').data('applicable-groups', groups);
  84. $(tr).find('.applicable').data('applicable-users', users);
  85. var mountType = 'group';
  86. $.each(oldGroups, function(index, applicable) {
  87. $.ajax({type: 'POST',
  88. url: OC.filePath('files_external', 'ajax', 'removeMountPoint.php'),
  89. data: {
  90. mountPoint: mountPoint,
  91. class: backendClass,
  92. classOptions: classOptions,
  93. mountType: mountType,
  94. applicable: applicable,
  95. isPersonal: isPersonal
  96. },
  97. async: false
  98. });
  99. });
  100. var mountType = 'user';
  101. $.each(oldUsers, function(index, applicable) {
  102. $.ajax({type: 'POST',
  103. url: OC.filePath('files_external', 'ajax', 'removeMountPoint.php'),
  104. data: {
  105. mountPoint: mountPoint,
  106. class: backendClass,
  107. classOptions: classOptions,
  108. mountType: mountType,
  109. applicable: applicable,
  110. isPersonal: isPersonal
  111. },
  112. async: false
  113. });
  114. });
  115. } else {
  116. var isPersonal = true;
  117. var mountType = 'user';
  118. var applicable = OC.currentUser;
  119. $.ajax({type: 'POST',
  120. url: OC.filePath('files_external', 'ajax', 'addMountPoint.php'),
  121. data: {
  122. mountPoint: mountPoint,
  123. 'class': backendClass,
  124. classOptions: classOptions,
  125. mountType: mountType,
  126. applicable: applicable,
  127. isPersonal: isPersonal
  128. },
  129. async: false,
  130. success: function(result) {
  131. statusSpan.removeClass();
  132. if (result && result.status == 'success' && result.data.message) {
  133. status = true;
  134. statusSpan.addClass('success');
  135. } else {
  136. statusSpan.addClass('error');
  137. }
  138. }
  139. });
  140. }
  141. return status;
  142. }
  143. }
  144. };
  145. $(document).ready(function() {
  146. $('.chzn-select').chosen();
  147. $('#externalStorage').on('change', '#selectBackend', function() {
  148. var tr = $(this).parent().parent();
  149. $('#externalStorage tbody').append($(tr).clone());
  150. $('#externalStorage tbody tr').last().find('.mountPoint input').val('');
  151. var selected = $(this).find('option:selected').text();
  152. var backendClass = $(this).val();
  153. $(this).parent().text(selected);
  154. if ($(tr).find('.mountPoint input').val() == '') {
  155. $(tr).find('.mountPoint input').val(suggestMountPoint(selected));
  156. }
  157. $(tr).addClass(backendClass);
  158. $(tr).find('.status').append('<span class="waiting"></span>');
  159. $(tr).find('.backend').data('class', backendClass);
  160. var configurations = $(this).data('configurations');
  161. var td = $(tr).find('td.configuration');
  162. $.each(configurations, function(backend, parameters) {
  163. if (backend == backendClass) {
  164. $.each(parameters['configuration'], function(parameter, placeholder) {
  165. if (placeholder.indexOf('*') != -1) {
  166. td.append('<input type="password" data-parameter="'+parameter+'" placeholder="'+placeholder.substring(1)+'" />');
  167. } else if (placeholder.indexOf('!') != -1) {
  168. td.append('<label><input type="checkbox" data-parameter="'+parameter+'" />'+placeholder.substring(1)+'</label>');
  169. } else if (placeholder.indexOf('&') != -1) {
  170. td.append('<input type="text" class="optional" data-parameter="'+parameter+'" placeholder="'+placeholder.substring(1)+'" />');
  171. } else if (placeholder.indexOf('#') != -1) {
  172. td.append('<input type="hidden" data-parameter="'+parameter+'" />');
  173. } else {
  174. td.append('<input type="text" data-parameter="'+parameter+'" placeholder="'+placeholder+'" />');
  175. }
  176. });
  177. if (parameters['custom'] && $('#externalStorage tbody tr.'+backendClass.replace(/\\/g, '\\\\')).length == 1) {
  178. OC.addScript('files_external', parameters['custom']);
  179. }
  180. return false;
  181. }
  182. });
  183. // Reset chosen
  184. var chosen = $(tr).find('.applicable select');
  185. chosen.parent().find('div').remove();
  186. chosen.removeAttr('id').removeClass('chzn-done').css({display:'inline-block'});
  187. chosen.chosen();
  188. $(tr).find('td').last().attr('class', 'remove');
  189. $(tr).find('td').last().removeAttr('style');
  190. $(tr).removeAttr('id');
  191. $(this).remove();
  192. });
  193. function suggestMountPoint(defaultMountPoint) {
  194. var pos = defaultMountPoint.indexOf('/');
  195. if (pos !== -1) {
  196. defaultMountPoint = defaultMountPoint.substring(0, pos);
  197. }
  198. defaultMountPoint = defaultMountPoint.replace(/\s+/g, '');
  199. var i = 1;
  200. var append = '';
  201. var match = true;
  202. while (match && i < 20) {
  203. match = false;
  204. $('#externalStorage tbody td.mountPoint input').each(function(index, mountPoint) {
  205. if ($(mountPoint).val() == defaultMountPoint+append) {
  206. match = true;
  207. return false;
  208. }
  209. });
  210. if (match) {
  211. append = i;
  212. i++;
  213. } else {
  214. break;
  215. }
  216. }
  217. return defaultMountPoint+append;
  218. }
  219. $('#externalStorage').on('paste', 'td', function() {
  220. var tr = $(this).parent();
  221. setTimeout(function() {
  222. OC.MountConfig.saveStorage(tr);
  223. }, 20);
  224. });
  225. var timer;
  226. $('#externalStorage').on('keyup', 'td input', function() {
  227. clearTimeout(timer);
  228. var tr = $(this).parent().parent();
  229. if ($(this).val) {
  230. timer = setTimeout(function() {
  231. OC.MountConfig.saveStorage(tr);
  232. }, 2000);
  233. }
  234. });
  235. $('#externalStorage').on('change', 'td input:checkbox', function() {
  236. OC.MountConfig.saveStorage($(this).parent().parent().parent());
  237. });
  238. $('#externalStorage').on('change', '.applicable .chzn-select', function() {
  239. OC.MountConfig.saveStorage($(this).parent().parent());
  240. });
  241. $('#externalStorage').on('click', 'td.remove>img', function() {
  242. var tr = $(this).parent().parent();
  243. var mountPoint = $(tr).find('.mountPoint input').val();
  244. if ( ! mountPoint) {
  245. var row=this.parentNode.parentNode;
  246. $.post(OC.filePath('files_external', 'ajax', 'removeRootCertificate.php'), { cert: row.id });
  247. $(tr).remove();
  248. return true;
  249. }
  250. if ($('#externalStorage').data('admin') === true) {
  251. var isPersonal = false;
  252. var multiselect = $(tr).find('.chzn-select').val();
  253. if (multiselect != null) {
  254. $.each(multiselect, function(index, value) {
  255. var pos = value.indexOf('(group)');
  256. if (pos != -1) {
  257. var mountType = 'group';
  258. var applicable = value.substr(0, pos);
  259. } else {
  260. var mountType = 'user';
  261. var applicable = value;
  262. }
  263. $.post(OC.filePath('files_external', 'ajax', 'removeMountPoint.php'), { mountPoint: mountPoint, mountType: mountType, applicable: applicable, isPersonal: isPersonal });
  264. });
  265. }
  266. } else {
  267. var mountType = 'user';
  268. var applicable = OC.currentUser;
  269. var isPersonal = true;
  270. $.post(OC.filePath('files_external', 'ajax', 'removeMountPoint.php'), { mountPoint: mountPoint, mountType: mountType, applicable: applicable, isPersonal: isPersonal });
  271. }
  272. $(tr).remove();
  273. });
  274. $('#allowUserMounting').bind('change', function() {
  275. if (this.checked) {
  276. OC.AppConfig.setValue('files_external', 'allow_user_mounting', 'yes');
  277. } else {
  278. OC.AppConfig.setValue('files_external', 'allow_user_mounting', 'no');
  279. }
  280. });
  281. });