Moved to App.Utils.replaceTags() and fixed quoting issues.

This commit is contained in:
Martin Edenhofer 2016-03-09 08:09:56 +01:00
parent 18e407693a
commit f6deecacd4
4 changed files with 46 additions and 54 deletions

View file

@ -2,6 +2,9 @@ class App.WidgetTextModule extends App.Controller
constructor: -> constructor: ->
super super
if !@data
@data = {}
# remember instances # remember instances
@bindElements = [] @bindElements = []
if @selector if @selector
@ -13,7 +16,7 @@ class App.WidgetTextModule extends App.Controller
@bindElements = @$('[contenteditable]').textmodule() @bindElements = @$('[contenteditable]').textmodule()
@update() @update()
@subscribeId = App.TextModule.subscribe(@update, initFetch: true ) @subscribeId = App.TextModule.subscribe(@update, initFetch: true)
release: => release: =>
App.TextModule.unsubscribe(@subscribeId) App.TextModule.unsubscribe(@subscribeId)
@ -26,22 +29,10 @@ class App.WidgetTextModule extends App.Controller
update: => update: =>
allRaw = App.TextModule.all() allRaw = App.TextModule.all()
all = [] all = []
data = @data || @
for item in allRaw for item in allRaw
if item.active is true if item.active is true
attributes = item.attributes() attributes = item.attributes()
attributes.content = attributes.content.replace( /#\{{0,2}(.+?)\s{0,2}\}/g, ( index, key ) -> attributes.content = App.Utils.replaceTags(attributes.content, @data)
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
)
all.push attributes all.push attributes
# set new data # set new data

View file

@ -453,7 +453,7 @@ class App.Utils
# textReplaced = App.Utils.replaceTags( template, { user: { firstname: 'Bob', lastname: 'Smith' } } ) # textReplaced = App.Utils.replaceTags( template, { user: { firstname: 'Bob', lastname: 'Smith' } } )
@replaceTags: (template, objects) -> @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(/\./) levels = key.split(/\./)
dataRef = objects dataRef = objects
for level in levels for level in levels

View file

