plugin.min.js 4.11 KB
/**
 * plugin.js
 * 
 * Copyright, Dmitry A Karasev (dima at karasev dot pro)
 * 
 */

/*global tinymce:true */

tinymce.PluginManager.add("fieldsetcontrols", function(editor) {
    
    editor.addButton("addfieldset", {
        text: "+FieldSet",
        icon: false,
        tooltip: "Insert/edit fieldset",
        onclick: function() {

            var data = {};
            var selection = editor.selection;
            var dom = editor.dom;
            var selectedElm, anchorElm;

            selectedElm = selection.getNode();
            anchorElm = dom.getParent(selectedElm,"fieldset");

            data.class = "";
            data.legend = "";
            if(anchorElm) {
                data.class = anchorElm.className;
                if(anchorElm.firstChild) {
                    if(anchorElm.firstChild.nodeName=="LEGEND") {
                        data.legend = anchorElm.firstChild.innerText;
                    }
                }
            }

            editor.windowManager.open({
                title: "Insert/edit fieldset",
                data: data,
                body: [
                    { type: "textbox", name: "legend", label: "Legend" },
                    { type: "listbox", name: "class", label: "Class", values: [{text:"None",value:""},{text:"spoiler",value:"spoiler"}] }
                ],
                width: 600,
                height: 130,
                onsubmit: function(e) {
                    data = tinymce.extend(data,e.data);
                    data.legend = data.legend ? data.legend : "Legend";
                    var fieldsetAttrs = {
                        class: data.class ? data.class : null
                    };
                    if(anchorElm) {
                        editor.focus();
                        dom.setAttribs(anchorElm,fieldsetAttrs);
                        if(anchorElm.firstChild) {
                            if(anchorElm.firstChild.nodeName=="LEGEND") {
                                anchorElm.firstChild.innerText = data.legend;
                            }
                            else {
                                anchorElm.insertBefore(dom.create("legend",null,data.legend),anchorElm.firstChild);
                            }
                        }
                        else {
                            dom.add(anchorElm,"legend",null,data.legend);
                            dom.add(anchorElm,"p",null," ");
                        }
                    }
                    else {
                        editor.insertContent(dom.createHTML("fieldset",fieldsetAttrs,"<legend>"+data.legend+"</legend>"+selection.getContent({format:"html"})+"<p>&nbsp;</p>")+"<p>&nbsp;</p>");
                    }
                }
            });
        },
        stateSelector: "fieldset"
    });

    editor.addButton("delfieldset", {
        text: "-FieldSet",
        icon: false,
        tooltip: "Remove fieldset",
        onclick: function() {
            var data = {};
            var selection = editor.selection;
            var dom = editor.dom;
            var selectedElm, anchorElm;

            selectedElm = selection.getNode();
            anchorElm = dom.getParent(selectedElm,"fieldset");

            if(anchorElm) {
                editor.focus();
                if(dom.isEmpty(selectedElm) && anchorElm.lastChild==selectedElm) {
                    if(anchorElm.firstChild!=selectedElm) {
                        dom.insertAfter(selectedElm,anchorElm);
                        selection.setCursorLocation(selectedElm,0);
                    }
                    else {
                        if(anchorElm.firstChild.nodeName=="LEGEND") {
                            dom.rename(anchorElm.firstChild,"p");
                        }
                        dom.remove(anchorElm,true);
                    }
                }
                else if(anchorElm.firstChild) {
                    if(anchorElm.firstChild.nodeName=="LEGEND") {
                        dom.rename(anchorElm.firstChild,"p");
                    }
                    dom.remove(anchorElm,true);
                }
            }
        },
        stateSelector: "fieldset"
    });
    
});