From 98b62b80f3f08310e86b9bc4d9740526c50080c2 Mon Sep 17 00:00:00 2001
From: rabuzarus <>
Date: Thu, 14 Apr 2016 23:59:29 +0200
Subject: [PATCH] rework autocomplete: seperate bbcode completion

---
 js/autocomplete.js                           | 122 +++++++++++++++++--
 js/main.js                                   |   2 +
 view/templates/event_head.tpl                |   3 +
 view/templates/jot-header.tpl                |   1 +
 view/templates/msg-header.tpl                |   5 +
 view/theme/smoothly/templates/jot-header.tpl |   1 +
 view/theme/vier/templates/event_form.tpl     |   2 +-
 7 files changed, 124 insertions(+), 12 deletions(-)

diff --git a/js/autocomplete.js b/js/autocomplete.js
index f31161ff83..053cc9e3aa 100644
--- a/js/autocomplete.js
+++ b/js/autocomplete.js
@@ -111,15 +111,70 @@ 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 return element;
+}
+
 /**
  * jQuery plugin 'editor_autocomplete'
  */
 (function( $ ) {
 	$.fn.editor_autocomplete = function(backend_url) {
 
-		// list of supported bbtags
-		var bbelements = ['b', 'u', 'i', 'img', 'url', 'quote', 'code', 'spoiler', 'audio', 'video', 'youtube', 'map', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 's', 'o', 'list', 'center', 'nosmile', 'vimeo' ];
-
 		// Autocomplete contacts
 		contacts = {
 			match: /(^|\s)(@\!*)([^ \n]+)$/,
@@ -138,15 +193,8 @@ function submit_form(e) {
 			replace: function(item) { return "$1" + item.text + ' '; },
 		};
 
-		// Autocomplete BBTags
-		bbtags = {
-			match: /\[(\w*)$/,
-			index: 1,
-			search: function (term, callback) { callback($.map(bbelements, function (element) { return element.indexOf(term) === 0 ? element : null; })); },
-			replace: function (element) { return ['[' + element + ']', '[/' + element + ']']; },
-		};
 		this.attr('autocomplete','off');
-		this.textcomplete([contacts,smilies, bbtags], {className:'acpopup', zIndex:1020});
+		this.textcomplete([contacts,smilies], {className:'acpopup', zIndex:1020});
 	};
 })( jQuery );
 
@@ -229,6 +277,58 @@ function submit_form(e) {
 	};
 })( jQuery );
 
+(function( $ ) {
+	$.fn.bbco_autocomplete = function(type) {
+
+		if(type=='bbcode') {
+			var open_close_elements = ['bold', 'italic', 'underline', 'overline', 'strike', 'quote', 'code', 'spoiler', 'map', 'nobb', 'list', 'ul', 'ol', 'li', 'table', 'tr', 'th', 'td', 'center', 'color', 'font', 'size'];
+			var open_elements = ['*', 'hr'];
+
+			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 );
 
 /**
  * Friendica people autocomplete legacy
diff --git a/js/main.js b/js/main.js
index 1ed0c4b6af..7e1c22ecfc 100644
--- a/js/main.js
+++ b/js/main.js
@@ -495,6 +495,8 @@
 			}
 			/* autocomplete @nicknames */
 			$(".comment-edit-form  textarea").editor_autocomplete(baseurl+"/acl");
+			/* autocomplete bbcode */
+ +			$(".comment-edit-form  textarea").bbco_autocomplete('bbcode');
 
 			// setup videos, since VideoJS won't take care of any loaded via AJAX
 			if(typeof videojs != 'undefined') videojs.autoSetup();
diff --git a/view/templates/event_head.tpl b/view/templates/event_head.tpl
index 745327e992..de5ad6070c 100644
--- a/view/templates/event_head.tpl
+++ b/view/templates/event_head.tpl
@@ -148,6 +148,9 @@
 
 
 	$(document).ready(function() { 
+		{{if $editselect = 'none'}}
+		$("#comment-edit-text-desc").bbco_autocomplete('bbcode');
+		{{/if}}
 
 		$('#event-share-checkbox').change(function() {
 
diff --git a/view/templates/jot-header.tpl b/view/templates/jot-header.tpl
index d12293f04c..7498bfc422 100644
--- a/view/templates/jot-header.tpl
+++ b/view/templates/jot-header.tpl
@@ -24,6 +24,7 @@ function initEditor(cb){
 			$("#profile-jot-text-loading").hide();
 			$("#profile-jot-text").css({ 'height': 200, 'color': '#000' });
 			$("#profile-jot-text").editor_autocomplete(baseurl+"/acl");
+			$("#profile-jot-text").bbco_autocomplete('bbcode');
 			editor = true;
 			$("a#jot-perms-icon").colorbox(colorbox_options);
 			$(".jothidden").show();
diff --git a/view/templates/msg-header.tpl b/view/templates/msg-header.tpl
index 9b1a92ef5f..ca91923524 100644
--- a/view/templates/msg-header.tpl
+++ b/view/templates/msg-header.tpl
@@ -63,6 +63,11 @@ else
 			}
 		);
 
+		{{if $editselect = 'none'}}
+		$("#prvmail-text").bbco_autocomplete('bbcode');
+		{{/if}}
+
+
 	});
 
 	function jotGetLink() {
diff --git a/view/theme/smoothly/templates/jot-header.tpl b/view/theme/smoothly/templates/jot-header.tpl
index 8b2666f0f3..880764da00 100644
--- a/view/theme/smoothly/templates/jot-header.tpl
+++ b/view/theme/smoothly/templates/jot-header.tpl
@@ -13,6 +13,7 @@ function initEditor(cb){
 			$("#profile-jot-text-loading").hide();
             		$("#profile-jot-text").css({ 'height': 200, 'color': '#000' });
 			$("#profile-jot-text").editor_autocomplete(baseurl+"/acl");
+			$("#profile-jot-text").bbco_autocomplete('bbcode');
             		$(".jothidden").show();
             		editor = true;
             		$("a#jot-perms-icon").colorbox({
diff --git a/view/theme/vier/templates/event_form.tpl b/view/theme/vier/templates/event_form.tpl
index 165234c262..4f3240de8e 100644
--- a/view/theme/vier/templates/event_form.tpl
+++ b/view/theme/vier/templates/event_form.tpl
@@ -34,7 +34,7 @@
 
 
 <div id="event-desc-text">{{$d_text}}</div>
-<textarea id="comment-edit-text-desc" rows="8" cols="64" name="desc">{{$d_orig}}</textarea>
+<textarea id="comment-edit-text-desc" rows="8" cols="64" name="desc" autocomplete="off">{{$d_orig}}</textarea>
 <div id="event-desc-text-edit-bb" class="comment-edit-bb">
 	<a title="{{$edimg}}" data-role="insert-formatting" data-comment="{{$comment}}" data-bbcode="img" data-id="desc"><i class="icon-picture"></i></a>      
 	<a title="{{$edurl}}" data-role="insert-formatting" data-comment="{{$comment}}" data-bbcode="url" data-id="desc"><i class="icon-link"></i></a>
-- 
2.39.5