<commit_before>/*global jQuery,  AjaxSolr, EUMSSI,  UTIL, CKEDITOR, FilterManager */
(function ($) {

	AjaxSolr.RichEditorWidget = AjaxSolr.AbstractTextWidget.extend({

		start:0,	//Reset the pagination with doRequest on this Widget

		init: function () {
			this.$target = $(this.target);
			if (CKEDITOR.env.ie && CKEDITOR.env.version < 9){
				CKEDITOR.tools.enableHtml5Elements(document);
			}

			CKEDITOR.on( 'instanceReady', function( ev ) {
				this._resizeEditor(ev.editor);
			}.bind(this));

			_.delay(function(){
				this._initCKEditor(this.$target.prop("id"));
			}.bind(this), 500);
		},

		/**
		 * Initializes the CKEditor
		 * @param {string} editorId
		 * @private
		 */
		_initCKEditor : function(editorId){
			// The trick to keep the editor in the sample quite small
			// unless user specified own height.
			CKEDITOR.config.width = 'auto';

			CKEDITOR.config.extraPlugins = 'embed,autoembed';

			//Toolbar configuration
			CKEDITOR.config.removeButtons = 'Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,CreateDiv,Language,Scayt,SelectAll,Anchor,Flash,Smiley,Iframe,Maximize,About';

			var wysiwygareaAvailable = this._isWysiwygareaAvailable(),
				isBBCodeBuiltIn = !!CKEDITOR.plugins.get('bbcode');

			var editorElement = CKEDITOR.document.getById(editorId);

			// :(((
			if (isBBCodeBuiltIn) {
				editorElement.setHtml(
					'Hello world!\n\n' +
					'I\'m an instance of [url=http://ckeditor.com]CKEditor[/url].'
				);
			}

			// Depending on the wysiwygare plugin availability initialize classic or inline editor.
			if (wysiwygareaAvailable) {
				CKEDITOR.replace(editorId);
			} else {
				editorElement.setAttribute('contenteditable', 'true');
				CKEDITOR.inline(editorId);

				// TODO we can consider displaying some info box that
				// without wysiwygarea the classic editor may not work.
			}

			this._addCustomButton();
			this._addDeleteHighLightButton();

			this._setDroppable();

			$(window).resize(this._resizeEditor);
		},

		_isWysiwygareaAvailable : function(){
			// If in development mode, then the wysiwygarea must be available.
			// Split REV into two strings so builder does not replace it :D.
			if (CKEDITOR.revision == ( '%RE' + 'V%' )) {
				return true;
			}

			return !!CKEDITOR.plugins.get('wysiwygarea');
		},

		_setDroppable : function(){
			this.$target.parent().droppable({
				activeClass: "ui-droppable-active",
				hoverClass: "ui-droppable-hover",
				scope: "editorDrop",
				tolerance: "pointer",
				drop: function( event, ui ) {
					var oEditor = CKEDITOR.instances["richeditor-placeholder"];
					oEditor.insertHtml( ui.draggable.html() );
				}
			});
		},

		clearSimilarityFilter: function() {
			EUMSSI.FilterManager.removeFilterByName(FilterManager.NAMES.SIMILARITY, this.id, true);
		},

		_addCustomButton : function(){
			var editor = CKEDITOR.instances["richeditor-placeholder"];
			editor.addCommand("myEumssiSearch", { // create named command
				exec: function(edt) {
					var text;
					var filterText;
					var query;
					var selectedText = edt.getSelection().getSelectedText();
					if (selectedText === "") {
						var body = edt.document.getBody();
						text =  body.getText();
						this._getSuggestedQuery(text);
					} else {
						this._getSuggestedQuery(selectedText);
						text = selectedText;
					}
					filterText = FilterManager.NAMES.SIMILARITY;
					query = filterText + ":" + text;
					this.clearSimilarityFilter();
					EUMSSI.FilterManager.addFilter(filterText, query, this.id, filterText);
					this._unHightlight();
					this.doRequest();
				}.bind(this)
			});

			editor.ui.addButton('btnEumssiSearch', { // add new button and bind our command
				label: "Get Related Content",
				title: "Uses the selected text on EUMSSI engine to obtain filter suggestions",
				command: 'myEumssiSearch',
				icon: '../../images/favicon-2.png'
			});

		},

		_addDeleteHighLightButton: function() {
			var editor = CKEDITOR.instances["richeditor-placeholder"];
			editor.addCommand("deleteHighLigh", {
				exec: function() {
					this._unHightlight();
					$(".kea-tags-container").empty().hide();
				}.bind(this)
			});

			editor.ui.addButton('btnEumssiDeleteHighlight', { // add new button and bind our command
				label: "Delete highlight text",
				title: "Delete highlight text",
				command: 'deleteHighLigh',
				icon: '../../images/favicon-2.png'
			});
		},

		_highlightEntities: function(entities) {
			entities.forEach(this._highlightEntity.bind(this));
		},

		_highlightEntity: function(entity) {
			var keyword = entity.text;
			var body = this._getContextHightlight();
			var opts = this._getDefaultsOptionsHightlight();
			opts.each = this._onEachHighlight.bind(this, entity);
			$(body).mark(keyword, opts);
		},

		_onEachHighlight: function(entity, dom) {
			$(dom).on('click', this._onClickMarkHighlight.bind(this, entity));
		},

		_unHightlight: function() {
			var body = this._getContextHightlight();
			var opts = this._getDefaultsOptionsHightlight();
			$(body).unmark(opts);
		},

		_setMenuEvents: function(entity) {
			this.$contentMenu.on("click", ".filter", UTIL.addPersonFilter.bind(this, entity.text));
			this.$contentMenu.on("click", ".filter-clear", UTIL.cleanPersonFilter.bind(this));
			this.$contentMenu.on("click", ".filter-country", UTIL.addContryFilter.bind(this, entity.text));
			this.$contentMenu.on("click", ".filter-country-clear", UTIL.cleanCountryFilter.bind(this, true));
			this.$contentMenu.on("click", ".filter-city", UTIL.addCityFilter.bind(this, entity.text));
			this.$contentMenu.on("click", ".filter-city-clear", UTIL.cleanCityFilter.bind(this, true));
			this.$contentMenu.on("click", ".filter-location", UTIL.addLocationFilter.bind(this, entity.text));
			this.$contentMenu.on("click", ".filter-location-clear", UTIL.cleanLocationFilter.bind(this, true));
		},

		_onClickMarkHighlight: function(entity, event) {
			if (this.$contentMenu) {
				this.$contentMenu.remove();
			}
			this.$contentMenu = UTIL.getMarkerMenu(entity, this.id);
			this._setMenuEvents(entity);
			var offset = $("#richeditor-placeholder").next("div").find("iframe").offset();
			var $body = $(event.currentTarget).closest("body");
			offset.top -= $body.scrollTop();
			offset.left -=  $body.scrollLeft();
			EUMSSI.UTIL.showMarkMenu(this.$contentMenu, $(event.currentTarget), offset);
			console.log($(event.currentTarget).html(), entity);
		},

		_getContextHightlight: function() {
			var ckeditor = CKEDITOR.instances["richeditor-placeholder"];
			var document = ckeditor.document.$;
			return document.querySelector("body");
		},

		_getDefaultsOptionsHightlight: function() {
			return {
				exclude: ["img", "iframe"],
				className: "highlight"<commit_msg>fix dbpedia filter in editor<commit_after>/*global jQuery,  AjaxSolr, EUMSSI,  UTIL, CKEDITOR, FilterManager */
(function ($) {

	AjaxSolr.RichEditorWidget = AjaxSolr.AbstractTextWidget.extend({

		start:0,	//Reset the pagination with doRequest on this Widget

		init: function () {
			this.$target = $(this.target);
			if (CKEDITOR.env.ie && CKEDITOR.env.version < 9){
				CKEDITOR.tools.enableHtml5Elements(document);
			}

			CKEDITOR.on( 'instanceReady', function( ev ) {
				this._resizeEditor(ev.editor);
			}.bind(this));

			_.delay(function(){
				this._initCKEditor(this.$target.prop("id"));
			}.bind(this), 500);
		},

		/**
		 * Initializes the CKEditor
		 * @param {string} editorId
		 * @private
		 */
		_initCKEditor : function(editorId){
			// The trick to keep the editor in the sample quite small
			// unless user specified own height.
			CKEDITOR.config.width = 'auto';

			CKEDITOR.config.extraPlugins = 'embed,autoembed';

			//Toolbar configuration
			CKEDITOR.config.removeButtons = 'Form,Checkbox,Radio,TextField,Textarea,Select,Button,ImageButton,HiddenField,CreateDiv,Language,Scayt,SelectAll,Anchor,Flash,Smiley,Iframe,Maximize,About';

			var wysiwygareaAvailable = this._isWysiwygareaAvailable(),
				isBBCodeBuiltIn = !!CKEDITOR.plugins.get('bbcode');

			var editorElement = CKEDITOR.document.getById(editorId);

			// :(((
			if (isBBCodeBuiltIn) {
				editorElement.setHtml(
					'Hello world!\n\n' +
					'I\'m an instance of [url=http://ckeditor.com]CKEditor[/url].'
				);
			}

			// Depending on the wysiwygare plugin availability initialize classic or inline editor.
			if (wysiwygareaAvailable) {
				CKEDITOR.replace(editorId);
			} else {
				editorElement.setAttribute('contenteditable', 'true');
				CKEDITOR.inline(editorId);

				// TODO we can consider displaying some info box that
				// without wysiwygarea the classic editor may not work.
			}

			this._addCustomButton();
			this._addDeleteHighLightButton();

			this._setDroppable();

			$(window).resize(this._resizeEditor);
		},

		_isWysiwygareaAvailable : function(){
			// If in development mode, then the wysiwygarea must be available.
			// Split REV into two strings so builder does not replace it :D.
			if (CKEDITOR.revision == ( '%RE' + 'V%' )) {
				return true;
			}

			return !!CKEDITOR.plugins.get('wysiwygarea');
		},

		_setDroppable : function(){
			this.$target.parent().droppable({
				activeClass: "ui-droppable-active",
				hoverClass: "ui-droppable-hover",
				scope: "editorDrop",
				tolerance: "pointer",
				drop: function( event, ui ) {
					var oEditor = CKEDITOR.instances["richeditor-placeholder"];
					oEditor.insertHtml( ui.draggable.html() );
				}
			});
		},

		clearSimilarityFilter: function() {
			EUMSSI.FilterManager.removeFilterByName(FilterManager.NAMES.SIMILARITY, this.id, true);
		},

		_addCustomButton : function(){
			var editor = CKEDITOR.instances["richeditor-placeholder"];
			editor.addCommand("myEumssiSearch", { // create named command
				exec: function(edt) {
					var text;
					var filterText;
					var query;
					var selectedText = edt.getSelection().getSelectedText();
					if (selectedText === "") {
						var body = edt.document.getBody();
						text =  body.getText();
						this._getSuggestedQuery(text);
					} else {
						this._getSuggestedQuery(selectedText);
						text = selectedText;
					}
					filterText = FilterManager.NAMES.SIMILARITY;
					query = filterText + ":" + text;
					this.clearSimilarityFilter();
					EUMSSI.FilterManager.addFilter(filterText, query, this.id, filterText);
					this._unHightlight();
					this.doRequest();
				}.bind(this)
			});

			editor.ui.addButton('btnEumssiSearch', { // add new button and bind our command
				label: "Get Related Content",
				title: "Uses the selected text on EUMSSI engine to obtain filter suggestions",
				command: 'myEumssiSearch',
				icon: '../../images/favicon-2.png'
			});

		},

		_addDeleteHighLightButton: function() {
			var editor = CKEDITOR.instances["richeditor-placeholder"];
			editor.addCommand("deleteHighLigh", {
				exec: function() {
					this._unHightlight();
					$(".kea-tags-container").empty().hide();
				}.bind(this)
			});

			editor.ui.addButton('btnEumssiDeleteHighlight', { // add new button and bind our command
				label: "Delete highlight text",
				title: "Delete highlight text",
				command: 'deleteHighLigh',
				icon: '../../images/favicon-2.png'
			});
		},

		_highlightEntities: function(entities) {
			entities.forEach(this._highlightEntity.bind(this));
		},

		_highlightEntity: function(entity) {
			var keyword = entity.text;
			var body = this._getContextHightlight();
			var opts = this._getDefaultsOptionsHightlight();
			opts.each = this._onEachHighlight.bind(this, entity);
			$(body).mark(keyword, opts);
		},

		_onEachHighlight: function(entity, dom) {
			$(dom).on('click', this._onClickMarkHighlight.bind(this, entity));
		},

		_unHightlight: function() {
			var body = this._getContextHightlight();
			var opts = this._getDefaultsOptionsHightlight();
			$(body).unmark(opts);
		},

		_setMenuEvents: function(entity) {
			this.$contentMenu.on("click", ".filter", UTIL.addPersonFilter.bind(this, entity.value));
			this.$contentMenu.on("click", ".filter-clear", UTIL.cleanPersonFilter.bind(this));
			this.$contentMenu.on("click", ".filter-country", UTIL.addContryFilter.bind(this, entity.value));
			this.$contentMenu.on("click", ".filter-country-clear", UTIL.cleanCountryFilter.bind(this, true));
			this.$contentMenu.on("click", ".filter-city", UTIL.addCityFilter.bind(this, entity.value));
			this.$contentMenu.on("click", ".filter-city-clear", UTIL.cleanCityFilter.bind(this, true));
			this.$contentMenu.on("click", ".filter-location", UTIL.addLocationFilter.bind(this, entity.value));
			this.$contentMenu.on("click", ".filter-location-clear", UTIL.cleanLocationFilter.bind(this, true));
		},

		_onClickMarkHighlight: function(entity, event) {
			if (this.$contentMenu) {
				this.$contentMenu.remove();
			}
			this.$contentMenu = UTIL.getMarkerMenu(entity, this.id);
			this._setMenuEvents(entity);
			var offset = $("#richeditor-placeholder").next("div").find("iframe").offset();
			var $body = $(event.currentTarget).closest("body");
			offset.top -= $body.scrollTop();
			offset.left -=  $body.scrollLeft();
			EUMSSI.UTIL.showMarkMenu(this.$contentMenu, $(event.currentTarget), offset);
			console.log($(event.currentTarget).html(), entity);
		},

		_getContextHightlight: function() {
			var ckeditor = CKEDITOR.instances["richeditor-placeholder"];
			var document = ckeditor.document.$;
			return document.querySelector("body");
		},

		_getDefaultsOptionsHightlight: function() {
			return {
				exclude: ["img", "iframe"],
				className: "highlight"
{'fraction_non_alphanumeric': 0.15077310054006066, 'fraction_numerical': 0.0011836946067914477, 'mean_word_length': 3.9031556039173014, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3688258', 'n_tokens_mistral': 5185, 'n_tokens_neox': 4238, 'n_words': 801}