Merge pull request #8101 from colemanw/CRM-18379
[civicrm-core.git] / js / jquery / jquery.crmeditable.js
index 459f27431721ab605e521259d910f873f9eef8ee..453c4969320ff3e64d3c9e7acefd13ba394795c7 100644 (file)
@@ -1,5 +1,8 @@
 // https://civicrm.org/licensing
-(function($) {
+(function($, _) {
+  "use strict";
+  /* jshint validthis: true */
+
   // TODO: We'll need a way to clear this cache if options are edited.
   // Maybe it should be stored in the CRM object so other parts of the app can use it.
   // Note that if we do move it, we should also change the format of option lists to our standard sequential arrays
@@ -15,7 +18,7 @@
       $row = this.first().closest('.crm-entity');
       ret.entity = $row.data('entity') || $row[0].id.split('-')[0];
       ret.id = $row.data('id') || $row[0].id.split('-')[1];
-      ret.action = $row.data('action') || 'setvalue';
+      ret.action = $row.data('action') || 'create';
 
     if (!ret.entity || !ret.id) {
       return false;
@@ -68,7 +71,7 @@
             if ($i.data('refresh')) {
               CRM.refreshParent($i);
             } else {
-              value = value === '' ? settings.placeholder : value;
+              value = value === '' ? settings.placeholder : _.escape(value);
               $i.html(value);
             }
           }
 
       var settings = {
         tooltip: $i.data('tooltip') || ts('Click to edit'),
-        placeholder: $i.data('placeholder') || '<span class="crm-editable-placeholder">' + ts('Click to edit') + '</span>',
+        placeholder: $i.data('placeholder') || '<i class="crm-i fa-pencil crm-editable-placeholder"></i>',
         onblur: 'cancel',
-        cancel: '<button type="cancel"><span class="ui-icon ui-icon-closethick"></span></button>',
-        submit: '<button type="submit"><span class="ui-icon ui-icon-check"></span></button>',
+        cancel: '<button type="cancel"><i class="crm-i fa-times"></i></button>',
+        submit: '<button type="submit"><i class="crm-i fa-check"></i></button>',
         cssclass: 'crm-editable-form',
         data: getData,
         onreset: restoreContainer
           }
           var result,
             info = $i.crmEditableEntity(),
-            hash = info.entity + '.' + info.field,
+            // Strip extra id from multivalued custom fields
+            custom = info.field.match(/(custom_\d+)_\d+/),
+            field = custom ? custom[1] : info.field,
+            hash = info.entity + '.' + field,
             params = {
-              field: info.field,
+              field: field,
               context: 'create'
             };
           $i.data('optionsHashKey', hash);
             });
           }
           return formatOptions(optionsCache[hash]);
-
         }
-        return value.replace(/<(?:.|\n)*?>/gm, '');
+        // Unwrap contents then replace html special characters with plain text
+        return _.unescape(value.replace(/<(?:.|\n)*?>/gm, ''));
       }
 
       function formatOptions(options) {
       }
 
       function restoreContainer() {
-        errorMsg && errorMsg.close && errorMsg.close();
+        if (errorMsg && errorMsg.close) errorMsg.close();
         $i.removeClass('crm-editable-saving crm-editable-editing');
       }
 
   };
 
   $(document).on('crmLoad', function(e) {
-    $('.crm-editable', e.target).crmEditable();
+    $('.crm-editable', e.target).not('thead *').crmEditable();
   });
 
-})(jQuery);
+})(jQuery, CRM._);