admin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /**
  2. * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
  3. *
  4. * @license GNU AGPL version 3 or any later version
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. (function() {
  21. Handlebars.registerHelper('selectItem', function(currentValue, itemValue) {
  22. if (currentValue === itemValue) {
  23. return 'selected="selected"';
  24. }
  25. return "";
  26. });
  27. Handlebars.registerHelper('getOperators', function(classname) {
  28. var check = OCA.WorkflowEngine.getCheckByClass(classname);
  29. if (!_.isUndefined(check)) {
  30. return check['operators'];
  31. }
  32. return [];
  33. });
  34. OCA.WorkflowEngine = _.extend(OCA.WorkflowEngine || {}, {
  35. availablePlugins: [],
  36. availableChecks: [],
  37. getCheckByClass: function(className) {
  38. var length = OCA.WorkflowEngine.availableChecks.length;
  39. for (var i = 0; i < length; i++) {
  40. if (OCA.WorkflowEngine.availableChecks[i]['class'] === className) {
  41. return OCA.WorkflowEngine.availableChecks[i];
  42. }
  43. }
  44. return undefined;
  45. }
  46. });
  47. /**
  48. * 888b d888 888 888
  49. * 8888b d8888 888 888
  50. * 88888b.d88888 888 888
  51. * 888Y88888P888 .d88b. .d88888 .d88b. 888 .d8888b
  52. * 888 Y888P 888 d88""88b d88" 888 d8P Y8b 888 88K
  53. * 888 Y8P 888 888 888 888 888 88888888 888 "Y8888b.
  54. * 888 " 888 Y88..88P Y88b 888 Y8b. 888 X88
  55. * 888 888 "Y88P" "Y88888 "Y8888 888 88888P'
  56. */
  57. /**
  58. * @class OCA.WorkflowEngine.Operation
  59. */
  60. OCA.WorkflowEngine.Operation =
  61. OC.Backbone.Model.extend({
  62. defaults: {
  63. 'class': 'OCA\\WorkflowEngine\\Operation',
  64. 'name': '',
  65. 'checks': [],
  66. 'operation': ''
  67. }
  68. });
  69. /**
  70. * .d8888b. 888 888 888 d8b
  71. * d88P Y88b 888 888 888 Y8P
  72. * 888 888 888 888 888
  73. * 888 .d88b. 888 888 .d88b. .d8888b 888888 888 .d88b. 88888b. .d8888b
  74. * 888 d88""88b 888 888 d8P Y8b d88P" 888 888 d88""88b 888 "88b 88K
  75. * 888 888 888 888 888 888 88888888 888 888 888 888 888 888 888 "Y8888b.
  76. * Y88b d88P Y88..88P 888 888 Y8b. Y88b. Y88b. 888 Y88..88P 888 888 X88
  77. * "Y8888P" "Y88P" 888 888 "Y8888 "Y8888P "Y888 888 "Y88P" 888 888 88888P'
  78. */
  79. /**
  80. * @class OCA.WorkflowEngine.OperationsCollection
  81. *
  82. * collection for all configurated operations
  83. */
  84. OCA.WorkflowEngine.OperationsCollection =
  85. OC.Backbone.Collection.extend({
  86. model: OCA.WorkflowEngine.Operation,
  87. url: OC.generateUrl('apps/workflowengine/operations')
  88. });
  89. /**
  90. * 888 888 d8b
  91. * 888 888 Y8P
  92. * 888 888
  93. * Y88b d88P 888 .d88b. 888 888 888 .d8888b
  94. * Y88b d88P 888 d8P Y8b 888 888 888 88K
  95. * Y88o88P 888 88888888 888 888 888 "Y8888b.
  96. * Y888P 888 Y8b. Y88b 888 d88P X88
  97. * Y8P 888 "Y8888 "Y8888888P" 88888P'
  98. */
  99. /**
  100. * @class OCA.WorkflowEngine.OperationView
  101. *
  102. * this creates the view for a single operation
  103. */
  104. OCA.WorkflowEngine.OperationView =
  105. OC.Backbone.View.extend({
  106. templateId: '#operation-template',
  107. events: {
  108. 'change .check-class': 'checkChanged',
  109. 'change .check-operator': 'checkChanged',
  110. 'change .check-value': 'checkChanged',
  111. 'change .operation-name': 'operationChanged',
  112. 'change .operation-operation': 'operationChanged',
  113. 'click .button-reset': 'reset',
  114. 'click .button-save': 'save',
  115. 'click .button-add': 'add',
  116. 'click .button-delete': 'delete',
  117. 'click .button-delete-check': 'deleteCheck'
  118. },
  119. originalModel: null,
  120. hasChanged: false,
  121. message: '',
  122. errorMessage: '',
  123. saving: false,
  124. groups: [],
  125. template: function(vars) {
  126. return OCA.WorkflowEngine.Templates['operation'](_.extend(
  127. {
  128. shortRuleDescTXT: t('workflowengine', 'Short rule description'),
  129. addRuleTXT: t('workflowengine', 'Add rule'),
  130. resetTXT: t('workflowengine', 'Reset'),
  131. saveTXT: t('workflowengine', 'Save'),
  132. savingTXT: t('workflowengine', 'Saving…')
  133. },
  134. vars
  135. ));
  136. },
  137. initialize: function() {
  138. // this creates a new copy of the object to definitely have a new reference and being able to reset the model
  139. this.originalModel = JSON.parse(JSON.stringify(this.model));
  140. this.model.on('change', function() {
  141. console.log('model changed');
  142. this.hasChanged = true;
  143. this.render();
  144. }, this);
  145. if (this.model.get('id') === undefined) {
  146. this.hasChanged = true;
  147. }
  148. var self = this;
  149. $.ajax({
  150. url: OC.linkToOCS('cloud/groups', 2) + 'details',
  151. dataType: 'json',
  152. quietMillis: 100,
  153. }).success(function(data) {
  154. if (data.ocs.data.groups && data.ocs.data.groups.length > 0) {
  155. data.ocs.data.groups.forEach(function(group) {
  156. self.groups.push({ id: group.id, displayname: group.displayname });
  157. });
  158. self.render();
  159. } else {
  160. OC.Notification.error(t('workflowengine', 'Group list is empty'), { type: 'error' });
  161. console.log(data);
  162. }
  163. }).error(function(data) {
  164. OC.Notification.error(t('workflowengine', 'Unable to retrieve the group list'), { type: 'error' });
  165. console.log(data);
  166. });
  167. },
  168. delete: function() {
  169. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  170. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.delete, this));
  171. return;
  172. }
  173. this.model.destroy();
  174. this.remove();
  175. },
  176. reset: function() {
  177. this.hasChanged = false;
  178. // silent is need to not trigger the change event which resets the hasChanged attribute
  179. this.model.set(this.originalModel, { silent: true });
  180. this.render();
  181. },
  182. save: function() {
  183. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  184. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.save, this));
  185. return;
  186. }
  187. var success = function(model, response, options) {
  188. this.saving = false;
  189. this.originalModel = JSON.parse(JSON.stringify(this.model));
  190. this.message = t('workflowengine', 'Saved');
  191. this.errorMessage = '';
  192. this.render();
  193. };
  194. var error = function(model, response, options) {
  195. this.saving = false;
  196. this.hasChanged = true;
  197. this.message = t('workflowengine', 'Saving failed:');
  198. this.errorMessage = response.responseText;
  199. this.render();
  200. };
  201. this.hasChanged = false;
  202. this.saving = true;
  203. this.render();
  204. this.model.save(null, { success: success, error: error, context: this });
  205. },
  206. add: function() {
  207. var checks = _.clone(this.model.get('checks')),
  208. classname = OCA.WorkflowEngine.availableChecks[0]['class'],
  209. operators = OCA.WorkflowEngine.availableChecks[0]['operators'];
  210. checks.push({
  211. 'class': classname,
  212. 'operator': operators[0]['operator'],
  213. 'value': ''
  214. });
  215. this.model.set({ 'checks': checks });
  216. },
  217. checkChanged: function(event) {
  218. var value = event.target.value,
  219. id = $(event.target.parentElement).data('id'),
  220. // this creates a new copy of the object to definitely have a new reference
  221. checks = JSON.parse(JSON.stringify(this.model.get('checks'))),
  222. key = null;
  223. for (var i = 0; i < event.target.classList.length; i++) {
  224. var className = event.target.classList[i];
  225. if (className.substr(0, 'check-'.length) === 'check-') {
  226. key = className.substr('check-'.length);
  227. break;
  228. }
  229. }
  230. if (key === null) {
  231. console.warn('checkChanged triggered but element doesn\'t have any "check-" class');
  232. return;
  233. }
  234. if (!_.has(checks[id], key)) {
  235. console.warn('key "' + key + '" is not available in check', check);
  236. return;
  237. }
  238. checks[id][key] = value;
  239. // if the class is changed most likely also the operators have changed
  240. // with this we set the operator to the first possible operator
  241. if (key === 'class') {
  242. var check = OCA.WorkflowEngine.getCheckByClass(value);
  243. if (!_.isUndefined(check)) {
  244. checks[id]['operator'] = check['operators'][0]['operator'];
  245. checks[id]['value'] = '';
  246. }
  247. }
  248. // model change will trigger render
  249. this.model.set({ 'checks': checks });
  250. },
  251. deleteCheck: function(event) {
  252. console.log(arguments);
  253. var id = $(event.target.parentElement).data('id'),
  254. checks = JSON.parse(JSON.stringify(this.model.get('checks')));
  255. // splice removes 1 element at index `id`
  256. checks.splice(id, 1);
  257. // model change will trigger render
  258. this.model.set({ 'checks': checks });
  259. },
  260. operationChanged: function(event) {
  261. var value = event.target.value,
  262. key = null;
  263. for (var i = 0; i < event.target.classList.length; i++) {
  264. var className = event.target.classList[i];
  265. if (className.substr(0, 'operation-'.length) === 'operation-') {
  266. key = className.substr('operation-'.length);
  267. break;
  268. }
  269. }
  270. if (key === null) {
  271. console.warn('operationChanged triggered but element doesn\'t have any "operation-" class');
  272. return;
  273. }
  274. if (key !== 'name' && key !== 'operation') {
  275. console.warn('key "' + key + '" is no valid attribute');
  276. return;
  277. }
  278. // model change will trigger render
  279. this.model.set(key, value);
  280. },
  281. render: function() {
  282. this.$el.html(this.template({
  283. operation: this.model.toJSON(),
  284. classes: OCA.WorkflowEngine.availableChecks,
  285. hasChanged: this.hasChanged,
  286. message: this.message,
  287. errorMessage: this.errorMessage,
  288. saving: this.saving
  289. }));
  290. var checks = this.model.get('checks');
  291. _.each(this.$el.find('.check'), function(element) {
  292. var $element = $(element),
  293. id = $element.data('id'),
  294. check = checks[id],
  295. valueElement = $element.find('.check-value').first();
  296. var self = this;
  297. _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
  298. if (_.isFunction(plugin.render)) {
  299. plugin.render(valueElement, check, self.groups);
  300. }
  301. });
  302. }, this);
  303. if (this.message !== '') {
  304. // hide success messages after some time
  305. _.delay(function(elements) {
  306. $(elements).css('opacity', 0);
  307. }, 7000, this.$el.find('.msg.success'));
  308. this.message = '';
  309. }
  310. return this.$el;
  311. }
  312. });
  313. /**
  314. * @class OCA.WorkflowEngine.OperationsView
  315. *
  316. * this creates the view for configured operations
  317. */
  318. OCA.WorkflowEngine.OperationsView =
  319. OC.Backbone.View.extend({
  320. templateId: '#operations-template',
  321. collection: null,
  322. $el: null,
  323. events: {
  324. 'click .button-add-operation': 'add'
  325. },
  326. template: function(vars) {
  327. return OCA.WorkflowEngine.Templates['operations'](_.extend(
  328. {
  329. addRuleGroupTXT: t('workflowengine', 'Add rule group')
  330. },
  331. vars
  332. ));
  333. },
  334. initialize: function(classname) {
  335. if (!OCA.WorkflowEngine.availablePlugins.length) {
  336. OCA.WorkflowEngine.availablePlugins = OC.Plugins.getPlugins('OCA.WorkflowEngine.CheckPlugins');
  337. _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
  338. if (_.isFunction(plugin.getCheck)) {
  339. OCA.WorkflowEngine.availableChecks.push(plugin.getCheck(classname));
  340. }
  341. });
  342. }
  343. this.collection.fetch({
  344. data: {
  345. 'class': classname
  346. }
  347. });
  348. this.collection.once('sync', this.render, this);
  349. },
  350. add: function() {
  351. var operation = this.collection.create();
  352. this.renderOperation(operation);
  353. },
  354. renderOperation: function(subView) {
  355. var operationsElement = this.$el.find('.operations');
  356. operationsElement.append(subView.$el);
  357. subView.render();
  358. },
  359. render: function() {
  360. this.$el.html(this.template());
  361. this.collection.each(this.renderOperation, this);
  362. }
  363. });
  364. })();