Commit | Line | Data |
---|---|---|
60ebf0a5 TO |
1 | /// crmUi: Sundry UI helpers |
2 | (function (angular, $, _) { | |
3 | angular.module('crmUtil', []); | |
4 | ||
a8e65974 TO |
5 | angular.module('crmUtil').factory('crmApi', function($q) { |
6 | return function(entity, action, params, message) { | |
7 | // JSON serialization in CRM.api3 is not aware of Angular metadata like $$hash, so use angular.toJson() | |
8 | var deferred = $q.defer(); | |
9 | var p; | |
10 | if (_.isObject(entity)) { | |
f91e4af5 TO |
11 | // eval content is locally generated. |
12 | /*jshint -W061 */ | |
a8e65974 TO |
13 | p = CRM.api3(eval('('+angular.toJson(entity)+')'), message); |
14 | } else { | |
f91e4af5 TO |
15 | // eval content is locally generated. |
16 | /*jshint -W061 */ | |
a8e65974 TO |
17 | p = CRM.api3(entity, action, eval('('+angular.toJson(params)+')'), message); |
18 | } | |
19 | // CRM.api3 returns a promise, but the promise doesn't really represent errors as errors, so we | |
20 | // convert them | |
21 | p.then( | |
22 | function(result) { | |
23 | if (result.is_error) { | |
24 | deferred.reject(result); | |
25 | } else { | |
26 | deferred.resolve(result); | |
27 | } | |
28 | }, | |
29 | function(error) { | |
30 | deferred.reject(error); | |
31 | } | |
32 | ); | |
33 | return deferred.promise; | |
34 | }; | |
35 | }); | |
36 | ||
37 | angular.module('crmUtil').factory('crmLegacy', function() { | |
38 | return CRM; | |
39 | }); | |
40 | ||
226ef186 TO |
41 | // example: scope.$watch('foo', crmLog.wrap(function(newValue, oldValue){ ... })); |
42 | angular.module('crmUtil').factory('crmLog', function(){ | |
43 | var level = 0; | |
44 | var write = console.log; | |
45 | function indent() { | |
46 | var s = '>'; | |
47 | for (var i = 0; i < level; i++) s = s + ' '; | |
48 | return s; | |
49 | } | |
50 | var crmLog = { | |
51 | log: function(msg, vars) { | |
52 | write(indent() + msg, vars); | |
53 | }, | |
54 | wrap: function(label, f) { | |
55 | return function(){ | |
56 | level++; | |
57 | crmLog.log(label + ": start", arguments); | |
58 | var r; | |
59 | try { | |
60 | r = f.apply(this, arguments); | |
61 | } finally { | |
62 | crmLog.log(label + ": end"); | |
63 | level--; | |
64 | } | |
65 | return r; | |
f2bad133 | 66 | }; |
226ef186 | 67 | } |
f286acec | 68 | }; |
226ef186 | 69 | return crmLog; |
f286acec TO |
70 | }); |
71 | ||
a8e65974 TO |
72 | angular.module('crmUtil').factory('crmNavigator', ['$window', function($window) { |
73 | return { | |
74 | redirect: function(path) { | |
75 | $window.location.href = path; | |
76 | } | |
77 | }; | |
78 | }]); | |
79 | ||
8717390f TO |
80 | angular.module('crmUtil').factory('crmNow', function($q){ |
81 | // FIXME: surely there's already some helper which can do this in one line? | |
82 | // @return string "YYYY-MM-DD hh:mm:ss" | |
83 | return function crmNow() { | |
84 | var currentdate = new Date(); | |
85 | var yyyy = currentdate.getFullYear(); | |
86 | var mm = currentdate.getMonth() + 1; | |
87 | mm = mm < 10 ? '0' + mm : mm; | |
88 | var dd = currentdate.getDate(); | |
89 | dd = dd < 10 ? '0' + dd : dd; | |
90 | var hh = currentdate.getHours(); | |
91 | hh = hh < 10 ? '0' + hh : hh; | |
92 | var min = currentdate.getMinutes(); | |
93 | min = min < 10 ? '0' + min : min; | |
94 | var sec = currentdate.getSeconds(); | |
95 | sec = sec < 10 ? '0' + sec : sec; | |
96 | return yyyy + "-" + mm + "-" + dd + " " + hh + ":" + min + ":" + sec; | |
97 | }; | |
98 | }); | |
99 | ||
226ef186 TO |
100 | // Adapter for CRM.status which supports Angular promises (instead of jQuery promises) |
101 | // example: crmStatus('Saving', crmApi(...)).then(function(result){...}) | |
102 | angular.module('crmUtil').factory('crmStatus', function($q){ | |
103 | return function(options, aPromise){ | |
104 | return CRM.toAPromise($q, CRM.status(options, CRM.toJqPromise(aPromise))); | |
105 | }; | |
106 | }); | |
107 | ||
60ebf0a5 TO |
108 | // crmWatcher allows one to setup event listeners and temporarily suspend |
109 | // them en masse. | |
110 | // | |
111 | // example: | |
112 | // angular.controller(... function($scope, crmWatcher){ | |
113 | // var watcher = crmWatcher(); | |
114 | // function myfunc() { | |
115 | // watcher.suspend('foo', function(){ | |
116 | // ...do stuff... | |
117 | // }); | |
118 | // } | |
119 | // watcher.setup('foo', function(){ | |
120 | // return [ | |
121 | // $scope.$watch('foo', myfunc), | |
122 | // $scope.$watch('bar', myfunc), | |
123 | // $scope.$watch('whiz', otherfunc) | |
124 | // ]; | |
125 | // }); | |
126 | // }); | |
127 | angular.module('crmUtil').factory('crmWatcher', function(){ | |
128 | return function() { | |
129 | var unwatches = {}, watchFactories = {}, suspends = {}; | |
130 | ||
131 | // Specify the list of watches | |
132 | this.setup = function(name, newWatchFactory) { | |
133 | watchFactories[name] = newWatchFactory; | |
134 | unwatches[name] = watchFactories[name](); | |
135 | suspends[name] = 0; | |
136 | return this; | |
137 | }; | |
138 | ||
139 | // Temporarily disable watches and run some logic | |
140 | this.suspend = function(name, f) { | |
141 | suspends[name]++; | |
142 | this.teardown(name); | |
143 | var r; | |
144 | try { | |
145 | r = f.apply(this, []); | |
146 | } finally { | |
147 | if (suspends[name] === 1) { | |
148 | unwatches[name] = watchFactories[name](); | |
149 | if (!angular.isArray(unwatches[name])) { | |
150 | unwatches[name] = [unwatches[name]]; | |
151 | } | |
152 | } | |
153 | suspends[name]--; | |
154 | } | |
155 | return r; | |
156 | }; | |
157 | ||
158 | this.teardown = function(name) { | |
159 | if (!unwatches[name]) return; | |
160 | _.each(unwatches[name], function(unwatch){ | |
161 | unwatch(); | |
162 | }); | |
163 | delete unwatches[name]; | |
164 | }; | |
165 | ||
166 | return this; | |
f2bad133 | 167 | }; |
60ebf0a5 TO |
168 | }); |
169 | ||
60ebf0a5 | 170 | })(angular, CRM.$, CRM._); |