From 28e111b72abb45f7be3a8e7dd9b5a2e55df90eeb Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 22 Jun 2021 17:06:13 -0700 Subject: [PATCH] msgtplui - Preview - Extract separate ctrl. Prepare for AJAX loading. --- ext/msgtplui/ang/msgtplui/Edit.js | 12 --------- ext/msgtplui/ang/msgtplui/Preview.html | 36 ++++++++++++++----------- ext/msgtplui/ang/msgtplui/Preview.js | 37 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 ext/msgtplui/ang/msgtplui/Preview.js diff --git a/ext/msgtplui/ang/msgtplui/Edit.js b/ext/msgtplui/ang/msgtplui/Edit.js index 9d5988bf75..85bfb9b340 100644 --- a/ext/msgtplui/ang/msgtplui/Edit.js +++ b/ext/msgtplui/ang/msgtplui/Edit.js @@ -242,7 +242,6 @@ // Ex: $rootScope.$emit('previewMsgTpl', {revisionName: 'txDraft', formatName: 'msg_text'}) function onPreview(event, args) { - var defaults = { exampleName: 'fix-this-example', examples: [{id: 0, name: 'fix-this-example', label: ts('Fix this example')}], @@ -261,16 +260,6 @@ title: ts('Preview') }; var model = angular.extend({}, defaults, args); - model.exampleId = parseInt(_.findKey(model.examples, {name: model.exampleName})); - model.revisionId = parseInt(_.findKey(model.revisions, {name: model.revisionName})); - model.formatId = parseInt(_.findKey(model.formats, {name: model.formatName})); - model.cycle = function(idFld, listFld, delta){ - model[idFld] = (model[idFld] + delta) % model[listFld].length; - }; - delete model.exampleName; - delete model.revisionName; - delete model.formatName; - var options = CRM.utils.adjustDialogDefaults({ dialogClass: 'msgtplui-dialog', autoOpen: false, @@ -280,7 +269,6 @@ return dialogService.open('previewMsgDlg', '~/msgtplui/Preview.html', model, options) // Nothing to do but hide warnings. The field was edited live. .then(function(){}, function(){}); - } $rootScope.$on('previewMsgTpl', onPreview); $rootScope.$on('$destroy', function (){ diff --git a/ext/msgtplui/ang/msgtplui/Preview.html b/ext/msgtplui/ang/msgtplui/Preview.html index c7c81071c2..e0e66ea8ff 100644 --- a/ext/msgtplui/ang/msgtplui/Preview.html +++ b/ext/msgtplui/ang/msgtplui/Preview.html @@ -1,4 +1,4 @@ -
+
@@ -24,10 +24,10 @@
- +
- - + +
@@ -35,10 +35,10 @@
- +
- - + +
@@ -46,28 +46,32 @@
- +
- - + +

-
+
+ {{::ts('Loading...')}} +
+ +
-

{{model.revisions[model.revisionId].rec.msg_subject}}

+

{{$ctrl.preview.msg_subject}}

-
-
{{model.revisions[model.revisionId].rec.msg_text}}
+
+
{{$ctrl.preview.msg_text}}
-
+
- +
diff --git a/ext/msgtplui/ang/msgtplui/Preview.js b/ext/msgtplui/ang/msgtplui/Preview.js new file mode 100644 index 0000000000..895de19696 --- /dev/null +++ b/ext/msgtplui/ang/msgtplui/Preview.js @@ -0,0 +1,37 @@ +(function(angular, $, _) { + + angular.module('msgtplui').controller('MsgtpluiPreviewCtrl', function($scope, crmUiHelp, $timeout, $q) { + var ts = $scope.ts = CRM.ts('msgtplui'); + var hs = $scope.hs = crmUiHelp({file: 'CRM/Msgtplui/msgtplui'}); // See: templates/CRM/Msgtplui/msgtplui.hlp + + var $ctrl = this, model = $scope.model; + + $ctrl.exampleId = parseInt(_.findKey(model.examples, {name: model.exampleName})); + $ctrl.revisionId = parseInt(_.findKey(model.revisions, {name: model.revisionName})); + $ctrl.formatId = parseInt(_.findKey(model.formats, {name: model.formatName})); + $ctrl.cycle = function(idFld, listFld, delta){ + $ctrl[idFld] = ($ctrl[idFld] + delta) % model[listFld].length; + }; + + var lastId = null; + var update = function update() { + var id = $ctrl.revisionId + ':' + $ctrl.exampleId; + if (lastId === id) return; + lastId = id; + + $ctrl.preview = {loading: true}; + var dfr = $q.defer(); + $timeout(function(){ + $ctrl.preview = model.revisions[$ctrl.revisionId].rec; + dfr.resolve($ctrl.preview); + }, 333); + return dfr.promise; + } + + $scope.$watch('$ctrl.revisionId', update); + $scope.$watch('$ctrl.formatId', update); + $scope.$watch('$ctrl.exampleId', update); + update(); + }); + +})(angular, CRM.$, CRM._); -- 2.25.1