Welcome to mirror list, hosted at ThFree Co, Russian Federation.

chromegoshen.js « onpage « goshen-chrome « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a95ac9c4ea60e2a02656dc66722daaa7265e1390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(function(window) {

    var demo_url = "ec2-52-23-242-15.compute-1.amazonaws.com:8081";

    var _goshen = window._goshen;

    on = function(event, cb) {
        window.addEventListener(event, cb);
    }

    off = function(event, cb) {
        window.removeEventListener(event, cb);
    }

    class ChromeGoshen {
        constructor() {
            this.G = new _goshen.Goshen(demo_url);
            console.info("Goshenjs engine loaded successfully.")
        }

        /**
        * Begin interactive dom node selection.
        */
        selectMode() {
            var self = this;
            var selection = [];
            var previousElement = null;

            var showSelection = function() {
                var olds = document.querySelectorAll('._goshen-selected');
                for (var i = 0; i < olds.length; i++) {
                    olds[i].classList.remove('_goshen-selected');
                }
                for (var i = 0; i < selection.length; i++) {
                    selection[i].classList.add('_goshen-selected');
                }
            };

            var setSelection = function(sel) {
                selection = sel;
                showSelection();
            };

            var validParents = [
                "DIV", "ARTICLE", "BLOCKQUOTE", "MAIN",
                "SECTION", "UL", "OL", "DL"
            ];

            var validChildren = [
                "P", "H1", "H2", "H3", "H4", "H5", "H6", "SPAN", "DL",
                "OL", "UL", "BLOCKQUOTE", "SECTION"
            ];

            var selectSiblings = function(el) {
                var firstChild = el;
                var parent = el.parentNode;
                while (parent && !~validParents.indexOf(parent.tagName)) {
                    firstChild = parent;
                    parent = firstChild.parentNode;
                }

                if (parent) {
                    var kids = parent.childNodes,
                        len = kids.length,
                        result = [],
                        i = 0;

                    while (kids[i] !== firstChild) { i++; }

                    for (; i < len; i++) {
                        var kid = kids[i];
                        if (!!~validChildren.indexOf(kid.tagName)) {
                            result.push(kid);
                        }
                    }
                    return result;

                } else { return [el]; }
            };

            var stop = function() {
                off("mouseover", mouseoverHandler);
                off("mousemove", moveHandler);
                off("keydown", keydownHandler);
                off("keyup", keyupHandler);
                off("click", clickHandler);
                self.performSelectTranslation(selection);
            };

            var mouseoverHandler = function(ev) {
                previousElement = ev.target;

                if (ev.altKey) {
                    setSelection([ev.target]);
                } else {
                    setSelection(selectSiblings(ev.target));
                }
            };

            var clickHandler = function(ev) {
                stop();
            };

            var moveHandler = function(ev) {
                mouseoverHandler(ev);
                off("mousemove", moveHandler);
            };

            var keydownHandler = function(ev) {
                if (ev.keyCode === 27) {
                    stop();
                } else if (ev.altKey && selection.length > 1) {
                    setSelection([selection[0]]);
                }
            };

            var keyupHandler = function(ev) {
                if (!ev.altKey && selection.length === 1) {
                    setSelection(selectSiblings(selection[0]));
                }
            };

            on("mouseover", mouseoverHandler);
            on("click", clickHandler);
            on("mousemove", moveHandler);
            on("keydown", keydownHandler);
            on("keyup", keyupHandler);
        }

        select(contextData) {
            var text;
            if (contextData === undefined) {
                text = window.getSelection().toString();
            } else {
                text = contextData.selectionText;
            }
            if (text.trim().length > 0) {
                this.init(this.parse.string(text));
                window.getSelection().removeAllRanges();
            } else {
                selectMode();
            }
        };

        _chunkedTranslation(text) {
            // We need to find a way to split on sentences, or long things.
            var texts = text.split('.');
            for (var i = 0; i < texts.length; i++) {
                texts[i] = this.G.translate(texts[i]);
            }
            return texts.join('.');
        }

        performSelectTranslation(selection) {
            for (var i = 0; i < selection.length; i++) {
                selection[i].classList.add('_goshen-active');
                selection[i].innerText = this._chunkedTranslation(selection[i].innerText);
                selection[i].classList.remove('_goshen-active');
                selection[i].classList.remove('_goshen-selected');
            }
        }
    };

    _goshen._cg = new ChromeGoshen();

})(this);