LocationType - Use standard delete function which calls hooks
[civicrm-core.git] / js / angular-crmResource / all.js
CommitLineData
a2dc0f82
TO
1// crmResource: Given a templateUrl "~/mymodule/myfile.html", load the matching HTML.
2// This implementation loads all partials and strings in one batch.
3(function(angular, $, _) {
4 angular.module('crmResource', []);
5
6 angular.module('crmResource').factory('crmResource', function($q, $http) {
7 var deferreds = {}; // null|object; deferreds[url][idx] = Deferred;
8 var templates = null; // null|object; templates[url] = HTML;
9
10 var notify = function notify() {
11 var oldDfrds = deferreds;
12 deferreds = null;
13
14 angular.forEach(oldDfrds, function(dfrs, url) {
15 if (templates[url]) {
16 angular.forEach(dfrs, function(dfr) {
17 dfr.resolve({
18 status: 200,
19 headers: function(name) {
20 var headers = {'Content-type': 'text/html'};
21 return name ? headers[name] : headers;
22 },
23 data: templates[url]
24 });
25 });
26 }
27 else {
28 angular.forEach(dfrs, function(dfr) {
29 dfr.reject({status: 500}); // FIXME
30 });
31 }
32 });
33 };
34
466e4b29 35 var moduleUrl = CRM.angular.bundleUrl;
a2dc0f82 36 $http.get(moduleUrl)
7585cf5b 37 .then(function httpSuccess(response) {
a2dc0f82 38 templates = [];
7585cf5b 39 angular.forEach(response.data, function(module) {
a2dc0f82
TO
40 if (module.partials) {
41 angular.extend(templates, module.partials);
42 }
43 if (module.strings) {
e3d90d6c 44 CRM.addStrings(module.domain, module.strings);
a2dc0f82
TO
45 }
46 });
47 notify();
7585cf5b 48 }, function httpError() {
a2dc0f82
TO
49 templates = [];
50 notify();
51 });
52
53 return {
54 // @return string|Promise<string>
55 getUrl: function getUrl(url) {
56 if (templates !== null) {
57 return templates[url];
58 }
59 else {
60 var deferred = $q.defer();
61 if (!deferreds[url]) {
62 deferreds[url] = [];
63 }
64 deferreds[url].push(deferred);
65 return deferred.promise;
66 }
67 }
68 };
69 });
70
71 angular.module('crmResource').config(function($provide) {
72 $provide.decorator('$templateCache', function($delegate, $http, $q, crmResource) {
73 var origGet = $delegate.get;
74 var urlPat = /^~\//;
75 $delegate.get = function(url) {
76 if (urlPat.test(url)) {
77 return crmResource.getUrl(url);
78 }
79 else {
80 return origGet.call(this, url);
81 }
82 };
83 return $delegate;
84 });
85 });
86
87})(angular, CRM.$, CRM._);