admin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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.TemplateView
  101. *
  102. * a generic template that handles the Handlebars template compile step
  103. * in a method called "template()"
  104. */
  105. OCA.WorkflowEngine.TemplateView =
  106. OC.Backbone.View.extend({
  107. _template: null,
  108. template: function(vars) {
  109. if (!this._template) {
  110. this._template = Handlebars.compile($(this.templateId).html());
  111. }
  112. return this._template(vars);
  113. }
  114. });
  115. /**
  116. * @class OCA.WorkflowEngine.OperationView
  117. *
  118. * this creates the view for a single operation
  119. */
  120. OCA.WorkflowEngine.OperationView =
  121. OCA.WorkflowEngine.TemplateView.extend({
  122. templateId: '#operation-template',
  123. events: {
  124. 'change .check-class': 'checkChanged',
  125. 'change .check-operator': 'checkChanged',
  126. 'change .check-value': 'checkChanged',
  127. 'change .operation-name': 'operationChanged',
  128. 'change .operation-operation': 'operationChanged',
  129. 'click .button-reset': 'reset',
  130. 'click .button-save': 'save',
  131. 'click .button-add': 'add',
  132. 'click .button-delete': 'delete',
  133. 'click .button-delete-check': 'deleteCheck'
  134. },
  135. originalModel: null,
  136. hasChanged: false,
  137. message: '',
  138. errorMessage: '',
  139. saving: false,
  140. groups: [],
  141. initialize: function() {
  142. // this creates a new copy of the object to definitely have a new reference and being able to reset the model
  143. this.originalModel = JSON.parse(JSON.stringify(this.model));
  144. this.model.on('change', function() {
  145. console.log('model changed');
  146. this.hasChanged = true;
  147. this.render();
  148. }, this);
  149. if (this.model.get('id') === undefined) {
  150. this.hasChanged = true;
  151. }
  152. var self = this;
  153. $.ajax({
  154. url: OC.linkToOCS('cloud/groups', 2) + 'details',
  155. dataType: 'json',
  156. quietMillis: 100,
  157. }).success(function(data) {
  158. if (data.ocs.data.groups && data.ocs.data.groups.length > 0) {
  159. data.ocs.data.groups.forEach(function(group) {
  160. self.groups.push({ id: group.id, displayname: group.displayname });
  161. });
  162. self.render();
  163. } else {
  164. OC.Notification.error(t('workflowengine', 'Group list is empty'), { type: 'error' });
  165. console.log(data);
  166. }
  167. }).error(function(data) {
  168. OC.Notification.error(t('workflowengine', 'Unable to retrieve the group list'), { type: 'error' });
  169. console.log(data);
  170. });
  171. },
  172. delete: function() {
  173. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  174. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.delete, this));
  175. return;
  176. }
  177. this.model.destroy();
  178. this.remove();
  179. },
  180. reset: function() {
  181. this.hasChanged = false;
  182. // silent is need to not trigger the change event which resets the hasChanged attribute
  183. this.model.set(this.originalModel, { silent: true });
  184. this.render();
  185. },
  186. save: function() {
  187. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  188. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.save, this));
  189. return;
  190. }
  191. var success = function(model, response, options) {
  192. this.saving = false;
  193. this.originalModel = JSON.parse(JSON.stringify(this.model));
  194. this.message = t('workflowengine', 'Saved');
  195. this.errorMessage = '';
  196. this.render();
  197. };
  198. var error = function(model, response, options) {
  199. this.saving = false;
  200. this.hasChanged = true;
  201. this.message = t('workflowengine', 'Saving failed:');
  202. this.errorMessage = response.responseText;
  203. this.render();
  204. };
  205. this.hasChanged = false;
  206. this.saving = true;
  207. this.render();
  208. this.model.save(null, { success: success, error: error, context: this });
  209. },
  210. add: function() {
  211. var checks = _.clone(this.model.get('checks')),
  212. classname = OCA.WorkflowEngine.availableChecks[0]['class'],
  213. operators = OCA.WorkflowEngine.availableChecks[0]['operators'];
  214. checks.push({
  215. 'class': classname,
  216. 'operator': operators[0]['operator'],
  217. 'value': ''
  218. });
  219. this.model.set({ 'checks': checks });
  220. },
  221. checkChanged: function(event) {
  222. var value = event.target.value,
  223. id = $(event.target.parentElement).data('id'),
  224. // this creates a new copy of the object to definitely have a new reference
  225. checks = JSON.parse(JSON.stringify(this.model.get('checks'))),
  226. key = null;
  227. for (var i = 0; i < event.target.classList.length; i++) {
  228. var className = event.target.classList[i];
  229. if (className.substr(0, 'check-'.length) === 'check-') {
  230. key = className.substr('check-'.length);
  231. break;
  232. }
  233. }
  234. if (key === null) {
  235. console.warn('checkChanged triggered but element doesn\'t have any "check-" class');
  236. return;
  237. }
  238. if (!_.has(checks[id], key)) {
  239. console.warn('key "' + key + '" is not available in check', check);
  240. return;
  241. }
  242. checks[id][key] = value;
  243. // if the class is changed most likely also the operators have changed
  244. // with this we set the operator to the first possible operator
  245. if (key === 'class') {
  246. var check = OCA.WorkflowEngine.getCheckByClass(value);
  247. if (!_.isUndefined(check)) {
  248. checks[id]['operator'] = check['operators'][0]['operator'];
  249. checks[id]['value'] = '';
  250. }
  251. }
  252. // model change will trigger render
  253. this.model.set({ 'checks': checks });
  254. },
  255. deleteCheck: function(event) {
  256. console.log(arguments);
  257. var id = $(event.target.parentElement).data('id'),
  258. checks = JSON.parse(JSON.stringify(this.model.get('checks')));
  259. // splice removes 1 element at index `id`
  260. checks.splice(id, 1);
  261. // model change will trigger render
  262. this.model.set({ 'checks': checks });
  263. },
  264. operationChanged: function(event) {
  265. var value = event.target.value,
  266. key = null;
  267. for (var i = 0; i < event.target.classList.length; i++) {
  268. var className = event.target.classList[i];
  269. if (className.substr(0, 'operation-'.length) === 'operation-') {
  270. key = className.substr('operation-'.length);
  271. break;
  272. }
  273. }
  274. if (key === null) {
  275. console.warn('operationChanged triggered but element doesn\'t have any "operation-" class');
  276. return;
  277. }
  278. if (key !== 'name' && key !== 'operation') {
  279. console.warn('key "' + key + '" is no valid attribute');
  280. return;
  281. }
  282. // model change will trigger render
  283. this.model.set(key, value);
  284. },
  285. render: function() {
  286. this.$el.html(this.template({
  287. operation: this.model.toJSON(),
  288. classes: OCA.WorkflowEngine.availableChecks,
  289. hasChanged: this.hasChanged,
  290. message: this.message,
  291. errorMessage: this.errorMessage,
  292. saving: this.saving
  293. }));
  294. var checks = this.model.get('checks');
  295. _.each(this.$el.find('.check'), function(element) {
  296. var $element = $(element),
  297. id = $element.data('id'),
  298. check = checks[id],
  299. valueElement = $element.find('.check-value').first();
  300. var self = this;
  301. _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
  302. if (_.isFunction(plugin.render)) {
  303. plugin.render(valueElement, check, self.groups);
  304. }
  305. });
  306. }, this);
  307. if (this.message !== '') {
  308. // hide success messages after some time
  309. _.delay(function(elements) {
  310. $(elements).css('opacity', 0);
  311. }, 7000, this.$el.find('.msg.success'));
  312. this.message = '';
  313. }
  314. return this.$el;
  315. }
  316. });
  317. /**
  318. * @class OCA.WorkflowEngine.OperationsView
  319. *
  320. * this creates the view for configured operations
  321. */
  322. OCA.WorkflowEngine.OperationsView =
  323. OCA.WorkflowEngine.TemplateView.extend({
  324. templateId: '#operations-template',
  325. collection: null,
  326. $el: null,
  327. events: {
  328. 'click .button-add-operation': 'add'
  329. },
  330. initialize: function(classname) {
  331. if (!OCA.WorkflowEngine.availablePlugins.length) {
  332. OCA.WorkflowEngine.availablePlugins = OC.Plugins.getPlugins('OCA.WorkflowEngine.CheckPlugins');
  333. _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
  334. if (_.isFunction(plugin.getCheck)) {
  335. OCA.WorkflowEngine.availableChecks.push(plugin.getCheck(classname));
  336. }
  337. });
  338. }
  339. this.collection.fetch({
  340. data: {
  341. 'class': classname
  342. }
  343. });
  344. this.collection.once('sync', this.render, this);
  345. },
  346. add: function() {
  347. var operation = this.collection.create();
  348. this.renderOperation(operation);
  349. },
  350. renderOperation: function(subView) {
  351. var operationsElement = this.$el.find('.operations');
  352. operationsElement.append(subView.$el);
  353. subView.render();
  354. },
  355. render: function() {
  356. this.$el.html(this.template());
  357. this.collection.each(this.renderOperation, this);
  358. }
  359. });
  360. })();