/usr/share/civicrm/ang/crmUtil.js is in civicrm-common 4.7.1+dfsg-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | /// crmUi: Sundry UI helpers
(function (angular, $, _) {
angular.module('crmUtil', []);
// Angular implementation of CRM.api3
// @link http://wiki.civicrm.org/confluence/display/CRMDOC/AJAX+Interface#AJAXInterface-CRM.api3
//
// Note: To mock API results in unit-tests, override crmApi.backend, e.g.
// var apiSpy = jasmine.createSpy('crmApi');
// crmApi.backend = apiSpy.and.returnValue(crmApi.val({
// is_error: 1
// }));
angular.module('crmUtil').factory('crmApi', function($q) {
var crmApi = function(entity, action, params, message) {
// JSON serialization in CRM.api3 is not aware of Angular metadata like $$hash, so use angular.toJson()
var deferred = $q.defer();
var p;
var backend = crmApi.backend || CRM.api3;
if (_.isObject(entity)) {
// eval content is locally generated.
/*jshint -W061 */
p = backend(eval('('+angular.toJson(entity)+')'), action);
} else {
// eval content is locally generated.
/*jshint -W061 */
p = backend(entity, action, eval('('+angular.toJson(params)+')'), message);
}
// CRM.api3 returns a promise, but the promise doesn't really represent errors as errors, so we
// convert them
p.then(
function(result) {
if (result.is_error) {
deferred.reject(result);
} else {
deferred.resolve(result);
}
},
function(error) {
deferred.reject(error);
}
);
return deferred.promise;
};
crmApi.backend = null;
crmApi.val = function(value) {
var d = $.Deferred();
d.resolve(value);
return d.promise();
};
return crmApi;
});
// Get and cache the metadata for an API entity.
// usage:
// $q.when(crmMetadata.getFields('MyEntity'), function(fields){
// console.log('The fields are:', options);
// });
angular.module('crmUtil').factory('crmMetadata', function($q, crmApi) {
// Convert {key:$,value:$} sequence to unordered {$key: $value} map.
function convertOptionsToMap(options) {
var result = {};
angular.forEach(options, function(o) {
result[o.key] = o.value;
});
return result;
}
var cache = {}; // cache[entityName+'::'+action][fieldName].title
var deferreds = {}; // deferreds[cacheKey].push($q.defer())
var crmMetadata = {
// usage: $q.when(crmMetadata.getField('MyEntity', 'my_field')).then(...);
getField: function getField(entity, field) {
return $q.when(crmMetadata.getFields(entity)).then(function(fields){
return fields[field];
});
},
// usage: $q.when(crmMetadata.getFields('MyEntity')).then(...);
// usage: $q.when(crmMetadata.getFields(['MyEntity', 'myaction'])).then(...);
getFields: function getFields(entity) {
var action = '', cacheKey;
if (_.isArray(entity)) {
action = entity[1];
entity = entity[0];
cacheKey = entity + '::' + action;
} else {
cacheKey = entity;
}
if (_.isObject(cache[cacheKey])) {
return cache[cacheKey];
}
var needFetch = _.isEmpty(deferreds[cacheKey]);
deferreds[cacheKey] = deferreds[cacheKey] || [];
var deferred = $q.defer();
deferreds[cacheKey].push(deferred);
if (needFetch) {
crmApi(entity, 'getfields', {action: action, sequential: 1, options: {get_options: 'all'}})
.then(
// on success:
function(fields) {
cache[cacheKey] = _.indexBy(fields.values, 'name');
angular.forEach(cache[cacheKey],function (field){
if (field.options) {
field.optionsMap = convertOptionsToMap(field.options);
}
});
angular.forEach(deferreds[cacheKey], function(dfr) {
dfr.resolve(cache[cacheKey]);
});
delete deferreds[cacheKey];
},
// on error:
function() {
cache[cacheKey] = {}; // cache nack
angular.forEach(deferreds[cacheKey], function(dfr) {
dfr.reject();
});
delete deferreds[cacheKey];
}
);
}
return deferred.promise;
}
};
return crmMetadata;
});
// usage:
// var block = $scope.block = crmBlocker();
// $scope.save = function() { return block(crmApi('MyEntity','create',...)); };
// <button ng-click="save()" ng-disabled="block.check()">Do something</button>
angular.module('crmUtil').factory('crmBlocker', function() {
return function() {
var blocks = 0;
var result = function(promise) {
blocks++;
return promise.finally(function() {
blocks--;
});
};
result.check = function() {
return blocks > 0;
};
return result;
};
});
angular.module('crmUtil').factory('crmLegacy', function() {
return CRM;
});
// example: scope.$watch('foo', crmLog.wrap(function(newValue, oldValue){ ... }));
angular.module('crmUtil').factory('crmLog', function(){
var level = 0;
var write = console.log;
function indent() {
var s = '>';
for (var i = 0; i < level; i++) s = s + ' ';
return s;
}
var crmLog = {
log: function(msg, vars) {
write(indent() + msg, vars);
},
wrap: function(label, f) {
return function(){
level++;
crmLog.log(label + ": start", arguments);
var r;
try {
r = f.apply(this, arguments);
} finally {
crmLog.log(label + ": end");
level--;
}
return r;
};
}
};
return crmLog;
});
angular.module('crmUtil').factory('crmNavigator', ['$window', function($window) {
return {
redirect: function(path) {
$window.location.href = path;
}
};
}]);
// Wrap an async function in a queue, ensuring that independent async calls are issued in strict sequence.
// usage: qApi = crmQueue(crmApi); qApi(entity,action,...).then(...); qApi(entity2,action2,...).then(...);
// This is similar to promise-chaining, but allows chaining independent procs (without explicitly sharing promises).
angular.module('crmUtil').factory('crmQueue', function($q) {
// @param worker A function which generates promises
return function crmQueue(worker) {
var queue = [];
function next() {
var task = queue[0];
worker.apply(null, task.a).then(
function onOk(data) {
queue.shift();
task.dfr.resolve(data);
if (queue.length > 0) next();
},
function onErr(err) {
queue.shift();
task.dfr.reject(err);
if (queue.length > 0) next();
}
);
}
function enqueue() {
var dfr = $q.defer();
queue.push({a: arguments, dfr: dfr});
if (queue.length === 1) {
next();
}
return dfr.promise;
}
return enqueue;
};
});
// Adapter for CRM.status which supports Angular promises (instead of jQuery promises)
// example: crmStatus('Saving', crmApi(...)).then(function(result){...})
angular.module('crmUtil').factory('crmStatus', function($q){
return function(options, aPromise){
if (aPromise) {
return CRM.toAPromise($q, CRM.status(options, CRM.toJqPromise(aPromise)));
} else {
return CRM.toAPromise($q, CRM.status(options));
}
};
});
// crmWatcher allows one to setup event listeners and temporarily suspend
// them en masse.
//
// example:
// angular.controller(... function($scope, crmWatcher){
// var watcher = crmWatcher();
// function myfunc() {
// watcher.suspend('foo', function(){
// ...do stuff...
// });
// }
// watcher.setup('foo', function(){
// return [
// $scope.$watch('foo', myfunc),
// $scope.$watch('bar', myfunc),
// $scope.$watch('whiz', otherfunc)
// ];
// });
// });
angular.module('crmUtil').factory('crmWatcher', function(){
return function() {
var unwatches = {}, watchFactories = {}, suspends = {};
// Specify the list of watches
this.setup = function(name, newWatchFactory) {
watchFactories[name] = newWatchFactory;
unwatches[name] = watchFactories[name]();
suspends[name] = 0;
return this;
};
// Temporarily disable watches and run some logic
this.suspend = function(name, f) {
suspends[name]++;
this.teardown(name);
var r;
try {
r = f.apply(this, []);
} finally {
if (suspends[name] === 1) {
unwatches[name] = watchFactories[name]();
if (!angular.isArray(unwatches[name])) {
unwatches[name] = [unwatches[name]];
}
}
suspends[name]--;
}
return r;
};
this.teardown = function(name) {
if (!unwatches[name]) return;
_.each(unwatches[name], function(unwatch){
unwatch();
});
delete unwatches[name];
};
return this;
};
});
})(angular, CRM.$, CRM._);
|