Newer
Older
dmpopidor / lib / assets / javascripts / utils_es5 / tinymce.js
(function(ctx){
    ctx.tinymce = ctx.tinymce || {};
    var tinymce = ctx.tinymce;
    /*
        Factory to create tinyMCE object that overrides defaults with the given object options passed
        @param options An object with known properties for tinyMCE
        @return A tinyMCE object or throws an error if jQuery is not present when this function is invoked
    */
    tinymce.factory = tinymce.factory || (function(options){
        if($){
            return $.extend(true, {
                    selector: 'textarea.tinymce',
                    statusbar: false,
                    menubar: false,
                    toolbar: 'bold italic | bullist numlist | link | table',
                    plugins: 'table autoresize link paste advlist',
                    advlist_bullet_styles: 'circle,disc,square', //only disc bullets display on htmltoword
                    target_list: false,
                    autoresize_min_height: 130,
                    autoresize_bottom_margin: 10,
                    extended_valid_elements: 'iframe[tooltip] , a[href|target=_blank]',
                    extended_valid_elements: 'a[href|target=_blank]',
                    paste_auto_cleanup_on_paste : true,
                    paste_remove_styles: true,
                    paste_retain_style_properties: 'none',
                    paste_convert_middot_lists: true,
                    paste_remove_styles_if_webkit: true,
                    paste_remove_spans: true,
                    paste_strip_class_attributes: "all",
                    table_default_attributes: {
                        border: 1
                    }
            }, options);
        }
        else
            throw Error('$ is undefined');
    });
    /*
        Initialises a tinymce editor given the object passed. If the object is
        undefined, a default object generated by dmproadmap.utils.tinymce.factory will be used
        @param obj An object with known properties for tinyMCE
    */
    tinymce.init = tinymce.init || (function(options){
        if(window.tinymce){
            //TODO, there is a bug on Firefox when init is executed again after partially refreshing page (e.g. https://github.com/tinymce/tinymce/issues/3763)
            window.tinymce.init(tinymce.factory(options));
        }
    });
    /*
        Finds any tinyMCE editor whose target element/textarea has className passed
        @param className A string representing the class name of the tinyMCE editor target element/textarea to look for
        @return An Array of tinymce.Editor objects
    */
    tinymce.findEditorsByClassName = tinymce.findEditorsByClassName || (function(className){
        if($ && window.tinymce && className){
            return window.tinymce.editors.reduce(function(acc,e){
                if($(e.getElement()).hasClass(className)) 
                    return acc.concat([e]);
                return acc;
            },[]);
        }
        return [];
    });
    /*
        Finds a tinyMCE editor whose target element/textarea has id passed
        @param id A string representing the id of the tinyMCE editor target element/textarea to look for
        @return tinymce.Editor object or undefined if not found
    */
    tinymce.findEditorById = tinymce.findEditorById || (function(id){
        if($ && window.tinymce && id){
            // Usage of Array.prototype.find below is desired, however IE does not support it.
            for(var i=0, l=window.tinymce.editors.length;i<l;i++) {
                if(window.tinymce.editors[i].id === id){
                    return window.tinymce.editors[i];
                }
            }
        }
    });
    /*
        Destroy every editor instance whose target element/textarea has className passed. This method
        executes for each editor the method defined at tinymce.Editor.destroy (e.g. https://www.tinymce.com/docs/api/tinymce/tinymce.editor/#destroy).
        @param className A string representing the class name of the tinyMCE editor target element/textarea to look for
        @return undefined
    */
    tinymce.destroyEditorsByClassName = tinymce.destroyEditorsByClassName || (function(className){
        var editors = tinymce.findEditorsByClassName(className);
        editors.forEach(function(ed){
            ed.destroy();
        });
    });
    /*
        Destroy an editor instance whose target element/textarea has HTML id passed. This method
        executes tinymce.Editor.destroy (e.g. https://www.tinymce.com/docs/api/tinymce/tinymce.editor/#destroy) for a successfull id found.
        @return undefined
    */
    tinymce.destroyEditorById = tinymce.destroyEditorById || (function(id){
        var ed = tinymce.findEditorById(id);
        if(ed)
            ed.destroy();  
    });
})(define('dmproadmap.utils'));