edittable.js 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * Created with JetBrains PhpStorm.
 * User: xuheng
 * Date: 12-12-19
 * Time: 下午4:55
 * To change this template use File | Settings | File Templates.
 */
(function () {
    var title = $G("J_title"),
        caption = $G("J_caption"),
        autoSizeContent = $G("J_autoSizeContent"),
        autoSizePage = $G("J_autoSizePage"),
13
        tone = $G("J_tone"),
14 15 16 17 18 19 20
        preview = $G("J_preview");

    var editTable = function () {
        this.init();
    };
    editTable.prototype = {
        init:function () {
21 22 23 24 25 26 27 28
            var me = this,
                colorPiker = new UE.ui.ColorPicker({
                    editor:editor
                }),
                colorPop = new UE.ui.Popup({
                    editor:editor,
                    content:colorPiker
                });
29 30 31 32 33 34

            title.checked = editor.queryCommandState("inserttitle") == -1;
            caption.checked = editor.queryCommandState("insertcaption") == -1;

            me.createTable(title.checked, caption.checked);
            me.setAutoSize();
35
            me.setColor();
36 37 38 39 40

            domUtils.on(title, "click", me.titleHanler);
            domUtils.on(caption, "click", me.captionHanler);
            domUtils.on(autoSizeContent, "click", me.autoSizeContentHanler);
            domUtils.on(autoSizePage, "click", me.autoSizePageHanler);
41 42 43 44 45 46 47 48 49 50 51

            domUtils.on(tone, "click", function () {
                colorPop.showAnchor(tone);
            });
            domUtils.on(document, 'mousedown', function () {
                colorPop.hide();
            });
            colorPiker.addListener("pickcolor", function () {
                tone.value = arguments[1];
                colorPop.hide();
            });
52 53 54
        },

        createTable:function (hasCaption, hasTitle) {
55
            var arr = [];
56 57
            arr.push("<table id='J_example'>");
            if (hasCaption) {
58
                arr.push("<caption>" + lang.captionName + "</caption>")
59 60 61
            }
            if (hasTitle) {
                arr.push("<tr>");
62 63
                for (var j = 0; j < 5; j++) {
                    arr.push("<th>" + lang.titleName + "</th>")
64 65 66 67 68
                }
                arr.push("</tr>");
            }
            for (var i = 0; i < 6; i++) {
                arr.push("<tr>");
69 70
                for (var k = 0; k < 5; k++) {
                    arr.push("<td>" + lang.cellsName + "</td>")
71 72 73 74
                }
                arr.push("</tr>");
            }
            arr.push("</table>");
75
            preview.innerHTML = arr.join("");
76 77 78 79 80 81
        },

        titleHanler:function () {
            var example = $G("J_example");
            if (title.checked) {
                var row = document.createElement("tr");
82 83
                row.innerHTML = "<th>" + lang.titleName + "</th><th>" + lang.titleName + "</th><th>" + lang.titleName + "</th><th>"
                    + lang.titleName + "</th><th>" + lang.titleName + "</th>";
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                example.insertBefore(row, example.firstChild);
            } else {
                domUtils.remove(example.rows[0]);
            }
        },
        captionHanler:function () {
            var example = $G("J_example");
            if (caption.checked) {
                var row = document.createElement('caption');
                row.innerHTML = lang.captionName;
                example.insertBefore(row, example.firstChild);
            } else {
                domUtils.remove(domUtils.getElementsByTagName(example, 'caption')[0]);
            }
        },
        autoSizeContentHanler:function () {
            var example = $G("J_example");
            example.removeAttribute("width");
        },
        autoSizePageHanler:function () {
            var example = $G("J_example");
            var tds = example.getElementsByTagName(example, "td");
            utils.each(tds, function (td) {
                td.removeAttribute("width");
            });
            example.setAttribute('width', '100%');
        },
111

112
        setColor:function () {
113
            var start = editor.selection.getStart(),
114 115
                color = domUtils.findParentByTagName(start, "td", true);
            tone.value = domUtils.getComputedStyle(color, "border-color");
116 117 118
        },
        setAutoSize:function () {
            var me = this,
119
                start = editor.selection.getStart(),
120 121 122
                wt = domUtils.findParentByTagName(start, "table", true).width,
                flag = !wt;
            if (wt && !(/%/.test(wt)))   return;
123 124
            if (flag) {
                autoSizeContent.checked = flag;
125
                me.autoSizeContentHanler();
126 127
            } else {
                autoSizePage.checked = !flag;
128 129 130 131 132 133 134
                me.autoSizePageHanler();
            }
        }
    };

    new editTable;

135
    function adaptByTextTable() {
136 137 138
        editor.execCommand("adaptbywindow");
        editor.execCommand('adaptbytext');
    }
139

140 141 142
    dialog.onok = function () {
        title.checked ? editor.execCommand("inserttitle") : editor.execCommand("deletetitle");
        caption.checked ? editor.execCommand("insertcaption") : editor.execCommand("deletecaption");
143 144 145
        autoSizeContent.checked ? adaptByTextTable() : "";
        autoSizePage.checked ? editor.execCommand("adaptbywindow") : "";
        editor.execCommand("edittable", tone.value);
146 147
    };
})();