/** * TinyMceController class * * @author Aivars Žukovskis * @category TinyMceController */ var TinyMceController = (function() { function TinyMceController(jqObj, vAdditionalCfg) { var _this = this; $.extend(_this.vAdditionalCfg, defaultVal(vAdditionalCfg, {})); _this.jqModuleObj = $.isDefined(jqObj) ? jqObj : $(".form-data"); _this.oBlank = CBase.jsModule.oTable.oBlank; _this.bindEvents(); } TinyMceController.prototype.jqModuleObj = {}; TinyMceController.prototype.oBlank = {}; TinyMceController.prototype.vAdditionalCfg = {}; /** * Function to bind tinyMceController base events * * @author Aivars Žukovskis * @category TinyMceController */ TinyMceController.prototype.bindEvents = function(){ var _this = this; // Load TinyMceController if($.fn.tinymce){ _this.addTinyMceControl(); // --------------------------------------------------------------------------------------------------------- // BIND ALL IMAGE DATA HANDLERS // --------------------------------------------------------------------------------------------------------- // Sortable; _this.jqModuleObj.find(".prod-img > ul").sortable({distance: 10, opacity: 0.6, cancel : ".click-to-addimg", tolerance: "pointer"}); // Bind insert youtube link _this.jqModuleObj.find(".prod-img li .youtube") .css({opacity : 0}) .click(function(){ _this.addFileToTinyMceTextField($(this).closest("li"), "insertYoutubeImg"); }); // Bind insert simple image; _this.jqModuleObj.find(".prod-img li .pic") .css({opacity : 0}) .click(function(){ _this.addFileToTinyMceTextField($(this).closest("li"), "insertFile"); }); // Bind insert download link; _this.jqModuleObj.find(".prod-img li .download") .css({opacity : 0}) .click(function(){ _this.insertDownloadLinkInTinyMceTextField($(this).closest("li")); }); // Bind insert inline fancybox element _this.jqModuleObj.find(".prod-img li .inline") .css({opacity : 0}) .click(function(){ _this.insertInlineElementInTinyMceTextField($(this).closest("li")); }); // Bind hover functions; _this.jqModuleObj.find(".prod-img li").bind({ mouseenter : function(){ $(this).find(".delete, .youtube, .pic, .download, .link").stop(true, true).fadeTo(200, 0.8); }, mouseleave : function(){ $(this).find(".delete, .youtube, .pic, .download, .link").stop(true, true).fadeTo(200, 0); } }); }else{ pr("TinyMceController js not loaded!"); } }; /** * Base Function to add tinyMceController to text field * * @author Aivars Žukovskis * @category TinyMceController */ TinyMceController.prototype.addTinyMceControl = function(){ var _this = this; var iInitiated = 0, iTextAreaCount = $('textarea.tinymce').length; var vAdditionalCfg = $.extend(true, {}, _this.vAdditionalCfg); CBase.oForm.disableShow = true; CBase.ajaxLoader.disabledFadeOut = true; //Init tinyMce v5.0.0 tinymce.remove(); var vBaseCfg = { selector: 'textarea.tinymce', // Select all textareas with class tinymce height: 450, // set the heigh of element // Premium plugins powerpaste plugins: [ 'advlist anchor autolink', 'code codesample charmap', 'directionality', 'fullscreen', 'help hr', 'image insertdatetime imagetools', 'lists link', 'media', 'nonbreaking', 'paste pagebreak print preview', 'searchreplace', 'table template textpattern toc', 'visualblocks visualchars', 'wordcount' ], toolbar: 'undo redo | formatselect | fontsizeselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify | numlist bullist outdent indent | removeformat | code', menubar: false, statusbar: false, resize: true, //skin: "oxide", skin: "oxide-dark", content_css : '/css/cms-tinymce.css?V=4', setup: function (editor) { editor.on('init', function() { iInitiated++; // The Editor has initialized - upon last loaded show form if(iInitiated == iTextAreaCount) { CBase.ajaxLoader.hideLoader(true); CBase.oForm.disableShow = false; CBase.oForm.show(); } }); editor.on('blur', function () { tinyMCE.activeEditor.save(); }); editor.on('focus', function () { CBase.oForm.jqBlank.find("#"+editor.id).parent().find(".tox-tinymce").addClass("tox-tinymce-focus"); }); editor.on('blur', function () { CBase.oForm.jqBlank.find("#"+editor.id).parent().find(".tox-tinymce").removeClass("tox-tinymce-focus"); }); // Bind save shortcuts // "meta" maps to Command on Mac and Ctrl on PC editor.shortcuts.add('meta+s', "Save form data", function() { tinyMCE.activeEditor.save(); CBase.oTable.oBlank.saveBlankView(); }); if($("textarea#" + editor.id).is(":disabled")) editor.setMode('readonly'); }, valid_children: "+a[div|span|img],+div[figure],+span[*]" }; if($.isFunction(vAdditionalCfg.setup)){ var fCustomSetup = vAdditionalCfg.setup; var fBaseSetup = vBaseCfg.setup; vBaseCfg.setup = function(editor){ fBaseSetup(editor); fCustomSetup(editor); }; delete vAdditionalCfg.setup; } tinymce.init($.extend(true, {}, vBaseCfg, vAdditionalCfg)); }; /** * Function to insert uploaded images in tinyMceController text field * * @author Aivars Žukovskis * @category TinyMceController */ TinyMceController.prototype.addFileToTinyMceTextField = function(jqCaller, sCase){ var _this = this; var $link = jqCaller.find("input#img").val(); var re = /(?:\.([^.]+))?$/; var ext = re.exec($link)[1]; switch(sCase) { case "insertFile": if(CBase.dt.prevExt.indexOf(ext) != -1) tinymce.activeEditor.execCommand('mceInsertContent', false, ' ' ); break; case "insertYoutubeImg": if(CBase.dt.prevExt.indexOf(ext) != -1) { let videoId = _this.sanitizeVideoId(window.prompt('Enter YouTube video ID', '')); if (!videoId) break; let div = '
' + '
' + '
' + '
' + '
' + '' + '' + '' + '
' + '' + '
' + '
' + '
' + '
' + '
'; tinymce.activeEditor.execCommand('mceInsertContent', false, div); } break; } }; /** * Function to insert download link for tinyMceController text field * * @author Aivars Žukovskis * @category TinyMceController */ TinyMceController.prototype.insertDownloadLinkInTinyMceTextField = function(jqObj){ CBase.jsModule.oTable.sendAjaxActionAdvanced({ action : "getFileLink", link : jqObj.find("input#img").val(), file_id : jqObj.find("input#img_id").val() }, function(d){ if(d && d.jsonok){ tinymce.activeEditor.execCommand('mceInsertContent', false, ' File title goes here... ' ); jqObj.find("input#img_id").val(d.id); } }); }; /** * Function to insert inline a element * * @author Aivars Žukovskis * @category TinyMceController */ TinyMceController.prototype.insertInlineElementInTinyMceTextField = function(jqCaller){ var $link = jqCaller.find("input#img").val(); var re = /(?:\.([^.]+))?$/; var ext = re.exec($link)[1]; if(CBase.dt.prevExt.indexOf(ext) != -1) tinymce.activeEditor.execCommand('mceInsertContent', false, 'Click me!'); }; TinyMceController.prototype.sanitizeVideoId = function (value) { return String(value || '').replace(/[^a-zA-Z0-9_-]/g, ''); }; TinyMceController.prototype.insertTopLevelBlock = function (html) { var editor = this.editor || tinymce.activeEditor; if (!editor) return; var body = editor.getBody(); var doc = editor.getDoc(); var buffer = doc.createElement('div'); var anchor = editor.selection.getNode(); // Selected node var lastInsertedNode = null; buffer.innerHTML = html; while (anchor && anchor.parentNode !== body) { anchor = anchor.parentNode; } editor.undoManager.transact(function () { if (anchor && anchor.parentNode === body) { var nextSibling = anchor.nextSibling; while (buffer.firstChild) { lastInsertedNode = buffer.firstChild; body.insertBefore(lastInsertedNode, nextSibling); } } else { while (buffer.firstChild) { lastInsertedNode = buffer.firstChild; body.appendChild(lastInsertedNode); } } }); editor.focus(); if (lastInsertedNode) { var range = editor.dom.createRng(); range.setStartAfter(lastInsertedNode); range.setEndAfter(lastInsertedNode); editor.selection.setRng(range); } else { editor.selection.select(body, true); editor.selection.collapse(false); } editor.nodeChanged(); }; TinyMceController.prototype.buildUid = function (prefix) { return prefix + '-' + Date.now().toString(36) + Math.random().toString(36).slice(2, 7); }; return TinyMceController; })();