diff options
Diffstat (limited to 'view/js/autocomplete.js')
-rw-r--r-- | view/js/autocomplete.js | 130 |
1 files changed, 129 insertions, 1 deletions
diff --git a/view/js/autocomplete.js b/view/js/autocomplete.js index 511a7f815..59e3600b3 100644 --- a/view/js/autocomplete.js +++ b/view/js/autocomplete.js @@ -100,6 +100,66 @@ function submit_form(e) { $(e).parents('form').submit(); } +function getWord(text, caretPos) { + var index = text.indexOf(caretPos); + var postText = text.substring(caretPos, caretPos+8); + if ((postText.indexOf("[/list]") > 0) || postText.indexOf("[/ul]") > 0 || postText.indexOf("[/ol]") > 0) { + return postText; + } +} + +function getCaretPosition(ctrl) { + var CaretPos = 0; // IE Support + if (document.selection) { + ctrl.focus(); + var Sel = document.selection.createRange(); + Sel.moveStart('character', -ctrl.value.length); + CaretPos = Sel.text.length; + } + // Firefox support + else if (ctrl.selectionStart || ctrl.selectionStart == '0') + CaretPos = ctrl.selectionStart; + return (CaretPos); +} + +function setCaretPosition(ctrl, pos){ + if(ctrl.setSelectionRange) { + ctrl.focus(); + ctrl.setSelectionRange(pos,pos); + } + else if (ctrl.createTextRange) { + var range = ctrl.createTextRange(); + range.collapse(true); + range.moveEnd('character', pos); + range.moveStart('character', pos); + range.select(); + } +} + +function listNewLineAutocomplete(id) { + var text = document.getElementById(id); + var caretPos = getCaretPosition(text) + var word = getWord(text.value, caretPos); + if (word != null) { + var textBefore = text.value.substring(0, caretPos); + var textAfter = text.value.substring(caretPos, text.length); + $('#' + id).val(textBefore + '\r\n[*] ' + textAfter); + setCaretPosition(text, caretPos + 5); + return true; + } +} + +function string2bb(element) { + if(element == 'bold') return 'b'; + else if(element == 'italic') return 'i'; + else if(element == 'underline') return 'u'; + else if(element == 'overline') return 'o'; + else if(element == 'strike') return 's'; + else if(element == 'superscript') return 'sup'; + else if(element == 'subscript') return 'sub'; + else return element; +} + /** * jQuery plugin 'editor_autocomplete' */ @@ -196,4 +256,72 @@ function submit_form(e) { if(typeof onselect !== 'undefined') a.on('textComplete:select', function(e, value, strategy) { onselect(value); }); }; -})( jQuery );
\ No newline at end of file +})( jQuery ); + +(function( $ ) { + $.fn.bbco_autocomplete = function(type) { + + if(type=='bbcode') { + var open_close_elements = ['bold', 'italic', 'underline', 'overline', 'strike', 'superscript', 'subscript', 'quote', 'code', 'spoiler', 'map', 'nobb', 'list', 'ul', 'ol', 'li', 'table', 'tr', 'th', 'td', 'center', 'color', 'font', 'size', 'zrl', 'zmg', 'rpost', 'qr', 'observer']; + var open_elements = ['observer.baseurl', 'observer.address', 'observer.photo', 'observer.name', 'observer.webname', 'observer.url', '*', 'hr', ]; + + var elements = open_close_elements.concat(open_elements); + } + + if(type=='comanche') { + var open_close_elements = ['region', 'layout', 'template', 'theme', 'widget', 'block', 'menu', 'var', 'css', 'js', 'authored', 'comment', 'webpage']; + var open_elements = []; + + var elements = open_close_elements.concat(open_elements); + } + + if(type=='comanche-block') { + var open_close_elements = ['menu', 'var']; + var open_elements = []; + + var elements = open_close_elements.concat(open_elements); + } + + bbco = { + match: /\[(\w*\**)$/, + search: function (term, callback) { + callback($.map(elements, function (element) { + return element.indexOf(term) === 0 ? element : null; + })); + }, + index: 1, + replace: function (element) { + element = string2bb(element); + if(open_elements.indexOf(element) < 0) { + if(element === 'list' || element === 'ol' || element === 'ul') { + return ['\[' + element + '\]' + '\n\[*\] ', '\n\[/' + element + '\]']; + } + else if(element === 'table') { + return ['\[' + element + '\]' + '\n\[tr\]', '\[/tr\]\n\[/' + element + '\]']; + } + else { + return ['\[' + element + '\]', '\[/' + element + '\]']; + } + } + else { + return '\[' + element + '\] '; + } + } + }; + + this.attr('autocomplete','off'); + var a = this.textcomplete([bbco], {className:'acpopup', zIndex:1020}); + + a.on('textComplete:select', function(e, value, strategy) { value; }); + + a.keypress(function(e){ + e.stopImmediatePropagation(); + if (e.keyCode == 13) { + var x = listNewLineAutocomplete(this.id); + if(x) + e.preventDefault(); + } + }); + }; +})( jQuery ); + |