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
) {
60 // Convert {key:$,value:$} sequence to unordered {$key: $value} map.
61 function convertOptionsToMap(options
) {
63 angular
.forEach(options
, function(o
) {
64 result
[o
.key
] = o
.value
;
69 var cache
= {}; // cache[entityName+'::'+action][fieldName].title
70 var deferreds
= {}; // deferreds[cacheKey].push($q.defer())
72 // usage: $q.when(crmMetadata.getField('MyEntity', 'my_field')).then(...);
73 getField
: function getField(entity
, field
) {
74 return $q
.when(crmMetadata
.getFields(entity
)).then(function(fields
){
78 // usage: $q.when(crmMetadata.getFields('MyEntity')).then(...);
79 // usage: $q.when(crmMetadata.getFields(['MyEntity', 'myaction'])).then(...);
80 getFields
: function getFields(entity
) {
81 var action
= '', cacheKey
;
82 if (_
.isArray(entity
)) {
85 cacheKey
= entity
+ '::' + action
;
90 if (_
.isObject(cache
[cacheKey
])) {
91 return cache
[cacheKey
];
94 var needFetch
= _
.isEmpty(deferreds
[cacheKey
]);
95 deferreds
[cacheKey
] = deferreds
[cacheKey
] || [];
96 var deferred
= $q
.defer();
97 deferreds
[cacheKey
].push(deferred
);
100 crmApi(entity
, 'getfields', {action
: action
, sequential
: 1, options
: {get_options
: 'all'}})
104 cache
[cacheKey
] = _
.indexBy(fields
.values
, 'name');
105 angular
.forEach(cache
[cacheKey
],function (field
){
107 field
.optionsMap
= convertOptionsToMap(field
.options
);
110 angular
.forEach(deferreds
[cacheKey
], function(dfr
) {
111 dfr
.resolve(cache
[cacheKey
]);
113 delete deferreds
[cacheKey
];
117 cache
[cacheKey
] = {}; // cache nack
118 angular
.forEach(deferreds
[cacheKey
], function(dfr
) {
121 delete deferreds
[cacheKey
];
126 return deferred
.promise
;
134 // var block = $scope.block = crmBlocker();
135 // $scope.save = function() { return block(crmApi('MyEntity','create',...)); };
136 // <button ng-click="save()" ng-disabled="block.check()">Do something</button>
137 angular
.module('crmUtil').factory('crmBlocker', function() {
140 var result = function(promise
) {
142 return promise
.finally(function() {
146 result
.check = function() {
153 angular
.module('crmUtil').factory('crmLegacy', function() {
157 // example: scope.$watch('foo', crmLog.wrap(function(newValue, oldValue){ ... }));
158 angular
.module('crmUtil').factory('crmLog', function(){
160 var write
= console
.log
;
163 for (var i
= 0; i
< level
; i
++) s
= s
+ ' ';
167 log: function(msg
, vars
) {
168 write(indent() + msg
, vars
);
170 wrap: function(label
, f
) {
173 crmLog
.log(label
+ ": start", arguments
);
176 r
= f
.apply(this, arguments
);
178 crmLog
.log(label
+ ": end");
188 angular
.module('crmUtil').factory('crmNavigator', ['$window', function($window
) {
190 redirect: function(path
) {
191 $window
.location
.href
= path
;
196 // Wrap an async function in a queue, ensuring that independent async calls are issued in strict sequence.
197 // usage: qApi = crmQueue(crmApi); qApi(entity,action,...).then(...); qApi(entity2,action2,...).then(...);
198 // This is similar to promise-chaining, but allows chaining independent procs (without explicitly sharing promises).
199 angular
.module('crmUtil').factory('crmQueue', function($q
) {
200 // @param worker A function which generates promises
201 return function crmQueue(worker
) {
205 worker
.apply(null, task
.a
).then(
206 function onOk(data
) {
208 task
.dfr
.resolve(data
);
209 if (queue
.length
> 0) next();
211 function onErr(err
) {
213 task
.dfr
.reject(err
);
214 if (queue
.length
> 0) next();
219 var dfr
= $q
.defer();
220 queue
.push({a
: arguments
, dfr
: dfr
});
221 if (queue
.length
=== 1) {
230 // Adapter for CRM.status which supports Angular promises (instead of jQuery promises)
231 // example: crmStatus('Saving', crmApi(...)).then(function(result){...})
232 angular
.module('crmUtil').factory('crmStatus', function($q
){
233 return function(options
, aPromise
){
235 return CRM
.toAPromise($q
, CRM
.status(options
, CRM
.toJqPromise(aPromise
)));
237 return CRM
.toAPromise($q
, CRM
.status(options
));
242 // crmWatcher allows one to setup event listeners and temporarily suspend
246 // angular.controller(... function($scope, crmWatcher){
247 // var watcher = crmWatcher();
248 // function myfunc() {
249 // watcher.suspend('foo', function(){
253 // watcher.setup('foo', function(){
255 // $scope.$watch('foo', myfunc),
256 // $scope.$watch('bar', myfunc),
257 // $scope.$watch('whiz', otherfunc)
261 angular
.module('crmUtil').factory('crmWatcher', function(){
263 var unwatches
= {}, watchFactories
= {}, suspends
= {};
265 // Specify the list of watches
266 this.setup = function(name
, newWatchFactory
) {
267 watchFactories
[name
] = newWatchFactory
;
268 unwatches
[name
] = watchFactories
[name
]();
273 // Temporarily disable watches and run some logic
274 this.suspend = function(name
, f
) {
279 r
= f
.apply(this, []);
281 if (suspends
[name
] === 1) {
282 unwatches
[name
] = watchFactories
[name
]();
283 if (!angular
.isArray(unwatches
[name
])) {
284 unwatches
[name
] = [unwatches
[name
]];
292 this.teardown = function(name
) {
293 if (!unwatches
[name
]) return;
294 _
.each(unwatches
[name
], function(unwatch
){
297 delete unwatches
[name
];
304 })(angular
, CRM
.$, CRM
._
);