@ -14,11 +14,11 @@
debug: false debug: false
} }
function Plugin( element, options ) { function Plugin(element, options) {
this.element = element this.element = element
this.$element = $(element) this.$element = $(element)
this.options = $.extend( {}, defaults, options) this.options = $.extend({}, defaults, options)
this._defaults = defaults this._defaults = defaults
this._name = pluginName this._name = pluginName
@ -44,15 +44,15 @@
this.$element.on('keydown', function (e) { this.$element.on('keydown', function (e) {
// esc // esc
if ( e.keyCode === 27 ) { if (e.keyCode === 27) {
_this.close() _this.close()
} }
// navigate through item // navigate through item
if ( _this.isActive() ) { if (_this.isActive()) {
// enter // enter
if ( e.keyCode === 13 ) { if (e.keyCode === 13) {
e.preventDefault() e.preventDefault()
var id = _this.$widget.find('.dropdown-menu li.is-active').data('id') var id = _this.$widget.find('.dropdown-menu li.is-active').data('id')
@ -70,20 +70,21 @@
} }
// arrow keys left/right // arrow keys left/right
if ( e.keyCode === 37 || e.keyCode === 39 ) { if (e.keyCode === 37 || e.keyCode === 39) {
e.preventDefault() e.preventDefault()
return return
} }
// up or down // up or down
if ( e.keyCode === 38 || e.keyCode === 40 ) { if (e.keyCode === 38 || e.keyCode === 40) {
e.preventDefault() e.preventDefault()
var active = _this.$widget.find('.dropdown-menu li.is-active') var active = _this.$widget.find('.dropdown-menu li.is-active')
active.removeClass('is-active') active.removeClass('is-active')
if ( e.keyCode == 38 && active.prev().size() ) { if (e.keyCode == 38 && active.prev().size()) {
active = active.prev() active = active.prev()
} else if ( e.keyCode == 40 && active.next().size() ) { }
else if (e.keyCode == 40 && active.next().size()) {
active = active.next() active = active.next()
} }
@ -91,10 +92,11 @@
var menu = _this.$widget.find('.dropdown-menu') var menu = _this.$widget.find('.dropdown-menu')
if ( active.position().top < 0 ) { if (active.position().top < 0) {
// scroll up // scroll up
menu.scrollTop( menu.scrollTop() + active.position().top ) 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 // scroll down
var invisibleHeight = active.position().top + active.height() - menu.height() var invisibleHeight = active.position().top + active.height() - menu.height()
menu.scrollTop( menu.scrollTop() + invisibleHeight ) menu.scrollTop( menu.scrollTop() + invisibleHeight )
@ -108,10 +110,10 @@
this.$element.on('keydown', function (e) { this.$element.on('keydown', function (e) {
// backspace // backspace
if ( e.keyCode === 8 && _this.buffer ) { if (e.keyCode === 8 && _this.buffer) {
// backspace + buffer === :: -> close textmodule // backspace + buffer === :: -> close textmodule
if ( _this.buffer === '::' ) { if (_this.buffer === '::') {
_this.close(true) _this.close(true)
e.preventDefault() e.preventDefault()
return return
@ -119,41 +121,41 @@
// reduce buffer and show new result // reduce buffer and show new result
var length = _this.buffer.length var length = _this.buffer.length
_this.buffer = _this.buffer.substr( 0, length-1 ) _this.buffer = _this.buffer.substr(0, length-1)
_this.log( 'BS backspace', _this.buffer ) _this.log('BS backspace', _this.buffer)
_this.result( _this.buffer.substr( 2, length-1 ) ) _this.result(_this.buffer.substr(2, length-1))
} }
}) })
// build buffer // build buffer
this.$element.on('keypress', function (e) { 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 // shift
if ( e.keyCode === 16 ) return if (e.keyCode === 16) return
// enter // enter
if ( e.keyCode === 13 ) return if (e.keyCode === 13) return
// arrow keys // 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 // observer other second key
if ( _this.buffer === ':' && String.fromCharCode(e.which) !== ':' ) { if (_this.buffer === ':' && String.fromCharCode(e.which) !== ':') {
_this.buffer = '' _this.buffer = ''
} }
// oberserve second : // oberserve second :
if ( _this.buffer === ':' && String.fromCharCode(e.which) === ':' ) { if (_this.buffer === ':' && String.fromCharCode(e.which) === ':') {
_this.buffer = _this.buffer + ':' _this.buffer = _this.buffer + ':'
} }
// oberserve first : // oberserve first :
if ( !_this.buffer && String.fromCharCode(e.which) === ':' ) { if (!_this.buffer && String.fromCharCode(e.which) === ':') {
_this.buffer = _this.buffer + ':' _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) var sign = String.fromCharCode(e.which)
if ( sign && sign !== ':' && e.which != 8 ) { // 8 == backspace if ( sign && sign !== ':' && e.which != 8 ) { // 8 == backspace
@ -166,7 +168,7 @@
_this.open() _this.open()
} }
_this.result( _this.buffer.substr(2, _this.buffer.length) ) _this.result(_this.buffer.substr(2, _this.buffer.length))
} }
}).on('focus', function (e) { }).on('focus', function (e) {
@ -204,8 +206,8 @@
// set position of widget // set position of widget
Plugin.prototype.updatePosition = function() { Plugin.prototype.updatePosition = function() {
this.$widget.find('.dropdown-menu').scrollTop( 300 ); this.$widget.find('.dropdown-menu').scrollTop(300)
if ( !this.$element.is(':visible') ) return if (!this.$element.is(':visible')) return
// get cursor position // get cursor position
var marker = '<span id="js-cursor-position"></span>' var marker = '<span id="js-cursor-position"></span>'
@ -281,7 +283,7 @@
clone.setStart(range.startContainer, start-1) clone.setStart(range.startContainer, start-1)
clone.setEnd(range.startContainer, start) clone.setEnd(range.startContainer, start)
var spacerChar = clone.toString() var spacerChar = clone.toString()
if ( spacerChar === ' ' ) { if (spacerChar === ' ') {
start = start - 1 start = start - 1
} }
} }
@ -292,7 +294,7 @@
// for chrome, insert space again // for chrome, insert space again
if (start) { if (start) {
if ( spacerChar === ' ' ) { if (spacerChar === ' ') {
string = "&nbsp;" string = "&nbsp;"
if (document.selection) { // IE if (document.selection) { // IE
var range = document.selection.createRange() var range = document.selection.createRange()
@ -350,12 +352,12 @@
// render result // render result
Plugin.prototype.result = function(term) { Plugin.prototype.result = function(term) {
var _this = this var _this = this
var result = _.filter( this.collection, function(item) { var result = _.filter(this.collection, function(item) {
var reg = new RegExp( term, 'i' ) var reg = new RegExp(term, 'i')
if ( item.name && item.name.match( reg ) ) { if (item.name && item.name.match(reg)) {
return item return item
} }
if ( item.keywords && item.keywords.match( reg ) ) { if (item.keywords && item.keywords.match(reg)) {
return item return item
} }
return return
@ -372,13 +374,13 @@
var item = result[i] var item = result[i]
var element = $('<li>') var element = $('<li>')
element.attr('data-id', item.id) element.attr('data-id', item.id)
element.text(App.Utils.htmlEscape(item.name)) element.text(item.name)
element.addClass('u-clickable u-textTruncate') element.addClass('u-clickable u-textTruncate')
if (i == result.length-1) { if (i == result.length-1) {
element.addClass('is-active') element.addClass('is-active')
} }
if (item.keywords) { if (item.keywords) {
element.append($('<kbd>').text(App.Utils.htmlEscape(item.keywords))) element.append($('<kbd>').text(item.keywords))
} }
elements = elements.add(element) elements = elements.add(element)
} }
@ -397,11 +399,10 @@
} }
} }
$.fn[pluginName] = function ( options ) { $.fn[pluginName] = function (options) {
return this.each(function () { return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) { if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, $.data(this, 'plugin_' + pluginName, new Plugin(this, options))
new Plugin( this, options ));
} }
}); });
} }

View file

@ -101,7 +101,7 @@ class AgentTicketActionLevel0Test < TestCase
data: { data: {
name: 'some name' + random, name: 'some name' + random,
keywords: random, keywords: random,
content: 'some content #{@ticket.customer.lastname}' + random, content: 'some content #{ticket.customer.lastname}' + random,
}, },
) )