From 5e7662764132d05da5ee4be05c77a16f374d5ade Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Wed, 28 Sep 2016 13:50:44 -0400 Subject: [PATCH] Revert "(js) Drop sgTransformOnBlur directive" This reverts commit 67258fe337f6d6b6442fa05b7a48ad9de7072894. Fixes #3815 --- UI/Templates/MailerUI/UIxMailEditor.wox | 6 +- UI/Templates/MailerUI/UIxMailMainFrame.wox | 2 +- .../js/Common/sgTransformOnBlur.directive.js | 71 +++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 UI/WebServerResources/js/Common/sgTransformOnBlur.directive.js diff --git a/UI/Templates/MailerUI/UIxMailEditor.wox b/UI/Templates/MailerUI/UIxMailEditor.wox index 9e91fb5c9..0978947d2 100644 --- a/UI/Templates/MailerUI/UIxMailEditor.wox +++ b/UI/Templates/MailerUI/UIxMailEditor.wox @@ -57,7 +57,7 @@ + sg-transform-on-blur="sg-transform-on-blur"> + sg-transform-on-blur="sg-transform-on-blur"> + sg-transform-on-blur="sg-transform-on-blur"> + sg-transform-on-blur="sg-transform-on-blur"> diff --git a/UI/WebServerResources/js/Common/sgTransformOnBlur.directive.js b/UI/WebServerResources/js/Common/sgTransformOnBlur.directive.js new file mode 100644 index 000000000..43a8a9a88 --- /dev/null +++ b/UI/WebServerResources/js/Common/sgTransformOnBlur.directive.js @@ -0,0 +1,71 @@ +/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ + +(function() { + /* jshint loopfunc: true */ + 'use strict'; + + /** + * sgTransformOnBlur - A directive to extend md-chips so the text of the input + * field is converted to a chip when the field is loosing focus. + * + * See issue on github: + * + * https://github.com/angular/material/issues/3364 + * + * Code is extracted from "MdChipsCtrl.prototype.onInputBlur" in controller: + * + * angular-material/src/components/chips/js/chipsController.js + * + * @memberof SOGo.Common + * @ngInject + * @example: + + + */ + sgTransformOnBlur.$inject = ['$window', '$timeout']; + function sgTransformOnBlur($window, $timeout) { + return { + link: link, + require: 'mdChips', // Extends the original mdChips directive + restrict: 'A' + }; + + function link(scope, element, attributes, mdChipsCtrl) { + var mouseUpActions = []; + + mdChipsCtrl.onInputBlur = function() { + var appendFcn; + + this.inputHasFocus = false; + appendFcn = (function() { + var chipBuffer = this.getChipBuffer(); + if ((this.hasAutocomplete && this.requireMatch) || !chipBuffer || chipBuffer === "") return; + this.appendChip(chipBuffer); + this.resetChipBuffer(); + }).bind(this); + + if (this.hasAutocomplete) { + mouseUpActions.push(appendFcn); + $window.addEventListener('click', function(event){ + while (mouseUpActions.length > 0) { + // Trigger actions after some delay to give time to md-autocomple to clear the input field + var action = mouseUpActions.splice(0,1)[0]; + $timeout(function(){ + $timeout(action); + }); + } + }, false); + } + else + appendFcn(); + }; + } + } + + angular + .module('SOGo.Common') + .directive('sgTransformOnBlur', sgTransformOnBlur); +})();