From 43588554fd4a70cb70492ea1f4586278a6b21c0b Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 14 Oct 2014 02:44:52 +0200 Subject: [PATCH] Fixed some IE issues. --- .../app/lib/base/jquery.contenteditable.js | 28 ++++++---- .../app/lib/base/jquery.textmodule.js | 56 ++++++------------- 2 files changed, 35 insertions(+), 49 deletions(-) diff --git a/app/assets/javascripts/app/lib/base/jquery.contenteditable.js b/app/assets/javascripts/app/lib/base/jquery.contenteditable.js index 8f249606e..fa573f508 100644 --- a/app/assets/javascripts/app/lib/base/jquery.contenteditable.js +++ b/app/assets/javascripts/app/lib/base/jquery.contenteditable.js @@ -132,11 +132,7 @@ e.preventDefault() return } - - newLine = "
" - if ( this.options.mode === 'textonly' ) { - newLine = "\n" - } + newLine = "

" if (document.selection) { var range = document.selection.createRange() newLine = "
" // ie is not supporting \n :( @@ -154,7 +150,14 @@ // just paste text if ( this.options.mode === 'textonly' ) { this.$element.on('paste', $.proxy(function (e) { - var text = (e.originalEvent || e).clipboardData.getData('text/plain') + e.preventDefault() + var text + if (window.clipboardData) { // IE + text = window.clipboardData.getData('Text') + } + else { + text = (e.originalEvent || e).clipboardData.getData('text/plain') + } var overlimit = false if (text) { @@ -177,8 +180,13 @@ } // insert new text - e.preventDefault() - document.execCommand('inserttext', false, text) + if (document.selection) { // IE + var range = document.selection.createRange() + range.pasteHTML(text) + } + else { + document.execCommand('inserttext', false, text) + } this.maxLengthOk( overlimit ) } @@ -243,7 +251,7 @@ if ( length > this.options.maxlength ) { // try to set error on framework form - parent = this.$element.parent().parent() + var parent = this.$element.parent().parent() if ( parent.hasClass('controls') ) { parent.addClass('has-error') setTimeout($.proxy(function(){ @@ -302,7 +310,7 @@ // strip html signes if multi line exists if ( this.options.multiline ) { - text = this.$element.html() + var text = this.$element.html() text = text.replace(/
/g, "\n") // new line as br text = text.replace(/
/g, "\n") // in some caes, new line als div text = $("
" + text + "
").text().trim() diff --git a/app/assets/javascripts/app/lib/base/jquery.textmodule.js b/app/assets/javascripts/app/lib/base/jquery.textmodule.js index 04ebdc467..d5cd96838 100644 --- a/app/assets/javascripts/app/lib/base/jquery.textmodule.js +++ b/app/assets/javascripts/app/lib/base/jquery.textmodule.js @@ -45,13 +45,11 @@ // navigate through widget if ( this.isActive() ) { - console.log('WIDGET IS OPEN', e.keyCode) // enter if ( e.keyCode === 13 ) { e.preventDefault() var id = this.$widget.find('.dropdown-menu li.active a').data('id') - console.log('ID', id) this.take(id) } @@ -89,7 +87,6 @@ if ( next[0] ) { top = next.addClass('active').position().top } - console.log('scrollTop', top, top-30) this.$widget.find('.dropdown-menu').scrollTop( top ); } @@ -105,9 +102,10 @@ if ( this.buffer === '::' ) { this.close() } - this.buffer = this.buffer.substr( 0, this.buffer.length-1 ) - console.log('BS', this.buffer) - this.result( this.buffer.substr(2,this.buffer.length) ) + var length = this.buffer.length + this.buffer = this.buffer.substr( 0, length-1 ) + console.log('BS backspace', this.buffer) + this.result( this.buffer.substr( 2, length-1 ) ) } }, this )) @@ -122,18 +120,18 @@ } // enter : - if ( e.keyCode === 58 ) { + if ( String.fromCharCode(e.which) === ':' ) { this.buffer = this.buffer + ':' } if ( this.buffer && this.buffer.substr(0,2) === '::' ) { - var sign = String.fromCharCode(e.which) - if ( e.keyCode !== 58 ) { + if ( sign && sign !== ':' && e.which != 8 ) { // 8 == backspace this.buffer = this.buffer + sign + //console.log('BUFF ADD', sign, this.buffer, sign.length, e.which) } - console.log('BUFF HINT', this.buffer, this.buffer.length, e.which) + console.log('BUFF HINT', this.buffer, this.buffer.length, e.which, String.fromCharCode(e.which)) this.result( this.buffer.substr(2,this.buffer.length) ) @@ -161,37 +159,16 @@ Plugin.prototype.baseTemplate = function() { this.$element.after('') this.$widget = this.$element.next() - this.updatePosition() - } - - // get cursor position - Plugin.prototype.getCaretPosition = function() { - // not needed on IE - if (document.selection) { - return - } - else { - document.execCommand('insertHTML', false, '') - } - var hiddenNode = document.getElementById('hidden'); - if (!hiddenNode) { - return 0; - } - var position = $(hiddenNode).position() - hiddenNode.parentNode.removeChild(hiddenNode) - return position } // update widget position Plugin.prototype.updatePosition = function() { this.$widget.find('.dropdown-menu').scrollTop( 300 ); - var position = this.getCaretPosition() - var heightTextarea = this.$element.height() - var widgetHeight = this.$widget.find('ul').height() + 40 - console.log('position', position) - console.log('heightTextarea', heightTextarea) - console.log('widgetHeight', widgetHeight) - this.$widget.css('top', position.top - heightTextarea - widgetHeight) + if ( !this.$element.is(':visible') ) return + var position = this.$element.caret('position'); + if (!position) return + var widgetHeight = this.$widget.find('ul').height() + 85 + this.$widget.css('top', position.top - widgetHeight) if ( !this.isActive() ) { this.$widget.css('left', position.left) } @@ -232,7 +209,7 @@ if ( item.id == id ) { var content = item.content + "\n" this.cutInput() - if (document.selection) { + if (document.selection) { // IE var range = document.selection.createRange() range.pasteHTML(content) } @@ -249,6 +226,7 @@ // cut out search string from text Plugin.prototype.cutInput = function() { if (!this.buffer) return + if (!this.$element.text()) return var sel = window.getSelection() if ( !sel || sel.rangeCount < 1) { this.buffer = '' @@ -266,7 +244,7 @@ Plugin.prototype.result = function(term) { var result = _.filter( this.collection, function(item) { - reg = new RegExp( term, 'i' ) + var reg = new RegExp( term, 'i' ) if ( item.name && item.name.match( reg ) ) { return item } @@ -280,7 +258,7 @@ console.log('result', term, result) for (var i = 0; i < result.length; i++) { var item = result[i] - template = "
  • " + item.name + var template = "
  • " + item.name if (item.keywords) { template = template + " (" + item.keywords + ")" }