From f6deecacd4914ee1bca0cce6ea830a9de330d1da Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 9 Mar 2016 08:09:56 +0100 Subject: [PATCH] Moved to App.Utils.replaceTags() and fixed quoting issues. --- .../app/controllers/widget/text_module.coffee | 19 ++--- .../javascripts/app/lib/app_post/utils.coffee | 2 +- .../app/lib/base/jquery.textmodule.js | 77 ++++++++++--------- .../agent_ticket_actions_level0_test.rb | 2 +- 4 files changed, 46 insertions(+), 54 deletions(-) diff --git a/app/assets/javascripts/app/controllers/widget/text_module.coffee b/app/assets/javascripts/app/controllers/widget/text_module.coffee index d99a3cec5..b4027bfeb 100644 --- a/app/assets/javascripts/app/controllers/widget/text_module.coffee +++ b/app/assets/javascripts/app/controllers/widget/text_module.coffee @@ -2,6 +2,9 @@ class App.WidgetTextModule extends App.Controller constructor: -> super + if !@data + @data = {} + # remember instances @bindElements = [] if @selector @@ -13,7 +16,7 @@ class App.WidgetTextModule extends App.Controller @bindElements = @$('[contenteditable]').textmodule() @update() - @subscribeId = App.TextModule.subscribe(@update, initFetch: true ) + @subscribeId = App.TextModule.subscribe(@update, initFetch: true) release: => App.TextModule.unsubscribe(@subscribeId) @@ -26,22 +29,10 @@ class App.WidgetTextModule extends App.Controller update: => allRaw = App.TextModule.all() all = [] - data = @data || @ for item in allRaw if item.active is true attributes = item.attributes() - attributes.content = attributes.content.replace( /#\{{0,2}(.+?)\s{0,2}\}/g, ( index, key ) -> - key = key.replace( /@/g, 'data.' ) - varString = "#{key}" + '' - #console.log( "tag replacement env: ", data) - try - #console.log( "tag replacement: " + key, varString ) - key = eval (varString) - catch error - #console.log( "tag replacement error: " + error ) - key = '' - return key - ) + attributes.content = App.Utils.replaceTags(attributes.content, @data) all.push attributes # set new data diff --git a/app/assets/javascripts/app/lib/app_post/utils.coffee b/app/assets/javascripts/app/lib/app_post/utils.coffee index 66ba50bd1..544ebb9e3 100644 --- a/app/assets/javascripts/app/lib/app_post/utils.coffee +++ b/app/assets/javascripts/app/lib/app_post/utils.coffee @@ -453,7 +453,7 @@ class App.Utils # textReplaced = App.Utils.replaceTags( template, { user: { firstname: 'Bob', lastname: 'Smith' } } ) @replaceTags: (template, objects) -> - template = template.replace( /#\{\s{0,2}(.+?)\s{0,2}\}/g, ( index, key ) -> + template = template.replace( /#\{\s{0,2}(.+?)\s{0,2}\}/g, (index, key) -> levels = key.split(/\./) dataRef = objects for level in levels diff --git a/app/assets/javascripts/app/lib/base/jquery.textmodule.js b/app/assets/javascripts/app/lib/base/jquery.textmodule.js index 4da286f24..492229a00 100644 --- a/app/assets/javascripts/app/lib/base/jquery.textmodule.js +++ b/app/assets/javascripts/app/lib/base/jquery.textmodule.js @@ -14,11 +14,11 @@ debug: false } - function Plugin( element, options ) { + function Plugin(element, options) { this.element = element this.$element = $(element) - this.options = $.extend( {}, defaults, options) + this.options = $.extend({}, defaults, options) this._defaults = defaults this._name = pluginName @@ -44,15 +44,15 @@ this.$element.on('keydown', function (e) { // esc - if ( e.keyCode === 27 ) { + if (e.keyCode === 27) { _this.close() } // navigate through item - if ( _this.isActive() ) { + if (_this.isActive()) { // enter - if ( e.keyCode === 13 ) { + if (e.keyCode === 13) { e.preventDefault() var id = _this.$widget.find('.dropdown-menu li.is-active').data('id') @@ -70,20 +70,21 @@ } // arrow keys left/right - if ( e.keyCode === 37 || e.keyCode === 39 ) { + if (e.keyCode === 37 || e.keyCode === 39) { e.preventDefault() return } // up or down - if ( e.keyCode === 38 || e.keyCode === 40 ) { + if (e.keyCode === 38 || e.keyCode === 40) { e.preventDefault() var active = _this.$widget.find('.dropdown-menu li.is-active') active.removeClass('is-active') - if ( e.keyCode == 38 && active.prev().size() ) { + if (e.keyCode == 38 && active.prev().size()) { active = active.prev() - } else if ( e.keyCode == 40 && active.next().size() ) { + } + else if (e.keyCode == 40 && active.next().size()) { active = active.next() } @@ -91,10 +92,11 @@ var menu = _this.$widget.find('.dropdown-menu') - if ( active.position().top < 0 ) { + if (active.position().top < 0) { // scroll up menu.scrollTop( menu.scrollTop() + active.position().top ) - } else if ( active.position().top + active.height() > menu.height() ) { + } + else if ( active.position().top + active.height() > menu.height() ) { // scroll down var invisibleHeight = active.position().top + active.height() - menu.height() menu.scrollTop( menu.scrollTop() + invisibleHeight ) @@ -108,10 +110,10 @@ this.$element.on('keydown', function (e) { // backspace - if ( e.keyCode === 8 && _this.buffer ) { + if (e.keyCode === 8 && _this.buffer) { // backspace + buffer === :: -> close textmodule - if ( _this.buffer === '::' ) { + if (_this.buffer === '::') { _this.close(true) e.preventDefault() return @@ -119,41 +121,41 @@ // reduce buffer and show new result var length = _this.buffer.length - _this.buffer = _this.buffer.substr( 0, length-1 ) - _this.log( 'BS backspace', _this.buffer ) - _this.result( _this.buffer.substr( 2, length-1 ) ) + _this.buffer = _this.buffer.substr(0, length-1) + _this.log('BS backspace', _this.buffer) + _this.result(_this.buffer.substr(2, length-1)) } }) // build buffer this.$element.on('keypress', function (e) { - _this.log('BUFF', _this.buffer, e.keyCode, String.fromCharCode(e.which) ) + _this.log('BUFF', _this.buffer, e.keyCode, String.fromCharCode(e.which)) // shift - if ( e.keyCode === 16 ) return + if (e.keyCode === 16) return // enter - if ( e.keyCode === 13 ) return + if (e.keyCode === 13) return // arrow keys - if ( e.keyCode === 37 || e.keyCode === 38 || e.keyCode === 39 || e.keyCode === 40 ) return + if (e.keyCode === 37 || e.keyCode === 38 || e.keyCode === 39 || e.keyCode === 40) return // observer other second key - if ( _this.buffer === ':' && String.fromCharCode(e.which) !== ':' ) { + if (_this.buffer === ':' && String.fromCharCode(e.which) !== ':') { _this.buffer = '' } // oberserve second : - if ( _this.buffer === ':' && String.fromCharCode(e.which) === ':' ) { + if (_this.buffer === ':' && String.fromCharCode(e.which) === ':') { _this.buffer = _this.buffer + ':' } // oberserve first : - if ( !_this.buffer && String.fromCharCode(e.which) === ':' ) { + if (!_this.buffer && String.fromCharCode(e.which) === ':') { _this.buffer = _this.buffer + ':' } - if ( _this.buffer && _this.buffer.substr(0,2) === '::' ) { + if (_this.buffer && _this.buffer.substr(0,2) === '::') { var sign = String.fromCharCode(e.which) if ( sign && sign !== ':' && e.which != 8 ) { // 8 == backspace @@ -166,7 +168,7 @@ _this.open() } - _this.result( _this.buffer.substr(2, _this.buffer.length) ) + _this.result(_this.buffer.substr(2, _this.buffer.length)) } }).on('focus', function (e) { @@ -204,8 +206,8 @@ // set position of widget Plugin.prototype.updatePosition = function() { - this.$widget.find('.dropdown-menu').scrollTop( 300 ); - if ( !this.$element.is(':visible') ) return + this.$widget.find('.dropdown-menu').scrollTop(300) + if (!this.$element.is(':visible')) return // get cursor position var marker = '' @@ -281,7 +283,7 @@ clone.setStart(range.startContainer, start-1) clone.setEnd(range.startContainer, start) var spacerChar = clone.toString() - if ( spacerChar === ' ' ) { + if (spacerChar === ' ') { start = start - 1 } } @@ -292,7 +294,7 @@ // for chrome, insert space again if (start) { - if ( spacerChar === ' ' ) { + if (spacerChar === ' ') { string = " " if (document.selection) { // IE var range = document.selection.createRange() @@ -350,12 +352,12 @@ // render result Plugin.prototype.result = function(term) { var _this = this - var result = _.filter( this.collection, function(item) { - var reg = new RegExp( term, 'i' ) - if ( item.name && item.name.match( reg ) ) { + var result = _.filter(this.collection, function(item) { + var reg = new RegExp(term, 'i') + if (item.name && item.name.match(reg)) { return item } - if ( item.keywords && item.keywords.match( reg ) ) { + if (item.keywords && item.keywords.match(reg)) { return item } return @@ -372,13 +374,13 @@ var item = result[i] var element = $('
  • ') element.attr('data-id', item.id) - element.text(App.Utils.htmlEscape(item.name)) + element.text(item.name) element.addClass('u-clickable u-textTruncate') if (i == result.length-1) { element.addClass('is-active') } if (item.keywords) { - element.append($('').text(App.Utils.htmlEscape(item.keywords))) + element.append($('').text(item.keywords)) } elements = elements.add(element) } @@ -397,11 +399,10 @@ } } - $.fn[pluginName] = function ( options ) { + $.fn[pluginName] = function (options) { return this.each(function () { if (!$.data(this, 'plugin_' + pluginName)) { - $.data(this, 'plugin_' + pluginName, - new Plugin( this, options )); + $.data(this, 'plugin_' + pluginName, new Plugin(this, options)) } }); } diff --git a/test/browser/agent_ticket_actions_level0_test.rb b/test/browser/agent_ticket_actions_level0_test.rb index 3abc0443e..16f34b7c2 100644 --- a/test/browser/agent_ticket_actions_level0_test.rb +++ b/test/browser/agent_ticket_actions_level0_test.rb @@ -101,7 +101,7 @@ class AgentTicketActionLevel0Test < TestCase data: { name: 'some name' + random, keywords: random, - content: 'some content #{@ticket.customer.lastname}' + random, + content: 'some content #{ticket.customer.lastname}' + random, }, )