Commit | Line | Data |
---|---|---|
685acae4 | 1 | /// crmUi: Sundry UI helpers |
2 | (function (angular, $, _) { | |
3 | ||
f8601d61 TO |
4 | var uidCount = 0; |
5 | ||
685acae4 | 6 | angular.module('crmUi', []) |
030dce01 | 7 | |
0cbed02c TO |
8 | // example <div crm-ui-accordion crm-title="ts('My Title')" crm-collapsed="true">...content...</div> |
9 | // WISHLIST: crmCollapsed should support two-way/continous binding | |
030dce01 TO |
10 | .directive('crmUiAccordion', function() { |
11 | return { | |
12 | scope: { | |
207819ec | 13 | crmUiAccordion: '=' |
030dce01 | 14 | }, |
207819ec | 15 | template: '<div ng-class="cssClasses"><div class="crm-accordion-header">{{crmUiAccordion.title}} <a crm-ui-help="help" ng-if="help"></a></div><div class="crm-accordion-body" ng-transclude></div></div>', |
030dce01 | 16 | transclude: true, |
0cbed02c TO |
17 | link: function (scope, element, attrs) { |
18 | scope.cssClasses = { | |
207819ec CW |
19 | 'crm-accordion-wrapper': true, |
20 | collapsed: scope.crmUiAccordion.collapsed | |
0cbed02c | 21 | }; |
207819ec CW |
22 | scope.help = null; |
23 | scope.$watch('crmUiAccordion', function(crmUiAccordion) { | |
24 | if (crmUiAccordion && crmUiAccordion.help) { | |
25 | scope.help = crmUiAccordion.help.clone({}, { | |
26 | title: crmUiAccordion.title | |
27 | }); | |
28 | } | |
29 | }); | |
0cbed02c | 30 | } |
030dce01 TO |
31 | }; |
32 | }) | |
33 | ||
69a65adc TO |
34 | // Examples: |
35 | // crmUiAlert({text: 'My text', title: 'My title', type: 'error'}); | |
36 | // crmUiAlert({template: '<a ng-click="ok()">Hello</a>', scope: $scope.$new()}); | |
37 | // var h = crmUiAlert({templateUrl: '~/crmFoo/alert.html', scope: $scope.$new()}); | |
38 | // ... h.close(); ... | |
39 | .service('crmUiAlert', function($compile, $rootScope, $templateRequest, $q) { | |
40 | var count = 0; | |
41 | return function crmUiAlert(params) { | |
42 | var id = 'crmUiAlert_' + (++count); | |
43 | var tpl = null; | |
44 | if (params.templateUrl) { | |
45 | tpl = $templateRequest(params.templateUrl); | |
46 | } | |
47 | else if (params.template) { | |
48 | tpl = params.template; | |
49 | } | |
50 | if (tpl) { | |
51 | params.text = '<div id="' + id + '"></div>'; // temporary stub | |
52 | } | |
53 | var result = CRM.alert(params.text, params.title, params.type, params.options); | |
54 | if (tpl) { | |
55 | $q.when(tpl, function(html) { | |
56 | var scope = params.scope || $rootScope.$new(); | |
57 | var linker = $compile(html); | |
58 | $('#' + id).append($(linker(scope))); | |
59 | }); | |
60 | } | |
61 | return result; | |
62 | }; | |
63 | }) | |
64 | ||
a42344f4 CW |
65 | // Simple wrapper around $.crmDatepicker. |
66 | // example with no time input: <input crm-ui-datepicker="{time: false}" ng-model="myobj.datefield"/> | |
67 | // example with custom date format: <input crm-ui-datepicker="{dateFormat: 'm/d/y'}" ng-model="myobj.datefield"/> | |
68 | .directive('crmUiDatepicker', function () { | |
510d515d TO |
69 | return { |
70 | restrict: 'AE', | |
2f31bc16 | 71 | require: 'ngModel', |
510d515d | 72 | scope: { |
a42344f4 | 73 | crmUiDatepicker: '=' |
510d515d | 74 | }, |
2f31bc16 | 75 | link: function (scope, element, attrs, ngModel) { |
ac5009c1 CW |
76 | ngModel.$render = function () { |
77 | element.val(ngModel.$viewValue).change(); | |
78 | }; | |
79 | ||
a42344f4 CW |
80 | element |
81 | .crmDatepicker(scope.crmUiDatepicker) | |
82 | .on('change', function() { | |
ac5009c1 | 83 | var requiredLength = 19; |
a42344f4 CW |
84 | if (scope.crmUiDatepicker && scope.crmUiDatepicker.time === false) { |
85 | requiredLength = 10; | |
86 | } | |
87 | if (scope.crmUiDatepicker && scope.crmUiDatepicker.date === false) { | |
ac5009c1 | 88 | requiredLength = 8; |
a42344f4 CW |
89 | } |
90 | ngModel.$setValidity('incompleteDateTime', !($(this).val().length && $(this).val().length !== requiredLength)); | |
510d515d | 91 | }); |
510d515d TO |
92 | } |
93 | }; | |
94 | }) | |
95 | ||
c4c69e7b TO |
96 | // Display debug information (if available) |
97 | // For richer DX, checkout Batarang/ng-inspector (Chrome/Safari), or AngScope/ng-inspect (Firefox). | |
98 | // example: <div crm-ui-debug="myobject" /> | |
99 | .directive('crmUiDebug', function ($location) { | |
100 | return { | |
101 | restrict: 'AE', | |
102 | scope: { | |
103 | crmUiDebug: '@' | |
104 | }, | |
105 | template: function() { | |
106 | var args = $location.search(); | |
916f65c0 | 107 | return (args && args.angularDebug) ? '<div crm-ui-accordion=\'{title: ts("Debug (%1)", {1: crmUiDebug}), collapsed: true}\'><pre>{{data|json}}</pre></div>' : ''; |
c4c69e7b TO |
108 | }, |
109 | link: function(scope, element, attrs) { | |
110 | var args = $location.search(); | |
111 | if (args && args.angularDebug) { | |
112 | scope.ts = CRM.ts(null); | |
113 | scope.$parent.$watch(attrs.crmUiDebug, function(data) { | |
114 | scope.data = data; | |
115 | }); | |
116 | } | |
117 | } | |
118 | }; | |
119 | }) | |
120 | ||
489c2674 | 121 | // Display a field/row in a field list |
c4256f35 CW |
122 | // example: <div crm-ui-field="{title: ts('My Field')}"> {{mydata}} </div> |
123 | // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field')}"> <input crm-ui-id="subform.myfield" name="myfield" /> </div> | |
124 | // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field')}"> <input crm-ui-id="subform.myfield" name="myfield" required /> </div> | |
563ba527 | 125 | // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field'), help: hs('help_field_name')}"> {{mydata}} </div> |
3cc9c048 | 126 | .directive('crmUiField', function() { |
489c2674 TO |
127 | // Note: When writing new templates, the "label" position is particular. See/patch "var label" below. |
128 | var templateUrls = { | |
ef5d18a1 TO |
129 | default: '~/crmUi/field.html', |
130 | checkbox: '~/crmUi/field-cb.html' | |
489c2674 TO |
131 | }; |
132 | ||
133 | return { | |
f8601d61 TO |
134 | require: '^crmUiIdScope', |
135 | restrict: 'EA', | |
489c2674 | 136 | scope: { |
d8356d0b | 137 | // {title, name, help, helpFile} |
c4256f35 | 138 | crmUiField: '=' |
489c2674 TO |
139 | }, |
140 | templateUrl: function(tElement, tAttrs){ | |
141 | var layout = tAttrs.crmLayout ? tAttrs.crmLayout : 'default'; | |
142 | return templateUrls[layout]; | |
143 | }, | |
144 | transclude: true, | |
f8601d61 | 145 | link: function (scope, element, attrs, crmUiIdCtrl) { |
489c2674 | 146 | $(element).addClass('crm-section'); |
563ba527 | 147 | scope.help = null; |
dfc2a868 TO |
148 | scope.$watch('crmUiField', function(crmUiField) { |
149 | if (crmUiField && crmUiField.help) { | |
150 | scope.help = crmUiField.help.clone({}, { | |
151 | title: crmUiField.title | |
152 | }); | |
153 | } | |
154 | }); | |
f8601d61 TO |
155 | } |
156 | }; | |
157 | }) | |
158 | ||
159 | // example: <div ng-form="subform" crm-ui-id-scope><label crm-ui-for="subform.foo">Foo:</label><input crm-ui-id="subform.foo" name="foo"/></div> | |
160 | .directive('crmUiId', function () { | |
161 | return { | |
162 | require: '^crmUiIdScope', | |
163 | restrict: 'EA', | |
3cc9c048 TO |
164 | link: { |
165 | pre: function (scope, element, attrs, crmUiIdCtrl) { | |
166 | var id = crmUiIdCtrl.get(attrs.crmUiId); | |
167 | element.attr('id', id); | |
168 | } | |
f8601d61 TO |
169 | } |
170 | }; | |
171 | }) | |
489c2674 | 172 | |
dfc2a868 TO |
173 | // for example, see crmUiHelp |
174 | .service('crmUiHelp', function(){ | |
175 | // example: var h = new FieldHelp({id: 'foo'}); h.open(); | |
176 | function FieldHelp(options) { | |
177 | this.options = options; | |
178 | } | |
179 | angular.extend(FieldHelp.prototype, { | |
180 | get: function(n) { | |
181 | return this.options[n]; | |
182 | }, | |
183 | open: function open() { | |
dfc2a868 TO |
184 | CRM.help(this.options.title, {id: this.options.id, file: this.options.file}); |
185 | }, | |
186 | clone: function clone(options, defaults) { | |
dfc2a868 TO |
187 | return new FieldHelp(angular.extend({}, defaults, this.options, options)); |
188 | } | |
189 | }); | |
190 | ||
563ba527 | 191 | // example: var hs = crmUiHelp({file: 'CRM/Foo/Bar'}); |
dfc2a868 | 192 | return function(defaults){ |
563ba527 TO |
193 | // example: hs('myfield') |
194 | // example: hs({id: 'myfield', title: 'Foo Bar', file: 'Whiz/Bang'}) | |
dfc2a868 TO |
195 | return function(options) { |
196 | if (_.isString(options)) { | |
197 | options = {id: options}; | |
198 | } | |
199 | return new FieldHelp(angular.extend({}, defaults, options)); | |
6dc9d4ca TO |
200 | }; |
201 | }; | |
dfc2a868 TO |
202 | }) |
203 | ||
204 | // Display a help icon | |
9a593827 | 205 | // Example: Use a default *.hlp file |
563ba527 TO |
206 | // scope.hs = crmUiHelp({file: 'Path/To/Help/File'}); |
207 | // HTML: <a crm-ui-help="hs({title:ts('My Field'), id:'my_field'})"> | |
9a593827 | 208 | // Example: Use an explicit *.hlp file |
563ba527 | 209 | // HTML: <a crm-ui-help="hs({title:ts('My Field'), id:'my_field', file:'CRM/Foo/Bar'})"> |
a517b78a CW |
210 | .directive('crmUiHelp', function() { |
211 | return { | |
212 | restrict: 'EA', | |
563ba527 TO |
213 | link: function(scope, element, attrs) { |
214 | setTimeout(function() { | |
215 | var crmUiHelp = scope.$eval(attrs.crmUiHelp); | |
dfc2a868 TO |
216 | var title = crmUiHelp && crmUiHelp.get('title') ? ts('%1 Help', {1: crmUiHelp.get('title')}) : ts('Help'); |
217 | element.attr('title', title); | |
563ba527 | 218 | }, 50); |
dfc2a868 | 219 | |
a517b78a CW |
220 | element |
221 | .addClass('helpicon') | |
a517b78a CW |
222 | .attr('href', '#') |
223 | .on('click', function(e) { | |
224 | e.preventDefault(); | |
563ba527 | 225 | scope.$eval(attrs.crmUiHelp).open(); |
a517b78a CW |
226 | }); |
227 | } | |
228 | }; | |
229 | }) | |
230 | ||
f8601d61 TO |
231 | // example: <div ng-form="subform" crm-ui-id-scope><label crm-ui-for="subform.foo">Foo:</label><input crm-ui-id="subform.foo" name="foo"/></div> |
232 | .directive('crmUiFor', function ($parse, $timeout) { | |
233 | return { | |
234 | require: '^crmUiIdScope', | |
235 | restrict: 'EA', | |
236 | template: '<span ng-class="cssClasses"><span ng-transclude/><span crm-ui-visible="crmIsRequired" class="crm-marker" title="This field is required.">*</span></span>', | |
237 | transclude: true, | |
238 | link: function (scope, element, attrs, crmUiIdCtrl) { | |
239 | scope.crmIsRequired = false; | |
240 | scope.cssClasses = {}; | |
489c2674 | 241 | |
f8601d61 | 242 | if (!attrs.crmUiFor) return; |
489c2674 | 243 | |
f8601d61 TO |
244 | var id = crmUiIdCtrl.get(attrs.crmUiFor); |
245 | element.attr('for', id); | |
246 | var ngModel = null; | |
489c2674 | 247 | |
f8601d61 TO |
248 | var updateCss = function () { |
249 | scope.cssClasses['crm-error'] = !ngModel.$valid && !ngModel.$pristine; | |
250 | }; | |
489c2674 | 251 | |
f8601d61 TO |
252 | // Note: if target element is dynamically generated (eg via ngInclude), then it may not be available |
253 | // immediately for initialization. Use retries/retryDelay to initialize such elements. | |
254 | var init = function (retries, retryDelay) { | |
255 | var input = $('#' + id); | |
4109d03e | 256 | if (input.length === 0) { |
f8601d61 TO |
257 | if (retries) { |
258 | $timeout(function(){ | |
259 | init(retries-1, retryDelay); | |
260 | }, retryDelay); | |
261 | } | |
262 | return; | |
263 | } | |
489c2674 | 264 | |
f8601d61 TO |
265 | var tgtScope = scope;//.$parent; |
266 | if (attrs.crmDepth) { | |
267 | for (var i = attrs.crmDepth; i > 0; i--) { | |
268 | tgtScope = tgtScope.$parent; | |
269 | } | |
270 | } | |
489c2674 | 271 | |
f8601d61 TO |
272 | if (input.attr('ng-required')) { |
273 | scope.crmIsRequired = scope.$parent.$eval(input.attr('ng-required')); | |
274 | scope.$parent.$watch(input.attr('ng-required'), function (isRequired) { | |
275 | scope.crmIsRequired = isRequired; | |
276 | }); | |
277 | } | |
278 | else { | |
f2bad133 | 279 | scope.crmIsRequired = input.prop('required'); |
f8601d61 | 280 | } |
489c2674 | 281 | |
f8601d61 TO |
282 | ngModel = $parse(attrs.crmUiFor)(tgtScope); |
283 | if (ngModel) { | |
284 | ngModel.$viewChangeListeners.push(updateCss); | |
285 | } | |
286 | }; | |
489c2674 | 287 | |
f8601d61 TO |
288 | $timeout(function(){ |
289 | init(3, 100); | |
489c2674 TO |
290 | }); |
291 | } | |
292 | }; | |
293 | }) | |
294 | ||
d376f33e | 295 | // Define a scope in which a name like "subform.foo" maps to a unique ID. |
f8601d61 TO |
296 | // example: <div ng-form="subform" crm-ui-id-scope><label crm-ui-for="subform.foo">Foo:</label><input crm-ui-id="subform.foo" name="foo"/></div> |
297 | .directive('crmUiIdScope', function () { | |
298 | return { | |
299 | restrict: 'EA', | |
300 | scope: {}, | |
301 | controllerAs: 'crmUiIdCtrl', | |
302 | controller: function($scope) { | |
303 | var ids = {}; | |
304 | this.get = function(name) { | |
305 | if (!ids[name]) { | |
306 | ids[name] = "crmUiId_" + (++uidCount); | |
307 | } | |
308 | return ids[name]; | |
f2bad133 | 309 | }; |
f8601d61 TO |
310 | }, |
311 | link: function (scope, element, attrs) {} | |
312 | }; | |
313 | }) | |
314 | ||
d376f33e | 315 | // Display an HTML blurb inside an IFRAME. |
107c5cc7 TO |
316 | // example: <iframe crm-ui-iframe="getHtmlContent()"></iframe> |
317 | .directive('crmUiIframe', function ($parse) { | |
318 | return { | |
319 | scope: { | |
320 | crmUiIframe: '@' // expression which evalutes to HTML content | |
321 | }, | |
322 | link: function (scope, elm, attrs) { | |
323 | var iframe = $(elm)[0]; | |
324 | iframe.setAttribute('width', '100%'); | |
325 | iframe.setAttribute('frameborder', '0'); | |
326 | ||
327 | var refresh = function () { | |
328 | // var iframeHtml = '<html><head><base target="_blank"></head><body onload="parent.document.getElementById(\'' + iframe.id + '\').style.height=document.body.scrollHeight + \'px\'"><scr' + 'ipt type="text/javascript" src="https://gist.github.com/' + iframeId + '.js"></sc' + 'ript></body></html>'; | |
329 | var iframeHtml = scope.$parent.$eval(attrs.crmUiIframe); | |
330 | ||
331 | var doc = iframe.document; | |
332 | if (iframe.contentDocument) { | |
333 | doc = iframe.contentDocument; | |
334 | } | |
335 | else if (iframe.contentWindow) { | |
336 | doc = iframe.contentWindow.document; | |
337 | } | |
338 | ||
339 | doc.open(); | |
340 | doc.writeln(iframeHtml); | |
341 | doc.close(); | |
f2bad133 | 342 | }; |
107c5cc7 | 343 | |
02cd9764 CW |
344 | // If the iframe is in a dialog, respond to resize events |
345 | $(elm).parent().on('dialogresize dialogopen', function(e, ui) { | |
346 | $(this).css({padding: '0', margin: '0', overflow: 'hidden'}); | |
347 | iframe.setAttribute('height', '' + $(this).innerHeight() + 'px'); | |
348 | }); | |
349 | ||
107c5cc7 | 350 | scope.$parent.$watch(attrs.crmUiIframe, refresh); |
107c5cc7 TO |
351 | } |
352 | }; | |
353 | }) | |
354 | ||
f8f85764 TO |
355 | // Example: |
356 | // <a ng-click="$broadcast('my-insert-target', 'some new text')>Insert</a> | |
357 | // <textarea crm-ui-insert-rx='my-insert-target'></textarea> | |
358 | // TODO Consider ways to separate the plain-text/rich-text implementations | |
359 | .directive('crmUiInsertRx', function() { | |
360 | return { | |
361 | link: function(scope, element, attrs) { | |
362 | scope.$on(attrs.crmUiInsertRx, function(e, tokenName) { | |
363 | var id = element.attr('id'); | |
364 | if (CKEDITOR.instances[id]) { | |
365 | CKEDITOR.instances[id].insertText(tokenName); | |
366 | $(element).select2('close').select2('val', ''); | |
367 | CKEDITOR.instances[id].focus(); | |
368 | } | |
369 | else { | |
370 | var crmForEl = $('#' + id); | |
371 | var origVal = crmForEl.val(); | |
372 | var origPos = crmForEl[0].selectionStart; | |
373 | var newVal = origVal.substring(0, origPos) + tokenName + origVal.substring(origPos, origVal.length); | |
374 | crmForEl.val(newVal); | |
375 | var newPos = (origPos + tokenName.length); | |
376 | crmForEl[0].selectionStart = newPos; | |
377 | crmForEl[0].selectionEnd = newPos; | |
378 | ||
379 | $(element).select2('close').select2('val', ''); | |
380 | crmForEl.triggerHandler('change'); | |
381 | crmForEl.focus(); | |
382 | } | |
383 | }); | |
384 | } | |
385 | }; | |
386 | }) | |
387 | ||
d376f33e | 388 | // Define a rich text editor. |
3cc9c048 TO |
389 | // example: <textarea crm-ui-id="myForm.body_html" crm-ui-richtext name="body_html" ng-model="mailing.body_html"></textarea> |
390 | .directive('crmUiRichtext', function ($timeout) { | |
38737af8 TO |
391 | return { |
392 | require: '?ngModel', | |
393 | link: function (scope, elm, attr, ngModel) { | |
38737af8 TO |
394 | var ck = CKEDITOR.replace(elm[0]); |
395 | ||
65d3e04c TO |
396 | if (ck) { |
397 | _.extend(ck.config, { | |
398 | width: '94%', | |
399 | height: '400', | |
400 | filebrowserBrowseUrl: CRM.crmUi.browseUrl + '?cms=civicrm&type=files', | |
401 | filebrowserImageBrowseUrl: CRM.crmUi.browseUrl + '?cms=civicrm&type=images', | |
402 | filebrowserFlashBrowseUrl: CRM.crmUi.browseUrl + '?cms=civicrm&type=flash', | |
403 | filebrowserUploadUrl: CRM.crmUi.uploadUrl + '?cms=civicrm&type=files', | |
404 | filebrowserImageUploadUrl: CRM.crmUi.uploadUrl + '?cms=civicrm&type=images', | |
405 | filebrowserFlashUploadUrl: CRM.crmUi.uploadUrl + '?cms=civicrm&type=flash', | |
406 | }); | |
407 | } | |
408 | ||
38737af8 TO |
409 | if (!ngModel) { |
410 | return; | |
411 | } | |
412 | ||
07996588 TO |
413 | if (attr.ngBlur) { |
414 | ck.on('blur', function(){ | |
415 | $timeout(function(){ | |
416 | scope.$eval(attr.ngBlur); | |
d03e0d13 | 417 | }); |
07996588 TO |
418 | }); |
419 | } | |
420 | ||
5ced105e TO |
421 | // CRM-16445 - When one inserts an image, none of these events seem to fire at the right time: |
422 | // afterCommandExec, afterInsertHtml, afterPaste, afterSetData, change, insertElement, | |
423 | // insertHtml, insertText, pasteState. It seems that 'pasteState' is the general equivalent of | |
424 | // what 'change' should be, except (in the case of image insertion) it fires too soon. | |
774738e2 TO |
425 | // The 'key' event is needed to detect changes in "Source" mode. |
426 | var debounce = null; | |
427 | angular.forEach(['key', 'pasteState'], function(evName){ | |
428 | ck.on(evName, function(evt) { | |
429 | $timeout.cancel(debounce); | |
430 | debounce = $timeout(function() { | |
431 | ngModel.$setViewValue(ck.getData()); | |
432 | }, 50); | |
433 | }); | |
38737af8 TO |
434 | }); |
435 | ||
436 | ngModel.$render = function (value) { | |
437 | ck.setData(ngModel.$viewValue); | |
438 | }; | |
439 | } | |
440 | }; | |
441 | }) | |
442 | ||
d376f33e | 443 | // Display a lock icon (based on a boolean). |
685acae4 | 444 | // example: <a crm-ui-lock binding="mymodel.boolfield"></a> |
445 | // example: <a crm-ui-lock | |
446 | // binding="mymodel.boolfield" | |
447 | // title-locked="ts('Boolfield is locked')" | |
448 | // title-unlocked="ts('Boolfield is unlocked')"></a> | |
449 | .directive('crmUiLock', function ($parse, $rootScope) { | |
450 | var defaultVal = function (defaultValue) { | |
451 | var f = function (scope) { | |
452 | return defaultValue; | |
f2bad133 | 453 | }; |
685acae4 | 454 | f.assign = function (scope, value) { |
455 | // ignore changes | |
f2bad133 | 456 | }; |
685acae4 | 457 | return f; |
458 | }; | |
459 | ||
460 | // like $parse, but accepts a defaultValue in case expr is undefined | |
461 | var parse = function (expr, defaultValue) { | |
462 | return expr ? $parse(expr) : defaultVal(defaultValue); | |
463 | }; | |
464 | ||
465 | return { | |
466 | template: '', | |
467 | link: function (scope, element, attrs) { | |
f2bad133 TO |
468 | var binding = parse(attrs.binding, true); |
469 | var titleLocked = parse(attrs.titleLocked, ts('Locked')); | |
470 | var titleUnlocked = parse(attrs.titleUnlocked, ts('Unlocked')); | |
685acae4 | 471 | |
472 | $(element).addClass('ui-icon lock-button'); | |
473 | var refresh = function () { | |
474 | var locked = binding(scope); | |
475 | if (locked) { | |
476 | $(element) | |
477 | .removeClass('ui-icon-unlocked') | |
478 | .addClass('ui-icon-locked') | |
479 | .prop('title', titleLocked(scope)) | |
480 | ; | |
481 | } | |
482 | else { | |
483 | $(element) | |
484 | .removeClass('ui-icon-locked') | |
485 | .addClass('ui-icon-unlocked') | |
486 | .prop('title', titleUnlocked(scope)) | |
487 | ; | |
488 | } | |
489 | }; | |
490 | ||
491 | $(element).click(function () { | |
492 | binding.assign(scope, !binding(scope)); | |
493 | //scope.$digest(); | |
494 | $rootScope.$digest(); | |
495 | }); | |
496 | ||
497 | scope.$watch(attrs.binding, refresh); | |
498 | scope.$watch(attrs.titleLocked, refresh); | |
499 | scope.$watch(attrs.titleUnlocked, refresh); | |
500 | ||
501 | refresh(); | |
502 | } | |
503 | }; | |
504 | }) | |
030dce01 | 505 | |
b21c9cdc TO |
506 | // CrmUiOrderCtrl is a controller class which manages sort orderings. |
507 | // Ex: | |
508 | // JS: $scope.myOrder = new CrmUiOrderCtrl(['+field1', '-field2]); | |
509 | // $scope.myOrder.toggle('field1'); | |
510 | // $scope.myOrder.setDir('field2', ''); | |
511 | // HTML: <tr ng-repeat="... | order:myOrder.get()">...</tr> | |
512 | .service('CrmUiOrderCtrl', function(){ | |
513 | // | |
514 | function CrmUiOrderCtrl(defaults){ | |
515 | this.values = defaults; | |
516 | } | |
517 | angular.extend(CrmUiOrderCtrl.prototype, { | |
518 | get: function get() { | |
519 | return this.values; | |
520 | }, | |
521 | getDir: function getDir(name) { | |
522 | if (this.values.indexOf(name) >= 0 || this.values.indexOf('+' + name) >= 0) { | |
523 | return '+'; | |
524 | } | |
525 | if (this.values.indexOf('-' + name) >= 0) { | |
526 | return '-'; | |
527 | } | |
528 | return ''; | |
529 | }, | |
530 | // @return bool TRUE if something is removed | |
531 | remove: function remove(name) { | |
532 | var idx = this.values.indexOf(name); | |
533 | if (idx >= 0) { | |
534 | this.values.splice(idx, 1); | |
535 | return true; | |
536 | } | |
537 | else { | |
538 | return false; | |
539 | } | |
540 | }, | |
541 | setDir: function setDir(name, dir) { | |
542 | return this.toggle(name, dir); | |
543 | }, | |
544 | // Toggle sort order on a field. | |
545 | // To set a specific order, pass optional parameter 'next' ('+', '-', or ''). | |
546 | toggle: function toggle(name, next) { | |
547 | if (!next && next !== '') { | |
548 | next = '+'; | |
549 | if (this.remove(name) || this.remove('+' + name)) { | |
550 | next = '-'; | |
551 | } | |
552 | if (this.remove('-' + name)) { | |
553 | next = ''; | |
554 | } | |
555 | } | |
556 | ||
557 | if (next == '+') { | |
558 | this.values.unshift('+' + name); | |
559 | } | |
560 | else if (next == '-') { | |
561 | this.values.unshift('-' + name); | |
562 | } | |
563 | } | |
564 | }); | |
565 | return CrmUiOrderCtrl; | |
566 | }) | |
567 | ||
568 | // Define a controller which manages sort order. You may interact with the controller | |
569 | // directly ("myOrder.toggle('fieldname')") order using the helper, crm-ui-order-by. | |
570 | // example: | |
571 | // <span crm-ui-order="{var: 'myOrder', defaults: {'-myField'}}"></span> | |
572 | // <th><a crm-ui-order-by="[myOrder,'myField']">My Field</a></th> | |
573 | // <tr ng-repeat="... | order:myOrder.get()">...</tr> | |
574 | // <button ng-click="myOrder.toggle('myField')"> | |
575 | .directive('crmUiOrder', function(CrmUiOrderCtrl) { | |
576 | return { | |
577 | link: function(scope, element, attrs){ | |
578 | var options = angular.extend({var: 'crmUiOrderBy'}, scope.$eval(attrs.crmUiOrder)); | |
579 | scope[options.var] = new CrmUiOrderCtrl(options.defaults); | |
580 | } | |
581 | }; | |
582 | }) | |
583 | ||
584 | // For usage, see crmUiOrder (above) | |
585 | .directive('crmUiOrderBy', function() { | |
586 | return { | |
587 | link: function(scope, element, attrs) { | |
588 | function updateClass(crmUiOrderCtrl, name) { | |
589 | var dir = crmUiOrderCtrl.getDir(name); | |
590 | element | |
591 | .toggleClass('sorting_asc', dir === '+') | |
592 | .toggleClass('sorting_desc', dir === '-') | |
593 | .toggleClass('sorting', dir === ''); | |
594 | } | |
595 | ||
596 | element.on('click', function(e){ | |
597 | var tgt = scope.$eval(attrs.crmUiOrderBy); | |
598 | tgt[0].toggle(tgt[1]); | |
599 | updateClass(tgt[0], tgt[1]); | |
600 | e.preventDefault(); | |
601 | scope.$digest(); | |
602 | }); | |
603 | ||
604 | var tgt = scope.$eval(attrs.crmUiOrderBy); | |
605 | updateClass(tgt[0], tgt[1]); | |
606 | } | |
607 | }; | |
608 | }) | |
609 | ||
d376f33e | 610 | // Display a fancy SELECT (based on select2). |
ebfe3efb TO |
611 | // usage: <select crm-ui-select="{placeholder:'Something',allowClear:true,...}" ng-model="myobj.field"><option...></select> |
612 | .directive('crmUiSelect', function ($parse, $timeout) { | |
8c632f2b | 613 | return { |
ebfe3efb | 614 | require: '?ngModel', |
8c632f2b | 615 | scope: { |
8674b5ac | 616 | crmUiSelect: '=' |
8c632f2b | 617 | }, |
ebfe3efb | 618 | link: function (scope, element, attrs, ngModel) { |
8c632f2b TO |
619 | // In cases where UI initiates update, there may be an extra |
620 | // call to refreshUI, but it doesn't create a cycle. | |
621 | ||
c81696a6 CW |
622 | if (ngModel) { |
623 | ngModel.$render = function () { | |
624 | $timeout(function () { | |
625 | // ex: msg_template_id adds new item then selects it; use $timeout to ensure that | |
626 | // new item is added before selection is made | |
627 | element.select2('val', ngModel.$viewValue); | |
628 | }); | |
629 | }; | |
630 | } | |
8c632f2b | 631 | function refreshModel() { |
50e25421 | 632 | var oldValue = ngModel.$viewValue, newValue = element.select2('val'); |
8c632f2b | 633 | if (oldValue != newValue) { |
ebfe3efb TO |
634 | scope.$parent.$apply(function () { |
635 | ngModel.$setViewValue(newValue); | |
8c632f2b | 636 | }); |
8c632f2b TO |
637 | } |
638 | } | |
ebfe3efb | 639 | |
8c632f2b TO |
640 | function init() { |
641 | // TODO watch select2-options | |
8674b5ac | 642 | element.select2(scope.crmUiSelect || {}); |
c81696a6 CW |
643 | if (ngModel) { |
644 | element.on('change', refreshModel); | |
645 | $timeout(ngModel.$render); | |
646 | } | |
8099bfee CW |
647 | } |
648 | ||
649 | init(); | |
650 | } | |
651 | }; | |
652 | }) | |
653 | ||
654 | // Render a crmEntityRef widget | |
655 | // usage: <input crm-entityref="{entity: 'Contact', select: {allowClear:true}}" ng-model="myobj.field" /> | |
656 | .directive('crmEntityref', function ($parse, $timeout) { | |
657 | return { | |
658 | require: '?ngModel', | |
659 | scope: { | |
660 | crmEntityref: '=' | |
661 | }, | |
662 | link: function (scope, element, attrs, ngModel) { | |
663 | // In cases where UI initiates update, there may be an extra | |
664 | // call to refreshUI, but it doesn't create a cycle. | |
665 | ||
666 | ngModel.$render = function () { | |
667 | $timeout(function () { | |
668 | // ex: msg_template_id adds new item then selects it; use $timeout to ensure that | |
669 | // new item is added before selection is made | |
670 | element.select2('val', ngModel.$viewValue); | |
671 | }); | |
672 | }; | |
673 | function refreshModel() { | |
674 | var oldValue = ngModel.$viewValue, newValue = element.select2('val'); | |
675 | if (oldValue != newValue) { | |
676 | scope.$parent.$apply(function () { | |
677 | ngModel.$setViewValue(newValue); | |
678 | }); | |
679 | } | |
680 | } | |
681 | ||
682 | function init() { | |
8099bfee CW |
683 | // TODO can we infer "entity" from model? |
684 | element.crmEntityRef(scope.crmEntityref || {}); | |
685 | element.on('change', refreshModel); | |
686 | $timeout(ngModel.$render); | |
8c632f2b | 687 | } |
ebfe3efb | 688 | |
8c632f2b TO |
689 | init(); |
690 | } | |
691 | }; | |
692 | }) | |
693 | ||
030dce01 | 694 | // example <div crm-ui-tab crm-title="ts('My Title')">...content...</div> |
5f3568fd | 695 | // WISHLIST: use a full Angular component instead of an incomplete jQuery wrapper |
030dce01 TO |
696 | .directive('crmUiTab', function($parse) { |
697 | return { | |
5f3568fd TO |
698 | require: '^crmUiTabSet', |
699 | restrict: 'EA', | |
030dce01 | 700 | scope: { |
5f3568fd TO |
701 | crmTitle: '@', |
702 | id: '@' | |
030dce01 | 703 | }, |
5f3568fd | 704 | template: '<div ng-transclude></div>', |
030dce01 | 705 | transclude: true, |
5f3568fd TO |
706 | link: function (scope, element, attrs, crmUiTabSetCtrl) { |
707 | crmUiTabSetCtrl.add(scope); | |
708 | } | |
030dce01 TO |
709 | }; |
710 | }) | |
711 | ||
712 | // example: <div crm-ui-tab-set><div crm-ui-tab crm-title="Tab 1">...</div><div crm-ui-tab crm-title="Tab 2">...</div></div> | |
713 | .directive('crmUiTabSet', function() { | |
714 | return { | |
5f3568fd TO |
715 | restrict: 'EA', |
716 | scope: { | |
717 | crmUiTabSet: '@' | |
718 | }, | |
ef5d18a1 | 719 | templateUrl: '~/crmUi/tabset.html', |
030dce01 | 720 | transclude: true, |
5f3568fd TO |
721 | controllerAs: 'crmUiTabSetCtrl', |
722 | controller: function($scope, $parse) { | |
723 | var tabs = $scope.tabs = []; // array<$scope> | |
724 | this.add = function(tab) { | |
725 | if (!tab.id) throw "Tab is missing 'id'"; | |
726 | tabs.push(tab); | |
727 | }; | |
728 | }, | |
030dce01 TO |
729 | link: function (scope, element, attrs) {} |
730 | }; | |
731 | }) | |
732 | ||
d376f33e | 733 | // Generic, field-independent form validator. |
3afb86ef TO |
734 | // example: <span ng-model="placeholder" crm-ui-validate="foo && bar || whiz" /> |
735 | // example: <span ng-model="placeholder" crm-ui-validate="foo && bar || whiz" crm-ui-validate-name="myError" /> | |
3afb86ef TO |
736 | .directive('crmUiValidate', function() { |
737 | return { | |
738 | restrict: 'EA', | |
739 | require: 'ngModel', | |
740 | link: function(scope, element, attrs, ngModel) { | |
741 | var validationKey = attrs.crmUiValidateName ? attrs.crmUiValidateName : 'crmUiValidate'; | |
742 | scope.$watch(attrs.crmUiValidate, function(newValue){ | |
743 | ngModel.$setValidity(validationKey, !!newValue); | |
744 | }); | |
745 | } | |
746 | }; | |
747 | }) | |
748 | ||
e84a11d8 TO |
749 | // like ng-show, but hides/displays elements using "visibility" which maintains positioning |
750 | // example <div crm-ui-visible="false">...content...</div> | |
751 | .directive('crmUiVisible', function($parse) { | |
752 | return { | |
753 | restrict: 'EA', | |
754 | scope: { | |
755 | crmUiVisible: '@' | |
756 | }, | |
757 | link: function (scope, element, attrs) { | |
758 | var model = $parse(attrs.crmUiVisible); | |
759 | function updatecChildren() { | |
760 | element.css('visibility', model(scope.$parent) ? 'inherit' : 'hidden'); | |
761 | } | |
762 | updatecChildren(); | |
763 | scope.$parent.$watch(attrs.crmUiVisible, updatecChildren); | |
764 | } | |
765 | }; | |
766 | }) | |
767 | ||
6717e4b9 TO |
768 | // example: <div crm-ui-wizard="myWizardCtrl"><div crm-ui-wizard-step crm-title="ts('Step 1')">...</div><div crm-ui-wizard-step crm-title="ts('Step 2')">...</div></div> |
769 | // Note: "myWizardCtrl" has various actions/properties like next() and $first(). | |
770 | // WISHLIST: Allow each step to determine if it is "complete" / "valid" / "selectable" | |
771 | // WISHLIST: Allow each step to enable/disable (show/hide) itself | |
030dce01 TO |
772 | .directive('crmUiWizard', function() { |
773 | return { | |
6717e4b9 TO |
774 | restrict: 'EA', |
775 | scope: { | |
776 | crmUiWizard: '@' | |
777 | }, | |
ef5d18a1 | 778 | templateUrl: '~/crmUi/wizard.html', |
030dce01 | 779 | transclude: true, |
6717e4b9 TO |
780 | controllerAs: 'crmUiWizardCtrl', |
781 | controller: function($scope, $parse) { | |
782 | var steps = $scope.steps = []; // array<$scope> | |
783 | var crmUiWizardCtrl = this; | |
784 | var maxVisited = 0; | |
785 | var selectedIndex = null; | |
786 | ||
787 | var findIndex = function() { | |
788 | var found = null; | |
789 | angular.forEach(steps, function(step, stepKey) { | |
790 | if (step.selected) found = stepKey; | |
791 | }); | |
792 | return found; | |
793 | }; | |
794 | ||
795 | /// @return int the index of the current step | |
796 | this.$index = function() { return selectedIndex; }; | |
797 | /// @return bool whether the currentstep is first | |
798 | this.$first = function() { return this.$index() === 0; }; | |
799 | /// @return bool whether the current step is last | |
800 | this.$last = function() { return this.$index() === steps.length -1; }; | |
f2bad133 | 801 | this.$maxVisit = function() { return maxVisited; }; |
3f0da451 | 802 | this.$validStep = function() { |
f8c05c87 | 803 | return steps[selectedIndex] && steps[selectedIndex].isStepValid(); |
3f0da451 | 804 | }; |
6717e4b9 TO |
805 | this.iconFor = function(index) { |
806 | if (index < this.$index()) return '√'; | |
807 | if (index === this.$index()) return '»'; | |
808 | return ' '; | |
f2bad133 | 809 | }; |
6717e4b9 TO |
810 | this.isSelectable = function(step) { |
811 | if (step.selected) return false; | |
812 | var result = false; | |
813 | angular.forEach(steps, function(otherStep, otherKey) { | |
814 | if (step === otherStep && otherKey <= maxVisited) result = true; | |
815 | }); | |
816 | return result; | |
817 | }; | |
818 | ||
819 | /*** @param Object step the $scope of the step */ | |
820 | this.select = function(step) { | |
821 | angular.forEach(steps, function(otherStep, otherKey) { | |
822 | otherStep.selected = (otherStep === step); | |
823 | if (otherStep === step && maxVisited < otherKey) maxVisited = otherKey; | |
824 | }); | |
825 | selectedIndex = findIndex(); | |
826 | }; | |
827 | /*** @param Object step the $scope of the step */ | |
828 | this.add = function(step) { | |
829 | if (steps.length === 0) { | |
830 | step.selected = true; | |
831 | selectedIndex = 0; | |
832 | } | |
833 | steps.push(step); | |
8688b463 TO |
834 | steps.sort(function(a,b){ |
835 | return a.crmUiWizardStep - b.crmUiWizardStep; | |
836 | }); | |
837 | selectedIndex = findIndex(); | |
838 | }; | |
839 | this.remove = function(step) { | |
840 | var key = null; | |
841 | angular.forEach(steps, function(otherStep, otherKey) { | |
842 | if (otherStep === step) key = otherKey; | |
843 | }); | |
4109d03e | 844 | if (key !== null) { |
8688b463 TO |
845 | steps.splice(key, 1); |
846 | } | |
6717e4b9 TO |
847 | }; |
848 | this.goto = function(index) { | |
849 | if (index < 0) index = 0; | |
850 | if (index >= steps.length) index = steps.length-1; | |
851 | this.select(steps[index]); | |
852 | }; | |
853 | this.previous = function() { this.goto(this.$index()-1); }; | |
854 | this.next = function() { this.goto(this.$index()+1); }; | |
855 | if ($scope.crmUiWizard) { | |
f2bad133 | 856 | $parse($scope.crmUiWizard).assign($scope.$parent, this); |
6717e4b9 TO |
857 | } |
858 | }, | |
c4991710 TO |
859 | link: function (scope, element, attrs) { |
860 | scope.ts = CRM.ts(null); | |
861 | } | |
030dce01 TO |
862 | }; |
863 | }) | |
864 | ||
6717e4b9 TO |
865 | // Use this to add extra markup to wizard |
866 | .directive('crmUiWizardButtons', function() { | |
030dce01 | 867 | return { |
6717e4b9 TO |
868 | require: '^crmUiWizard', |
869 | restrict: 'EA', | |
870 | scope: {}, | |
871 | template: '<span ng-transclude></span>', | |
030dce01 | 872 | transclude: true, |
6717e4b9 TO |
873 | link: function (scope, element, attrs, crmUiWizardCtrl) { |
874 | var realButtonsEl = $(element).closest('.crm-wizard').find('.crm-wizard-buttons'); | |
875 | $(element).appendTo(realButtonsEl); | |
876 | } | |
030dce01 TO |
877 | }; |
878 | }) | |
879 | ||
39f4f54f CW |
880 | // Example: <button crm-icon="check">Save</button> |
881 | .directive('crmIcon', function() { | |
882 | return { | |
883 | restrict: 'EA', | |
884 | scope: {}, | |
885 | link: function (scope, element, attrs) { | |
886 | $(element).prepend('<span class="icon ui-icon-' + attrs.crmIcon + '"></span> '); | |
887 | if ($(element).is('button')) { | |
888 | $(element).addClass('crm-button'); | |
889 | } | |
890 | } | |
891 | }; | |
892 | }) | |
893 | ||
3f0da451 | 894 | // example: <div crm-ui-wizard-step crm-title="ts('My Title')" ng-form="mySubForm">...content...</div> |
8688b463 TO |
895 | // If there are any conditional steps, then be sure to set a weight explicitly on *all* steps to maintain ordering. |
896 | // example: <div crm-ui-wizard-step="100" crm-title="..." ng-if="...">...content...</div> | |
030dce01 | 897 | .directive('crmUiWizardStep', function() { |
8688b463 | 898 | var nextWeight = 1; |
030dce01 | 899 | return { |
3f0da451 | 900 | require: ['^crmUiWizard', 'form'], |
6717e4b9 | 901 | restrict: 'EA', |
030dce01 | 902 | scope: { |
8688b463 TO |
903 | crmTitle: '@', // expression, evaluates to a printable string |
904 | crmUiWizardStep: '@' // int, a weight which determines the ordering of the steps | |
030dce01 | 905 | }, |
6717e4b9 | 906 | template: '<div class="crm-wizard-step" ng-show="selected" ng-transclude/></div>', |
030dce01 | 907 | transclude: true, |
3f0da451 TO |
908 | link: function (scope, element, attrs, ctrls) { |
909 | var crmUiWizardCtrl = ctrls[0], form = ctrls[1]; | |
8688b463 TO |
910 | if (scope.crmUiWizardStep) { |
911 | scope.crmUiWizardStep = parseInt(scope.crmUiWizardStep); | |
912 | } else { | |
913 | scope.crmUiWizardStep = nextWeight++; | |
914 | } | |
3f0da451 TO |
915 | scope.isStepValid = function() { |
916 | return form.$valid; | |
917 | }; | |
6717e4b9 | 918 | crmUiWizardCtrl.add(scope); |
8688b463 TO |
919 | element.on('$destroy', function(){ |
920 | crmUiWizardCtrl.remove(scope); | |
921 | }); | |
6717e4b9 | 922 | } |
030dce01 TO |
923 | }; |
924 | }) | |
925 | ||
5fb5b3cf | 926 | // Example: <button crm-confirm="{message: ts('Are you sure you want to continue?')}" on-yes="frobnicate(123)">Frobincate</button> |
4b8c8b42 | 927 | // Example: <button crm-confirm="{type: 'disable', obj: myObject}" on-yes="myObject.is_active=0; myObject.save()">Disable</button> |
6a6d0f41 TO |
928 | // Example: <button crm-confirm="{templateUrl: '~/path/to/view.html', export: {foo: bar}}" on-yes="frobnicate(123)">Frobincate</button> |
929 | .directive('crmConfirm', function ($compile, $rootScope, $templateRequest, $q) { | |
88fcc9f1 | 930 | // Helpers to calculate default options for CRM.confirm() |
4b8c8b42 TO |
931 | var defaultFuncs = { |
932 | 'disable': function (options) { | |
933 | return { | |
934 | message: ts('Are you sure you want to disable this?'), | |
935 | options: {no: ts('Cancel'), yes: ts('Disable')}, | |
936 | width: 300, | |
937 | title: ts('Disable %1?', { | |
938 | 1: options.obj.title || options.obj.label || options.obj.name || ts('the record') | |
939 | }) | |
940 | }; | |
941 | }, | |
470a458e TO |
942 | 'revert': function (options) { |
943 | return { | |
944 | message: ts('Are you sure you want to revert this?'), | |
945 | options: {no: ts('Cancel'), yes: ts('Revert')}, | |
946 | width: 300, | |
947 | title: ts('Revert %1?', { | |
948 | 1: options.obj.title || options.obj.label || options.obj.name || ts('the record') | |
949 | }) | |
950 | }; | |
951 | }, | |
4b8c8b42 TO |
952 | 'delete': function (options) { |
953 | return { | |
954 | message: ts('Are you sure you want to delete this?'), | |
955 | options: {no: ts('Cancel'), yes: ts('Delete')}, | |
956 | width: 300, | |
957 | title: ts('Delete %1?', { | |
958 | 1: options.obj.title || options.obj.label || options.obj.name || ts('the record') | |
959 | }) | |
960 | }; | |
961 | } | |
962 | }; | |
6a6d0f41 | 963 | var confirmCount = 0; |
4b8c8b42 | 964 | return { |
4b8c8b42 TO |
965 | link: function (scope, element, attrs) { |
966 | $(element).click(function () { | |
f2bad133 | 967 | var options = scope.$eval(attrs.crmConfirm); |
b8603a6d CW |
968 | if (attrs.title && !options.title) { |
969 | options.title = attrs.title; | |
970 | } | |
4b8c8b42 | 971 | var defaults = (options.type) ? defaultFuncs[options.type](options) : {}; |
6a6d0f41 TO |
972 | |
973 | var tpl = null, stubId = null; | |
974 | if (!options.message) { | |
975 | if (options.templateUrl) { | |
976 | tpl = $templateRequest(options.templateUrl); | |
977 | } | |
978 | else if (options.template) { | |
979 | tpl = options.template; | |
980 | } | |
981 | if (tpl) { | |
982 | stubId = 'crmUiConfirm_' + (++confirmCount); | |
983 | options.message = '<div id="' + stubId + '"></div>'; | |
984 | } | |
985 | } | |
986 | ||
4b8c8b42 | 987 | CRM.confirm(_.extend(defaults, options)) |
6a6d0f41 TO |
988 | .on('crmConfirm:yes', function() { scope.$apply(attrs.onYes); }) |
989 | .on('crmConfirm:no', function() { scope.$apply(attrs.onNo); }); | |
990 | ||
991 | if (tpl && stubId) { | |
992 | $q.when(tpl, function(html) { | |
993 | var scope = options.scope || $rootScope.$new(); | |
994 | if (options.export) { | |
995 | angular.extend(scope, options.export); | |
996 | } | |
997 | var linker = $compile(html); | |
998 | $('#' + stubId).append($(linker(scope))); | |
999 | }); | |
1000 | } | |
4b8c8b42 TO |
1001 | }); |
1002 | } | |
1003 | }; | |
1004 | }) | |
02308c07 TO |
1005 | .run(function($rootScope, $location) { |
1006 | /// Example: <button ng-click="goto('home')">Go home!</button> | |
1007 | $rootScope.goto = function(path) { | |
1008 | $location.path(path); | |
1009 | }; | |
4b8c8b42 | 1010 | // useful for debugging: $rootScope.log = console.log || function() {}; |
02308c07 | 1011 | }) |
685acae4 | 1012 | ; |
1013 | ||
5fb5b3cf | 1014 | })(angular, CRM.$, CRM._); |