2349fcff97c472730b277d5b6fe75d44ca104b1f
1 /// crmUi: Sundry UI helpers
2 (function (angular
, $, _
) {
3 angular
.module('crmUtil', []);
6 // crmApi('Entity', 'action', {...}).then(function(apiResult){...})
8 // Note: To mock API results in unit-tests, override crmApi.backend, e.g.
9 // var apiSpy = jasmine.createSpy('crmApi');
10 // crmApi.backend = apiSpy.and.returnValue(crmApi.val({
13 angular
.module('crmUtil').factory('crmApi', function($q
) {
14 var crmApi = function(entity
, action
, params
, message
) {
15 // JSON serialization in CRM.api3 is not aware of Angular metadata like $$hash, so use angular.toJson()
16 var deferred
= $q
.defer();
18 var backend
= crmApi
.backend
|| CRM
.api3
;
19 if (_
.isObject(entity
)) {
20 // eval content is locally generated.
22 p
= backend(eval('('+angular
.toJson(entity
)+')'), message
);
24 // eval content is locally generated.
26 p
= backend(entity
, action
, eval('('+angular
.toJson(params
)+')'), message
);
28 // CRM.api3 returns a promise, but the promise doesn't really represent errors as errors, so we
32 if (result
.is_error
) {
33 deferred
.reject(result
);
35 deferred
.resolve(result
);
39 deferred
.reject(error
);
42 return deferred
.promise
;
44 crmApi
.backend
= null;
45 crmApi
.val = function(value
) {
53 // Get and cache the metadata for an API entity.
55 // $q.when(crmMetadata.getFields('MyEntity'), function(fields){
56 // console.log('The fields are:', options);
58 angular
.module('crmUtil').factory('crmMetadata', function($q
, crmApi
) {
59 var cache
= {}; // cache[entityName+'::'+action][fieldName].title
60 var deferreds
= {}; // deferreds[cacheKey].push($q.defer())
62 // usage: $q.when(crmMetadata.getField('MyEntity', 'my_field')).then(...);
63 getField
: function getField(entity
, field
) {
64 return $q
.when(crmMetadata
.getFields(entity
)).then(function(fields
){
68 // usage: $q.when(crmMetadata.getFields('MyEntity')).then(...);
69 // usage: $q.when(crmMetadata.getFields(['MyEntity', 'myaction'])).then(...);
70 getFields
: function getFields(entity
) {
71 var action
= '', cacheKey
;
72 if (_
.isArray(entity
)) {
75 cacheKey
= entity
+ '::' + action
;
80 if (_
.isObject(cache
[cacheKey
])) {
81 return cache
[cacheKey
];
84 var needFetch
= _
.isEmpty(deferreds
[cacheKey
]);
85 deferreds
[cacheKey
] = deferreds
[cacheKey
] || [];
86 var deferred
= $q
.defer();
87 deferreds
[cacheKey
].push(deferred
);
90 crmApi(entity
, 'getfields', {action
: action
, options
: {get_options
: 'all'}})
94 cache
[cacheKey
] = fields
.values
;
95 angular
.forEach(deferreds
[cacheKey
], function(dfr
) {
96 dfr
.resolve(fields
.values
);
98 delete deferreds
[cacheKey
];
102 cache
[cacheKey
] = {}; // cache nack
103 angular
.forEach(deferreds
[cacheKey
], function(dfr
) {
106 delete deferreds
[cacheKey
];
111 return deferred
.promise
;
119 // var block = $scope.block = crmBlocker();
120 // $scope.save = function() { return block(crmApi('MyEntity','create',...)); };
121 // <button ng-click="save()" ng-disabled="block.check()">Do something</button>
122 angular
.module('crmUtil').factory('crmBlocker', function() {
125 var result = function(promise
) {
127 return promise
.finally(function() {
131 result
.check = function() {
138 angular
.module('crmUtil').factory('crmLegacy', function() {
142 // example: scope.$watch('foo', crmLog.wrap(function(newValue, oldValue){ ... }));
143 angular
.module('crmUtil').factory('crmLog', function(){
145 var write
= console
.log
;
148 for (var i
= 0; i
< level
; i
++) s
= s
+ ' ';
152 log: function(msg
, vars
) {
153 write(indent() + msg
, vars
);
155 wrap: function(label
, f
) {
158 crmLog
.log(label
+ ": start", arguments
);
161 r
= f
.apply(this, arguments
);
163 crmLog
.log(label
+ ": end");
173 angular
.module('crmUtil').factory('crmNavigator', ['$window', function($window
) {
175 redirect: function(path
) {
176 $window
.location
.href
= path
;
181 angular
.module('crmUtil').factory('crmNow', function($q
){
182 // FIXME: surely there's already some helper which can do this in one line?
183 // @return string "YYYY-MM-DD hh:mm:ss"
184 return function crmNow() {
185 var currentdate
= new Date();
186 var yyyy
= currentdate
.getFullYear();
187 var mm
= currentdate
.getMonth() + 1;
188 mm
= mm
< 10 ? '0' + mm
: mm
;
189 var dd
= currentdate
.getDate();
190 dd
= dd
< 10 ? '0' + dd
: dd
;
191 var hh
= currentdate
.getHours();
192 hh
= hh
< 10 ? '0' + hh
: hh
;
193 var min
= currentdate
.getMinutes();
194 min
= min
< 10 ? '0' + min
: min
;
195 var sec
= currentdate
.getSeconds();
196 sec
= sec
< 10 ? '0' + sec
: sec
;
197 return yyyy
+ "-" + mm
+ "-" + dd
+ " " + hh
+ ":" + min
+ ":" + sec
;
201 // Adapter for CRM.status which supports Angular promises (instead of jQuery promises)
202 // example: crmStatus('Saving', crmApi(...)).then(function(result){...})
203 angular
.module('crmUtil').factory('crmStatus', function($q
){
204 return function(options
, aPromise
){
206 return CRM
.toAPromise($q
, CRM
.status(options
, CRM
.toJqPromise(aPromise
)));
208 return CRM
.toAPromise($q
, CRM
.status(options
));
213 // crmWatcher allows one to setup event listeners and temporarily suspend
217 // angular.controller(... function($scope, crmWatcher){
218 // var watcher = crmWatcher();
219 // function myfunc() {
220 // watcher.suspend('foo', function(){
224 // watcher.setup('foo', function(){
226 // $scope.$watch('foo', myfunc),
227 // $scope.$watch('bar', myfunc),
228 // $scope.$watch('whiz', otherfunc)
232 angular
.module('crmUtil').factory('crmWatcher', function(){
234 var unwatches
= {}, watchFactories
= {}, suspends
= {};
236 // Specify the list of watches
237 this.setup = function(name
, newWatchFactory
) {
238 watchFactories
[name
] = newWatchFactory
;
239 unwatches
[name
] = watchFactories
[name
]();
244 // Temporarily disable watches and run some logic
245 this.suspend = function(name
, f
) {
250 r
= f
.apply(this, []);
252 if (suspends
[name
] === 1) {
253 unwatches
[name
] = watchFactories
[name
]();
254 if (!angular
.isArray(unwatches
[name
])) {
255 unwatches
[name
] = [unwatches
[name
]];
263 this.teardown = function(name
) {
264 if (!unwatches
[name
]) return;
265 _
.each(unwatches
[name
], function(unwatch
){
268 delete unwatches
[name
];
275 })(angular
, CRM
.$, CRM
._
);