CRM-16585 - Add print button to dialogs
authorColeman Watts <coleman@civicrm.org>
Thu, 28 May 2015 23:07:25 +0000 (19:07 -0400)
committerColeman Watts <coleman@civicrm.org>
Fri, 29 May 2015 00:04:58 +0000 (20:04 -0400)
js/crm.ajax.js

index 02a4a6312b041409efbeed994bee7c78a40864a0..8d266e38bd41186d4539f0b1288d15fcd4347fe0 100644 (file)
         this.element.dialog('close');
       }
     },
-    _formatUrl: function(url) {
+    _formatUrl: function(url, snippetType) {
       // Strip hash
       url = url.split('#')[0];
       // Add snippet argument to url
-      if (url.search(/[&?]snippet=/) < 0) {
-        url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=json';
-      } else {
-        url = url.replace(/snippet=[^&]*/, 'snippet=json');
+      if (snippetType) {
+        if (url.search(/[&?]snippet=/) < 0) {
+          url += (url.indexOf('?') < 0 ? '?' : '&') + 'snippet=' + snippetType;
+        } else {
+          url = url.replace(/snippet=[^&]*/, 'snippet=' + snippetType);
+        }
       }
       return url;
     },
       var that = this;
       $('a.crm-weight-arrow', that.element).click(function(e) {
         if (that.options.block) that.element.block();
-        $.getJSON(that._formatUrl(this.href)).done(function() {
+        $.getJSON(that._formatUrl(this.href, 'json')).done(function() {
           that.refresh();
         });
         e.stopImmediatePropagation();
     },
     refresh: function() {
       var that = this;
-      var url = this._formatUrl(this.options.url);
+      var url = this._formatUrl(this.options.url, 'json');
       if (this.options.crmForm) $('form', this.element).ajaxFormUnbind();
       if (this.options.block) this.element.block();
       $.getJSON(url, function(data) {
     // Create new dialog
     if (settings.dialog) {
       settings.dialog = CRM.utils.adjustDialogDefaults(settings.dialog);
-      $('<div id="' + settings.target.substring(1) + '"></div>').dialog(settings.dialog);
+      $('<div id="' + settings.target.substring(1) + '"></div>')
+        .dialog(settings.dialog)
+        .parent().find('.ui-dialog-titlebar')
+        .append($('<a class="crm-dialog-titlebar-print ui-dialog-titlebar-close" title="'+ts('Print window')+'" target="_blank" style="right:3.8em;"/>')
+          .button({icons: {primary: 'ui-icon-print'}, text: false}));
     }
+    // Add handlers to new or existing dialog
     if ($(settings.target).data('uiDialog')) {
       $(settings.target)
         .on('dialogclose', function() {
           if (e.target === $(settings.target)[0] && data && !settings.dialog.title && data.title) {
             $(this).dialog('option', 'title', data.title);
           }
+          // Update print url
+          $(this).parent().find('a.crm-dialog-titlebar-print').attr('href', $(this).data('civiCrmSnippet')._formatUrl($(this).crmSnippet('option', 'url'), '2'));
         });
     }
     $(settings.target).crmSnippet(settings).crmSnippet('refresh');