crmUiHelp - Render title once (rather than watching)
authorTim Otten <totten@civicrm.org>
Wed, 18 Mar 2015 21:17:51 +0000 (14:17 -0700)
committerColeman Watts <coleman@civicrm.org>
Thu, 19 Mar 2015 15:39:48 +0000 (11:39 -0400)
js/angular-crm-ui.js
partials/crmUi/field.html

index 8ed534c9027e932dd427572b834a4dbfc71fa119..c97a861444c1276ccf3ff8c60a3c3b3c8d006863 100644 (file)
     // example: <div crm-ui-field="{title: ts('My Field')}"> {{mydata}} </div>
     // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field')}"> <input crm-ui-id="subform.myfield" name="myfield" /> </div>
     // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field')}"> <input crm-ui-id="subform.myfield" name="myfield" required /> </div>
-    // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field'), help: help('help_field_name')}"> {{mydata}} </div>
+    // example: <div crm-ui-field="{name: 'subform.myfield', title: ts('My Field'), help: hs('help_field_name')}"> {{mydata}} </div>
     .directive('crmUiField', function() {
       // Note: When writing new templates, the "label" position is particular. See/patch "var label" below.
       var templateUrls = {
         transclude: true,
         link: function (scope, element, attrs, crmUiIdCtrl) {
           $(element).addClass('crm-section');
+          scope.help = null;
           scope.$watch('crmUiField', function(crmUiField) {
             if (crmUiField && crmUiField.help) {
               scope.help = crmUiField.help.clone({}, {
         }
       });
 
-      // example: var help = crmUiHelp({file: 'CRM/Foo/Bar'});
+      // example: var hs = crmUiHelp({file: 'CRM/Foo/Bar'});
       return function(defaults){
-        // example: help('myfield')
-        // example: help({id: 'myfield', title: 'Foo Bar', file: 'Whiz/Bang'})
+        // example: hs('myfield')
+        // example: hs({id: 'myfield', title: 'Foo Bar', file: 'Whiz/Bang'})
         return function(options) {
           if (_.isString(options)) {
             options = {id: options};
 
     // Display a help icon
     // Example: Use a default *.hlp file
-    //   scope.help = crmUiHelp({file: 'Path/To/Help/File'});
-    //   HTML: <a crm-ui-help="help({title:ts('My Field'), id:'my_field'})">
+    //   scope.hs = crmUiHelp({file: 'Path/To/Help/File'});
+    //   HTML: <a crm-ui-help="hs({title:ts('My Field'), id:'my_field'})">
     // Example: Use an explicit *.hlp file
-    //   HTML: <a crm-ui-help="help({title:ts('My Field'), id:'my_field', file:'CRM/Foo/Bar'})">
+    //   HTML: <a crm-ui-help="hs({title:ts('My Field'), id:'my_field', file:'CRM/Foo/Bar'})">
     .directive('crmUiHelp', function() {
       return {
         restrict: 'EA',
-        link: function (scope, element, attrs) {
-          var crmUiHelp = null;
-
-          scope.$watch(attrs.crmUiHelp, function(newCrmUiHelp){
-            crmUiHelp = newCrmUiHelp;
+        link: function(scope, element, attrs) {
+          setTimeout(function() {
+            var crmUiHelp = scope.$eval(attrs.crmUiHelp);
             var title = crmUiHelp && crmUiHelp.get('title') ? ts('%1 Help', {1: crmUiHelp.get('title')}) : ts('Help');
             element.attr('title', title);
-          });
+          }, 50);
 
           element
             .addClass('helpicon')
             .attr('href', '#')
             .on('click', function(e) {
               e.preventDefault();
-              crmUiHelp.open();
+              scope.$eval(attrs.crmUiHelp).open();
             });
         }
       };
index cf160bc60a242506c2132119b9a13d6ef56b8b46..aca140d29ed0c3c5253faa61c411b98003d603db 100644 (file)
@@ -1,6 +1,6 @@
 <div class="label">
   <label crm-ui-for="{{crmUiField.name}}" crm-depth="1">{{crmUiField.title}}</label>
-  <a crm-ui-help="help" ng-if="crmUiField.help"></a>
+  <a crm-ui-help="help" ng-if="help"></a>
 </div>
 <div class="content" ng-transclude></div>
 <div class="clear"></div>