admin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. initialize: function() {
  141. // this creates a new copy of the object to definitely have a new reference and being able to reset the model
  142. this.originalModel = JSON.parse(JSON.stringify(this.model));
  143. this.model.on('change', function(){
  144. console.log('model changed');
  145. this.hasChanged = true;
  146. this.render();
  147. }, this);
  148. if (this.model.get('id') === undefined) {
  149. this.hasChanged = true;
  150. }
  151. },
  152. delete: function() {
  153. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  154. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.delete, this));
  155. return;
  156. }
  157. this.model.destroy();
  158. this.remove();
  159. },
  160. reset: function() {
  161. this.hasChanged = false;
  162. // silent is need to not trigger the change event which resets the hasChanged attribute
  163. this.model.set(this.originalModel, {silent: true});
  164. this.render();
  165. },
  166. save: function() {
  167. if (OC.PasswordConfirmation.requiresPasswordConfirmation()) {
  168. OC.PasswordConfirmation.requirePasswordConfirmation(_.bind(this.save, this));
  169. return;
  170. }
  171. var success = function(model, response, options) {
  172. this.saving = false;
  173. this.originalModel = JSON.parse(JSON.stringify(this.model));
  174. this.message = t('workflowengine', 'Successfully saved');
  175. this.errorMessage = '';
  176. this.render();
  177. };
  178. var error = function(model, response, options) {
  179. this.saving = false;
  180. this.hasChanged = true;
  181. this.message = t('workflowengine', 'Saving failed:');
  182. this.errorMessage = response.responseText;
  183. this.render();
  184. };
  185. this.hasChanged = false;
  186. this.saving = true;
  187. this.render();
  188. this.model.save(null, {success: success, error: error, context: this});
  189. },
  190. add: function() {
  191. var checks = _.clone(this.model.get('checks')),
  192. classname = OCA.WorkflowEngine.availableChecks[0]['class'],
  193. operators = OCA.WorkflowEngine.availableChecks[0]['operators'];
  194. checks.push({
  195. 'class': classname,
  196. 'operator': operators[0]['operator'],
  197. 'value': ''
  198. });
  199. this.model.set({'checks': checks});
  200. },
  201. checkChanged: function(event) {
  202. var value = event.target.value,
  203. id = $(event.target.parentElement).data('id'),
  204. // this creates a new copy of the object to definitely have a new reference
  205. checks = JSON.parse(JSON.stringify(this.model.get('checks'))),
  206. key = null;
  207. for (var i = 0; i < event.target.classList.length; i++) {
  208. var className = event.target.classList[i];
  209. if (className.substr(0, 'check-'.length) === 'check-') {
  210. key = className.substr('check-'.length);
  211. break;
  212. }
  213. }
  214. if (key === null) {
  215. console.warn('checkChanged triggered but element doesn\'t have any "check-" class');
  216. return;
  217. }
  218. if (!_.has(checks[id], key)) {
  219. console.warn('key "' + key + '" is not available in check', check);
  220. return;
  221. }
  222. checks[id][key] = value;
  223. // if the class is changed most likely also the operators have changed
  224. // with this we set the operator to the first possible operator
  225. if (key === 'class') {
  226. var check = OCA.WorkflowEngine.getCheckByClass(value);
  227. if (!_.isUndefined(check)) {
  228. checks[id]['operator'] = check['operators'][0]['operator'];
  229. }
  230. }
  231. // model change will trigger render
  232. this.model.set({'checks': checks});
  233. },
  234. deleteCheck: function(event) {
  235. console.log(arguments);
  236. var id = $(event.target.parentElement).data('id'),
  237. checks = JSON.parse(JSON.stringify(this.model.get('checks')));
  238. // splice removes 1 element at index `id`
  239. checks.splice(id, 1);
  240. // model change will trigger render
  241. this.model.set({'checks': checks});
  242. },
  243. operationChanged: function(event) {
  244. var value = event.target.value,
  245. key = null;
  246. for (var i = 0; i < event.target.classList.length; i++) {
  247. var className = event.target.classList[i];
  248. if (className.substr(0, 'operation-'.length) === 'operation-') {
  249. key = className.substr('operation-'.length);
  250. break;
  251. }
  252. }
  253. if (key === null) {
  254. console.warn('operationChanged triggered but element doesn\'t have any "operation-" class');
  255. return;
  256. }
  257. if (key !== 'name' && key !== 'operation') {
  258. console.warn('key "' + key + '" is no valid attribute');
  259. return;
  260. }
  261. // model change will trigger render
  262. this.model.set(key, value);
  263. },
  264. render: function() {
  265. this.$el.html(this.template({
  266. operation: this.model.toJSON(),
  267. classes: OCA.WorkflowEngine.availableChecks,
  268. hasChanged: this.hasChanged,
  269. message: this.message,
  270. errorMessage: this.errorMessage,
  271. saving: this.saving
  272. }));
  273. var checks = this.model.get('checks');
  274. _.each(this.$el.find('.check'), function(element){
  275. var $element = $(element),
  276. id = $element.data('id'),
  277. check = checks[id],
  278. valueElement = $element.find('.check-value').first();
  279. _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
  280. if (_.isFunction(plugin.render)) {
  281. plugin.render(valueElement, check);
  282. }
  283. });
  284. }, this);
  285. if (this.message !== '') {
  286. // hide success messages after some time
  287. _.delay(function(elements){
  288. $(elements).css('opacity', 0);
  289. }, 7000, this.$el.find('.msg.success'));
  290. this.message = '';
  291. }
  292. return this.$el;
  293. }
  294. });
  295. /**
  296. * @class OCA.WorkflowEngine.OperationsView
  297. *
  298. * this creates the view for configured operations
  299. */
  300. OCA.WorkflowEngine.OperationsView =
  301. OCA.WorkflowEngine.TemplateView.extend({
  302. templateId: '#operations-template',
  303. collection: null,
  304. $el: null,
  305. events: {
  306. 'click .button-add-operation': 'add'
  307. },
  308. initialize: function(classname) {
  309. if (!OCA.WorkflowEngine.availablePlugins.length) {
  310. OCA.WorkflowEngine.availablePlugins = OC.Plugins.getPlugins('OCA.WorkflowEngine.CheckPlugins');
  311. _.each(OCA.WorkflowEngine.availablePlugins, function(plugin) {
  312. if (_.isFunction(plugin.getCheck)) {
  313. OCA.WorkflowEngine.availableChecks.push(plugin.getCheck(classname));
  314. }
  315. });
  316. }
  317. this.collection.fetch({data: {
  318. 'class': classname
  319. }});
  320. this.collection.once('sync', this.render, this);
  321. },
  322. add: function() {
  323. var operation = this.collection.create();
  324. this.renderOperation(operation);
  325. },
  326. renderOperation: function(subView){
  327. var operationsElement = this.$el.find('.operations');
  328. operationsElement.append(subView.$el);
  329. subView.render();
  330. },
  331. render: function() {
  332. this.$el.html(this.template());
  333. this.collection.each(this.renderOperation, this);
  334. }
  335. });
  336. })();