6a54b58b7bf8d39ba23baee0dac7992c5631227f
1 /// crmUi: Sundry UI helpers
2 (function (angular
, $, _
) {
6 documentTitle
= 'CiviCRM';
8 angular
.module('crmUi', CRM
.angRequires('crmUi'))
10 // example <div crm-ui-accordion crm-title="ts('My Title')" crm-collapsed="true">...content...</div>
11 // WISHLIST: crmCollapsed should support two-way/continuous binding
12 .directive('crmUiAccordion', function() {
17 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>',
19 link: function (scope
, element
, attrs
) {
21 'crm-accordion-wrapper': true,
22 collapsed
: scope
.crmUiAccordion
.collapsed
25 scope
.$watch('crmUiAccordion', function(crmUiAccordion
) {
26 if (crmUiAccordion
&& crmUiAccordion
.help
) {
27 scope
.help
= crmUiAccordion
.help
.clone({}, {
28 title
: crmUiAccordion
.title
37 // crmUiAlert({text: 'My text', title: 'My title', type: 'error'});
38 // crmUiAlert({template: '<a ng-click="ok()">Hello</a>', scope: $scope.$new()});
39 // var h = crmUiAlert({templateUrl: '~/crmFoo/alert.html', scope: $scope.$new()});
41 .service('crmUiAlert', function($compile
, $rootScope
, $templateRequest
, $q
) {
43 return function crmUiAlert(params
) {
44 var id
= 'crmUiAlert_' + (++count
);
46 if (params
.templateUrl
) {
47 tpl
= $templateRequest(params
.templateUrl
);
49 else if (params
.template
) {
50 tpl
= params
.template
;
53 params
.text
= '<div id="' + id
+ '"></div>'; // temporary stub
55 var result
= CRM
.alert(params
.text
, params
.title
, params
.type
, params
.options
);
57 $q
.when(tpl
, function(html
) {
58 var scope
= params
.scope
|| $rootScope
.$new();
59 var linker
= $compile(html
);
60 $('#' + id
).append($(linker(scope
)));
67 // Simple wrapper around $.crmDatepicker.
68 // example with no time input: <input crm-ui-datepicker="{time: false}" ng-model="myobj.datefield"/>
69 // example with custom date format: <input crm-ui-datepicker="{date: 'm/d/y'}" ng-model="myobj.datefield"/>
70 .directive('crmUiDatepicker', function () {
77 link: function (scope
, element
, attrs
, ngModel
) {
78 ngModel
.$render = function () {
79 element
.val(ngModel
.$viewValue
).change();
83 .crmDatepicker(scope
.crmUiDatepicker
)
84 .on('change', function() {
85 var requiredLength
= 19;
86 if (scope
.crmUiDatepicker
&& scope
.crmUiDatepicker
.time
=== false) {
89 if (scope
.crmUiDatepicker
&& scope
.crmUiDatepicker
.date
=== false) {
92 ngModel
.$setValidity('incompleteDateTime', !($(this).val().length
&& $(this).val().length
!== requiredLength
));
98 // Display debug information (if available)
99 // For richer DX, checkout Batarang/ng-inspector (Chrome/Safari), or AngScope/ng-inspect (Firefox).
100 // example: <div crm-ui-debug="myobject" />
101 .directive('crmUiDebug', function ($location
) {
107 template: function() {
108 var args
= $location
.search();
109 return (args
&& args
.angularDebug
) ? '<div crm-ui-accordion=\'{title: ts("Debug (%1)", {1: crmUiDebug}), collapsed: true}\'><pre>{{data|json}}</pre></div>' : '';
111 link: function(scope
, element
, attrs
) {
112 var args
= $location
.search();
113 if (args
&& args
.angularDebug
) {
114 scope
.ts
= CRM
.ts(null);
115 scope
.$parent
.$watch(attrs
.crmUiDebug
, function(data
) {
123 // Display a field/row in a field list
124 // example: <div crm-ui-field="{title: ts('My Field')}"> {{mydata}} </div>
125 // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field')}"> <input crm-ui-id="subform.myfield" name="myfield" /> </div>
126 // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field')}"> <input crm-ui-id="subform.myfield" name="myfield" required /> </div>
127 // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field'), help: hs('help_field_name')}"> {{mydata}} </div>
128 .directive('crmUiField', function() {
129 // Note: When writing new templates, the "label" position is particular. See/patch "var label" below.
131 default: '~/crmUi/field.html',
132 checkbox
: '~/crmUi/field-cb.html'
136 require
: '^crmUiIdScope',
139 // {title, name, help, helpFile}
142 templateUrl: function(tElement
, tAttrs
){
143 var layout
= tAttrs
.crmLayout
? tAttrs
.crmLayout
: 'default';
144 return templateUrls
[layout
];
147 link: function (scope
, element
, attrs
, crmUiIdCtrl
) {
148 $(element
).addClass('crm-section');
150 scope
.$watch('crmUiField', function(crmUiField
) {
151 if (crmUiField
&& crmUiField
.help
) {
152 scope
.help
= crmUiField
.help
.clone({}, {
153 title
: crmUiField
.title
161 // 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>
162 .directive('crmUiId', function () {
164 require
: '^crmUiIdScope',
167 pre: function (scope
, element
, attrs
, crmUiIdCtrl
) {
168 var id
= crmUiIdCtrl
.get(attrs
.crmUiId
);
169 element
.attr('id', id
);
175 // for example, see crmUiHelp
176 .service('crmUiHelp', function(){
177 // example: var h = new FieldHelp({id: 'foo'}); h.open();
178 function FieldHelp(options
) {
179 this.options
= options
;
181 angular
.extend(FieldHelp
.prototype, {
183 return this.options
[n
];
185 open
: function open() {
186 CRM
.help(this.options
.title
, {id
: this.options
.id
, file
: this.options
.file
});
188 clone
: function clone(options
, defaults
) {
189 return new FieldHelp(angular
.extend({}, defaults
, this.options
, options
));
193 // example: var hs = crmUiHelp({file: 'CRM/Foo/Bar'});
194 return function(defaults
){
195 // example: hs('myfield')
196 // example: hs({id: 'myfield', title: 'Foo Bar', file: 'Whiz/Bang'})
197 return function(options
) {
198 if (_
.isString(options
)) {
199 options
= {id
: options
};
201 return new FieldHelp(angular
.extend({}, defaults
, options
));
206 // Display a help icon
207 // Example: Use a default *.hlp file
208 // scope.hs = crmUiHelp({file: 'Path/To/Help/File'});
209 // HTML: <a crm-ui-help="hs({title:ts('My Field'), id:'my_field'})">
210 // Example: Use an explicit *.hlp file
211 // HTML: <a crm-ui-help="hs({title:ts('My Field'), id:'my_field', file:'CRM/Foo/Bar'})">
212 .directive('crmUiHelp', function() {
215 link: function(scope
, element
, attrs
) {
216 setTimeout(function() {
217 var crmUiHelp
= scope
.$eval(attrs
.crmUiHelp
);
218 var title
= crmUiHelp
&& crmUiHelp
.get('title') ? ts('%1 Help', {1: crmUiHelp
.get('title')}) : ts('Help');
219 element
.attr('title', title
);
223 .addClass('helpicon')
225 .on('click', function(e
) {
227 scope
.$eval(attrs
.crmUiHelp
).open();
233 // 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>
234 .directive('crmUiFor', function ($parse
, $timeout
) {
236 require
: '^crmUiIdScope',
238 template
: '<span ng-class="cssClasses"><span ng-transclude/><span crm-ui-visible="crmIsRequired" class="crm-marker" title="This field is required.">*</span></span>',
240 link: function (scope
, element
, attrs
, crmUiIdCtrl
) {
241 scope
.crmIsRequired
= false;
242 scope
.cssClasses
= {};
244 if (!attrs
.crmUiFor
) return;
246 var id
= crmUiIdCtrl
.get(attrs
.crmUiFor
);
247 element
.attr('for', id
);
250 var updateCss = function () {
251 scope
.cssClasses
['crm-error'] = !ngModel
.$valid
&& !ngModel
.$pristine
;
254 // Note: if target element is dynamically generated (eg via ngInclude), then it may not be available
255 // immediately for initialization. Use retries/retryDelay to initialize such elements.
256 var init = function (retries
, retryDelay
) {
257 var input
= $('#' + id
);
258 if (input
.length
=== 0) {
261 init(retries
-1, retryDelay
);
267 var tgtScope
= scope
;//.$parent;
268 if (attrs
.crmDepth
) {
269 for (var i
= attrs
.crmDepth
; i
> 0; i
--) {
270 tgtScope
= tgtScope
.$parent
;
274 if (input
.attr('ng-required')) {
275 scope
.crmIsRequired
= scope
.$parent
.$eval(input
.attr('ng-required'));
276 scope
.$parent
.$watch(input
.attr('ng-required'), function (isRequired
) {
277 scope
.crmIsRequired
= isRequired
;
281 scope
.crmIsRequired
= input
.prop('required');
284 ngModel
= $parse(attrs
.crmUiFor
)(tgtScope
);
286 ngModel
.$viewChangeListeners
.push(updateCss
);
297 // Define a scope in which a name like "subform.foo" maps to a unique ID.
298 // 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>
299 .directive('crmUiIdScope', function () {
303 controllerAs
: 'crmUiIdCtrl',
304 controller: function($scope
) {
306 this.get = function(name
) {
308 ids
[name
] = "crmUiId_" + (++uidCount
);
313 link: function (scope
, element
, attrs
) {}
317 // Display an HTML blurb inside an IFRAME.
318 // example: <iframe crm-ui-iframe="getHtmlContent()"></iframe>
319 // example: <iframe crm-ui-iframe crm-ui-iframe-src="getUrl()"></iframe>
320 .directive('crmUiIframe', function ($parse
) {
323 crmUiIframeSrc
: '@', // expression which evaluates to a URL
324 crmUiIframe
: '@' // expression which evaluates to HTML content
326 link: function (scope
, elm
, attrs
) {
327 var iframe
= $(elm
)[0];
328 iframe
.setAttribute('width', '100%');
329 iframe
.setAttribute('height', '250px');
330 iframe
.setAttribute('frameborder', '0');
332 var refresh = function () {
333 if (attrs
.crmUiIframeSrc
) {
334 iframe
.setAttribute('src', scope
.$parent
.$eval(attrs
.crmUiIframeSrc
));
337 var iframeHtml
= scope
.$parent
.$eval(attrs
.crmUiIframe
);
339 var doc
= iframe
.document
;
340 if (iframe
.contentDocument
) {
341 doc
= iframe
.contentDocument
;
343 else if (iframe
.contentWindow
) {
344 doc
= iframe
.contentWindow
.document
;
348 doc
.writeln(iframeHtml
);
353 // If the iframe is in a dialog, respond to resize events
354 $(elm
).parent().on('dialogresize dialogopen', function(e
, ui
) {
355 $(this).css({padding
: '0', margin
: '0', overflow
: 'hidden'});
356 iframe
.setAttribute('height', '' + $(this).innerHeight() + 'px');
359 $(elm
).parent().on('dialogresize', function(e
, ui
) {
360 iframe
.setAttribute('class', 'resized');
363 scope
.$parent
.$watch(attrs
.crmUiIframe
, refresh
);
369 // <a ng-click="$broadcast('my-insert-target', 'some new text')>Insert</a>
370 // <textarea crm-ui-insert-rx='my-insert-target'></textarea>
371 .directive('crmUiInsertRx', function() {
373 link: function(scope
, element
, attrs
) {
374 scope
.$on(attrs
.crmUiInsertRx
, function(e
, tokenName
) {
375 CRM
.wysiwyg
.insert(element
, tokenName
);
376 $(element
).select2('close').select2('val', '');
377 CRM
.wysiwyg
.focus(element
);
383 // Define a rich text editor.
384 // example: <textarea crm-ui-id="myForm.body_html" crm-ui-richtext name="body_html" ng-model="mailing.body_html"></textarea>
385 .directive('crmUiRichtext', function ($timeout
) {
388 link: function (scope
, elm
, attr
, ngModel
) {
390 var editor
= CRM
.wysiwyg
.create(elm
);
396 $(elm
).on('blur', function() {
397 $timeout(function() {
398 scope
.$eval(attr
.ngBlur
);
403 ngModel
.$render = function(value
) {
404 editor
.done(function() {
405 CRM
.wysiwyg
.setVal(elm
, ngModel
.$viewValue
|| '');
412 // Display a lock icon (based on a boolean).
413 // example: <a crm-ui-lock binding="mymodel.boolfield"></a>
414 // example: <a crm-ui-lock
415 // binding="mymodel.boolfield"
416 // title-locked="ts('Boolfield is locked')"
417 // title-unlocked="ts('Boolfield is unlocked')"></a>
418 .directive('crmUiLock', function ($parse
, $rootScope
) {
419 var defaultVal = function (defaultValue
) {
420 var f = function (scope
) {
423 f
.assign = function (scope
, value
) {
429 // like $parse, but accepts a defaultValue in case expr is undefined
430 var parse = function (expr
, defaultValue
) {
431 return expr
? $parse(expr
) : defaultVal(defaultValue
);
436 link: function (scope
, element
, attrs
) {
437 var binding
= parse(attrs
.binding
, true);
438 var titleLocked
= parse(attrs
.titleLocked
, ts('Locked'));
439 var titleUnlocked
= parse(attrs
.titleUnlocked
, ts('Unlocked'));
441 $(element
).addClass('crm-i lock-button');
442 var refresh = function () {
443 var locked
= binding(scope
);
446 .removeClass('fa-unlock')
448 .prop('title', titleLocked(scope
))
453 .removeClass('fa-lock')
454 .addClass('fa-unlock')
455 .prop('title', titleUnlocked(scope
))
460 $(element
).click(function () {
461 binding
.assign(scope
, !binding(scope
));
463 $rootScope
.$digest();
466 scope
.$watch(attrs
.binding
, refresh
);
467 scope
.$watch(attrs
.titleLocked
, refresh
);
468 scope
.$watch(attrs
.titleUnlocked
, refresh
);
475 // CrmUiOrderCtrl is a controller class which manages sort orderings.
477 // JS: $scope.myOrder = new CrmUiOrderCtrl(['+field1', '-field2]);
478 // $scope.myOrder.toggle('field1');
479 // $scope.myOrder.setDir('field2', '');
480 // HTML: <tr ng-repeat="... | order:myOrder.get()">...</tr>
481 .service('CrmUiOrderCtrl', function(){
483 function CrmUiOrderCtrl(defaults
){
484 this.values
= defaults
;
486 angular
.extend(CrmUiOrderCtrl
.prototype, {
487 get: function get() {
490 getDir
: function getDir(name
) {
491 if (this.values
.indexOf(name
) >= 0 || this.values
.indexOf('+' + name
) >= 0) {
494 if (this.values
.indexOf('-' + name
) >= 0) {
499 // @return bool TRUE if something is removed
500 remove
: function remove(name
) {
501 var idx
= this.values
.indexOf(name
);
503 this.values
.splice(idx
, 1);
510 setDir
: function setDir(name
, dir
) {
511 return this.toggle(name
, dir
);
513 // Toggle sort order on a field.
514 // To set a specific order, pass optional parameter 'next' ('+', '-', or '').
515 toggle
: function toggle(name
, next
) {
516 if (!next
&& next
!== '') {
518 if (this.remove(name
) || this.remove('+' + name
)) {
521 if (this.remove('-' + name
)) {
527 this.values
.unshift('+' + name
);
529 else if (next
== '-') {
530 this.values
.unshift('-' + name
);
534 return CrmUiOrderCtrl
;
537 // Define a controller which manages sort order. You may interact with the controller
538 // directly ("myOrder.toggle('fieldname')") order using the helper, crm-ui-order-by.
540 // <span crm-ui-order="{var: 'myOrder', defaults: {'-myField'}}"></span>
541 // <th><a crm-ui-order-by="[myOrder,'myField']">My Field</a></th>
542 // <tr ng-repeat="... | order:myOrder.get()">...</tr>
543 // <button ng-click="myOrder.toggle('myField')">
544 .directive('crmUiOrder', function(CrmUiOrderCtrl
) {
546 link: function(scope
, element
, attrs
){
547 var options
= angular
.extend({var: 'crmUiOrderBy'}, scope
.$eval(attrs
.crmUiOrder
));
548 scope
[options
.var] = new CrmUiOrderCtrl(options
.defaults
);
553 // For usage, see crmUiOrder (above)
554 .directive('crmUiOrderBy', function() {
556 link: function(scope
, element
, attrs
) {
557 function updateClass(crmUiOrderCtrl
, name
) {
558 var dir
= crmUiOrderCtrl
.getDir(name
);
560 .toggleClass('sorting_asc', dir
=== '+')
561 .toggleClass('sorting_desc', dir
=== '-')
562 .toggleClass('sorting', dir
=== '');
565 element
.on('click', function(e
){
566 var tgt
= scope
.$eval(attrs
.crmUiOrderBy
);
567 tgt
[0].toggle(tgt
[1]);
568 updateClass(tgt
[0], tgt
[1]);
573 var tgt
= scope
.$eval(attrs
.crmUiOrderBy
);
574 updateClass(tgt
[0], tgt
[1]);
579 // Display a fancy SELECT (based on select2).
580 // usage: <select crm-ui-select="{placeholder:'Something',allowClear:true,...}" ng-model="myobj.field"><option...></select>
581 .directive('crmUiSelect', function ($parse
, $timeout
) {
588 link: function (scope
, element
, attrs
, ngModel
) {
589 // In cases where UI initiates update, there may be an extra
590 // call to refreshUI, but it doesn't create a cycle.
593 ngModel
.$render = function () {
594 $timeout(function () {
595 // ex: msg_template_id adds new item then selects it; use $timeout to ensure that
596 // new item is added before selection is made
597 var newVal
= _
.cloneDeep(ngModel
.$modelValue
);
598 // Fix possible data-type mismatch
599 if (typeof newVal
=== 'string' && element
.select2('container').hasClass('select2-container-multi')) {
600 newVal
= newVal
.length
? newVal
.split(',') : [];
602 element
.select2('val', newVal
);
606 function refreshModel() {
607 var oldValue
= ngModel
.$viewValue
, newValue
= element
.select2('val');
608 if (oldValue
!= newValue
) {
609 scope
.$parent
.$apply(function () {
610 ngModel
.$setViewValue(newValue
);
616 // TODO watch select2-options
617 element
.crmSelect2(scope
.crmUiSelect
|| {});
619 element
.on('change', refreshModel
);
620 $timeout(ngModel
.$render
);
629 // Render a crmEntityRef widget
630 // usage: <input crm-entityref="{entity: 'Contact', select: {allowClear:true}}" ng-model="myobj.field" />
631 .directive('crmEntityref', function ($parse
, $timeout
) {
637 link: function (scope
, element
, attrs
, ngModel
) {
638 // In cases where UI initiates update, there may be an extra
639 // call to refreshUI, but it doesn't create a cycle.
641 ngModel
.$render = function () {
642 $timeout(function () {
643 // ex: msg_template_id adds new item then selects it; use $timeout to ensure that
644 // new item is added before selection is made
645 var newVal
= _
.cloneDeep(ngModel
.$modelValue
);
646 // Fix possible data-type mismatch
647 if (typeof newVal
=== 'string' && element
.select2('container').hasClass('select2-container-multi')) {
648 newVal
= newVal
.length
? newVal
.split(',') : [];
650 element
.select2('val', newVal
);
653 function refreshModel() {
654 var oldValue
= ngModel
.$viewValue
, newValue
= element
.select2('val');
655 if (oldValue
!= newValue
) {
656 scope
.$parent
.$apply(function () {
657 ngModel
.$setViewValue(newValue
);
663 // TODO can we infer "entity" from model?
664 element
.crmEntityRef(scope
.crmEntityref
|| {});
665 element
.on('change', refreshModel
);
666 $timeout(ngModel
.$render
);
674 // example <div crm-ui-tab id="tab-1" crm-title="ts('My Title')" count="3">...content...</div>
675 // WISHLIST: use a full Angular component instead of an incomplete jQuery wrapper
676 .directive('crmUiTab', function($parse
) {
678 require
: '^crmUiTabSet',
686 template
: '<div ng-transclude></div>',
688 link: function (scope
, element
, attrs
, crmUiTabSetCtrl
) {
689 crmUiTabSetCtrl
.add(scope
);
694 // 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>
695 .directive('crmUiTabSet', function() {
702 templateUrl
: '~/crmUi/tabset.html',
704 controllerAs
: 'crmUiTabSetCtrl',
705 controller: function($scope
, $parse
) {
706 var tabs
= $scope
.tabs
= []; // array<$scope>
707 this.add = function(tab
) {
708 if (!tab
.id
) throw "Tab is missing 'id'";
712 link: function (scope
, element
, attrs
) {}
716 // Generic, field-independent form validator.
717 // example: <span ng-model="placeholder" crm-ui-validate="foo && bar || whiz" />
718 // example: <span ng-model="placeholder" crm-ui-validate="foo && bar || whiz" crm-ui-validate-name="myError" />
719 .directive('crmUiValidate', function() {
723 link: function(scope
, element
, attrs
, ngModel
) {
724 var validationKey
= attrs
.crmUiValidateName
? attrs
.crmUiValidateName
: 'crmUiValidate';
725 scope
.$watch(attrs
.crmUiValidate
, function(newValue
){
726 ngModel
.$setValidity(validationKey
, !!newValue
);
732 // like ng-show, but hides/displays elements using "visibility" which maintains positioning
733 // example <div crm-ui-visible="false">...content...</div>
734 .directive('crmUiVisible', function($parse
) {
740 link: function (scope
, element
, attrs
) {
741 var model
= $parse(attrs
.crmUiVisible
);
742 function updatecChildren() {
743 element
.css('visibility', model(scope
.$parent
) ? 'inherit' : 'hidden');
746 scope
.$parent
.$watch(attrs
.crmUiVisible
, updatecChildren
);
751 // 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>
752 // example with custom nav classes: <div crm-ui-wizard crm-ui-wizard-nav-class="ng-animate-out ...">...</div>
753 // Note: "myWizardCtrl" has various actions/properties like next() and $first().
754 // WISHLIST: Allow each step to determine if it is "complete" / "valid" / "selectable"
755 // WISHLIST: Allow each step to enable/disable (show/hide) itself
756 .directive('crmUiWizard', function() {
761 crmUiWizardNavClass
: '@' // string, A list of classes that will be added to the nav items
763 templateUrl
: '~/crmUi/wizard.html',
765 controllerAs
: 'crmUiWizardCtrl',
766 controller: function($scope
, $parse
) {
767 var steps
= $scope
.steps
= []; // array<$scope>
768 var crmUiWizardCtrl
= this;
770 var selectedIndex
= null;
772 var findIndex = function() {
774 angular
.forEach(steps
, function(step
, stepKey
) {
775 if (step
.selected
) found
= stepKey
;
780 /// @return int the index of the current step
781 this.$index = function() { return selectedIndex
; };
782 /// @return bool whether the currentstep is first
783 this.$first = function() { return this.$index() === 0; };
784 /// @return bool whether the current step is last
785 this.$last = function() { return this.$index() === steps
.length
-1; };
786 this.$maxVisit = function() { return maxVisited
; };
787 this.$validStep = function() {
788 return steps
[selectedIndex
] && steps
[selectedIndex
].isStepValid();
790 this.iconFor = function(index
) {
791 if (index
< this.$index()) return '√';
792 if (index
=== this.$index()) return '»';
795 this.isSelectable = function(step
) {
796 if (step
.selected
) return false;
798 angular
.forEach(steps
, function(otherStep
, otherKey
) {
799 if (step
=== otherStep
&& otherKey
<= maxVisited
) result
= true;
804 /*** @param Object step the $scope of the step */
805 this.select = function(step
) {
806 angular
.forEach(steps
, function(otherStep
, otherKey
) {
807 otherStep
.selected
= (otherStep
=== step
);
808 if (otherStep
=== step
&& maxVisited
< otherKey
) maxVisited
= otherKey
;
810 selectedIndex
= findIndex();
812 /*** @param Object step the $scope of the step */
813 this.add = function(step
) {
814 if (steps
.length
=== 0) {
815 step
.selected
= true;
819 steps
.sort(function(a
,b
){
820 return a
.crmUiWizardStep
- b
.crmUiWizardStep
;
822 selectedIndex
= findIndex();
824 this.remove = function(step
) {
826 angular
.forEach(steps
, function(otherStep
, otherKey
) {
827 if (otherStep
=== step
) key
= otherKey
;
830 steps
.splice(key
, 1);
833 this.goto = function(index
) {
834 if (index
< 0) index
= 0;
835 if (index
>= steps
.length
) index
= steps
.length
-1;
836 this.select(steps
[index
]);
838 this.previous = function() { this.goto(this.$index()-1); };
839 this.next = function() { this.goto(this.$index()+1); };
840 if ($scope
.crmUiWizard
) {
841 $parse($scope
.crmUiWizard
).assign($scope
.$parent
, this);
844 link: function (scope
, element
, attrs
) {
845 scope
.ts
= CRM
.ts(null);
850 // Use this to add extra markup to wizard
851 .directive('crmUiWizardButtons', function() {
853 require
: '^crmUiWizard',
856 template
: '<span ng-transclude></span>',
858 link: function (scope
, element
, attrs
, crmUiWizardCtrl
) {
859 var realButtonsEl
= $(element
).closest('.crm-wizard').find('.crm-wizard-buttons');
860 $(element
).appendTo(realButtonsEl
);
865 // Example for Font Awesome: <button crm-icon="fa-check">Save</button>
866 // Example for jQuery UI (deprecated): <button crm-icon="check">Save</button>
867 .directive('crmIcon', function() {
870 link: function (scope
, element
, attrs
) {
871 if (element
.is('[crm-ui-tab]')) {
872 // handled in crmUiTab ctrl
875 if (attrs
.crmIcon
.substring(0,3) == 'fa-') {
876 $(element
).prepend('<i class="crm-i ' + attrs
.crmIcon
+ '"></i> ');
879 $(element
).prepend('<span class="icon ui-icon-' + attrs
.crmIcon
+ '"></span> ');
881 if ($(element
).is('button')) {
882 $(element
).addClass('crm-button');
888 // example: <div crm-ui-wizard-step crm-title="ts('My Title')" ng-form="mySubForm">...content...</div>
889 // If there are any conditional steps, then be sure to set a weight explicitly on *all* steps to maintain ordering.
890 // example: <div crm-ui-wizard-step="100" crm-title="..." ng-if="...">...content...</div>
891 // example with custom classes: <div crm-ui-wizard-step="100" crm-ui-wizard-step-class="ng-animate-out ...">...content...</div>
892 .directive('crmUiWizardStep', function() {
895 require
: ['^crmUiWizard', 'form'],
898 crmTitle
: '@', // expression, evaluates to a printable string
899 crmUiWizardStep
: '@', // int, a weight which determines the ordering of the steps
900 crmUiWizardStepClass
: '@' // string, A list of classes that will be added to the template
902 template
: '<div class="crm-wizard-step {{crmUiWizardStepClass}}" ng-show="selected" ng-transclude/></div>',
904 link: function (scope
, element
, attrs
, ctrls
) {
905 var crmUiWizardCtrl
= ctrls
[0], form
= ctrls
[1];
906 if (scope
.crmUiWizardStep
) {
907 scope
.crmUiWizardStep
= parseInt(scope
.crmUiWizardStep
);
909 scope
.crmUiWizardStep
= nextWeight
++;
911 scope
.isStepValid = function() {
914 crmUiWizardCtrl
.add(scope
);
915 scope
.$on('$destroy', function(){
916 crmUiWizardCtrl
.remove(scope
);
922 // Example: <button crm-confirm="{message: ts('Are you sure you want to continue?')}" on-yes="frobnicate(123)">Frobincate</button>
923 // Example: <button crm-confirm="{type: 'disable', obj: myObject}" on-yes="myObject.is_active=0; myObject.save()">Disable</button>
924 // Example: <button crm-confirm="{templateUrl: '~/path/to/view.html', export: {foo: bar}}" on-yes="frobnicate(123)">Frobincate</button>
925 .directive('crmConfirm', function ($compile
, $rootScope
, $templateRequest
, $q
) {
926 // Helpers to calculate default options for CRM.confirm()
928 'disable': function (options
) {
930 message
: ts('Are you sure you want to disable this?'),
931 options
: {no
: ts('Cancel'), yes
: ts('Disable')},
933 title
: ts('Disable %1?', {
934 1: options
.obj
.title
|| options
.obj
.label
|| options
.obj
.name
|| ts('the record')
938 'revert': function (options
) {
940 message
: ts('Are you sure you want to revert this?'),
941 options
: {no
: ts('Cancel'), yes
: ts('Revert')},
943 title
: ts('Revert %1?', {
944 1: options
.obj
.title
|| options
.obj
.label
|| options
.obj
.name
|| ts('the record')
948 'delete': function (options
) {
950 message
: ts('Are you sure you want to delete this?'),
951 options
: {no
: ts('Cancel'), yes
: ts('Delete')},
953 title
: ts('Delete %1?', {
954 1: options
.obj
.title
|| options
.obj
.label
|| options
.obj
.name
|| ts('the record')
959 var confirmCount
= 0;
961 link: function (scope
, element
, attrs
) {
962 $(element
).click(function () {
963 var options
= scope
.$eval(attrs
.crmConfirm
);
964 if (attrs
.title
&& !options
.title
) {
965 options
.title
= attrs
.title
;
967 var defaults
= (options
.type
) ? defaultFuncs
[options
.type
](options
) : {};
969 var tpl
= null, stubId
= null;
970 if (!options
.message
) {
971 if (options
.templateUrl
) {
972 tpl
= $templateRequest(options
.templateUrl
);
974 else if (options
.template
) {
975 tpl
= options
.template
;
978 stubId
= 'crmUiConfirm_' + (++confirmCount
);
979 options
.message
= '<div id="' + stubId
+ '"></div>';
983 CRM
.confirm(_
.extend(defaults
, options
))
984 .on('crmConfirm:yes', function() { scope
.$apply(attrs
.onYes
); })
985 .on('crmConfirm:no', function() { scope
.$apply(attrs
.onNo
); });
988 $q
.when(tpl
, function(html
) {
989 var scope
= options
.scope
|| $rootScope
.$new();
990 if (options
.export) {
991 angular
.extend(scope
, options
.export);
993 var linker
= $compile(html
);
994 $('#' + stubId
).append($(linker(scope
)));
1002 // Sets document title & page title; attempts to override CMS title markup for the latter
1003 // WARNING: Use only once per route!
1004 // Example (same title for both): <h1 crm-page-title>{{ts('Hello')}}</h1>
1005 // Example (separate document title): <h1 crm-document-title="ts('Hello')" crm-page-title><i class="crm-i fa-flag"></i>{{ts('Hello')}}</h1>
1006 .directive('crmPageTitle', function($timeout
) {
1009 crmDocumentTitle
: '='
1011 link: function(scope
, $el
, attrs
) {
1013 $timeout(function() {
1014 var newPageTitle
= _
.trim($el
.html()),
1015 newDocumentTitle
= scope
.crmDocumentTitle
|| $el
.text();
1016 document
.title
= $('title').text().replace(documentTitle
, newDocumentTitle
);
1017 // If the CMS has already added title markup to the page, use it
1018 $('h1').not('.crm-container h1').each(function() {
1019 if (_
.trim($(this).html()) === pageTitle
) {
1020 $(this).addClass('crm-page-title').html(newPageTitle
);
1024 pageTitle
= newPageTitle
;
1025 documentTitle
= newDocumentTitle
;
1029 scope
.$watch(function() {return scope
.crmDocumentTitle
+ $el
.html();}, update
);
1034 .run(function($rootScope
, $location
) {
1035 /// Example: <button ng-click="goto('home')">Go home!</button>
1036 $rootScope
.goto = function(path
) {
1037 $location
.path(path
);
1039 // useful for debugging: $rootScope.log = console.log || function() {};
1043 })(angular
, CRM
.$, CRM
._
);