Commit | Line | Data |
---|---|---|
6b8bd380 TO |
1 | (function(angular, $, _) { |
2 | ||
3 | // Controller for the "Save Message Template" dialog | |
4 | // Scope members: | |
5 | // - [input] "model": Object | |
6 | // - "selected_id": int | |
7 | // - "tpl": Object | |
8 | // - "msg_subject": string | |
9 | // - "msg_text": string | |
10 | // - "msg_html": string | |
11 | angular.module('crmMailing').controller('SaveMsgTemplateDialogCtrl', function SaveMsgTemplateDialogCtrl($scope, crmMsgTemplates, dialogService) { | |
12 | var ts = $scope.ts = CRM.ts(null); | |
13 | $scope.saveOpt = {mode: '', newTitle: ''}; | |
14 | $scope.selected = null; | |
15 | ||
16 | $scope.save = function save() { | |
17 | var tpl = _.extend({}, $scope.model.tpl); | |
18 | switch ($scope.saveOpt.mode) { | |
19 | case 'add': | |
20 | tpl.msg_title = $scope.saveOpt.newTitle; | |
21 | break; | |
22 | case 'update': | |
23 | tpl.id = $scope.selected.id; | |
24 | tpl.msg_title = $scope.selected.msg_title; | |
25 | break; | |
26 | default: | |
27 | throw 'SaveMsgTemplateDialogCtrl: Unrecognized mode: ' + $scope.saveOpt.mode; | |
28 | } | |
29 | return crmMsgTemplates.save(tpl) | |
30 | .then(function (item) { | |
31 | CRM.status(ts('Saved')); | |
32 | return item; | |
33 | }); | |
34 | }; | |
35 | ||
36 | function scopeApply(f) { | |
37 | return function () { | |
38 | var args = arguments; | |
39 | $scope.$apply(function () { | |
40 | f.apply(args); | |
41 | }); | |
42 | }; | |
43 | } | |
44 | ||
45 | function init() { | |
46 | crmMsgTemplates.get($scope.model.selected_id).then( | |
47 | function (tpl) { | |
48 | $scope.saveOpt.mode = 'update'; | |
49 | $scope.selected = tpl; | |
50 | }, | |
51 | function () { | |
52 | $scope.saveOpt.mode = 'add'; | |
53 | $scope.selected = null; | |
54 | } | |
55 | ); | |
56 | // When using dialogService with a button bar, the major button actions | |
57 | // need to be registered with the dialog widget (and not embedded in | |
58 | // the body of the dialog). | |
59 | var buttons = [ | |
60 | { | |
61 | text: ts('Save'), | |
77ec5a8d | 62 | icons: {primary: 'fa-check'}, |
6b8bd380 TO |
63 | click: function () { |
64 | $scope.save().then(function (item) { | |
65 | dialogService.close('saveTemplateDialog', item); | |
66 | }); | |
67 | } | |
68 | }, | |
69 | { | |
70 | text: ts('Cancel'), | |
77ec5a8d | 71 | icons: {primary: 'fa-times'}, |
6b8bd380 TO |
72 | click: function () { |
73 | dialogService.cancel('saveTemplateDialog'); | |
74 | } | |
75 | } | |
76 | ]; | |
77 | dialogService.setButtons('saveTemplateDialog', buttons); | |
78 | } | |
79 | ||
80 | setTimeout(scopeApply(init), 0); | |
81 | }); | |
82 | ||
83 | })(angular, CRM.$, CRM._); |