codemirror_shared_head

NAML documentation   Watch a video
   Usages of this macro
... in macro_viewer.naml
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<macro name="codemirror_shared_head">
    <style type="text/css">
        .CodeMirror-line-numbers {
            font-family: verdana, arial, sans-serif;
            font-size: 11pt;
            width: 2.2em;
            text-align: right;
            padding-right: .3em;
            line-height: normal;
            border-right-width:2px;
            border-right-style:solid;
        }
        #resizebar {
            height:8px;
            background:url('/images/grip.png') #d8d8d8 repeat;
            border:1px solid #aaa;
            cursor:n-resize;
        }
    </style>
    <script src="/util/codemirror/js/codemirror.js"></script>
    <script src="/util/codemirror/js/highlight.js"></script>
    <script src="/util/codemirror/js/stringstream.js"></script>
    <script src="/util/codemirror/js/tokenize.js"></script>
    <script src="/util/codemirror/js/parsexml.js"></script>
    <script type="text/javascript">
        <![CDATA[
        function newEditor(height) {
            var e = CodeMirror.fromTextArea('txt_basic', {
                parserfile: "parsexml.js",
                stylesheet: "/util/codemirror/css/xmlcolors.css",
                path: "/util/codemirror/js/",
                lineNumbers: true,
                height: height,
                indentUnit: 4,
            });
            var $wrapping = $('#txt_basic').next();
            $wrapping.attr('id', 'wrapping_basic');
            $wrapping.css('margin-top', '.4em');
            $wrapping.addClass('medium-border-color border1');
            addResizeBar($wrapping);
            $('div.CodeMirror-line-numbers', $wrapping).addClass('shaded-bg-color medium-border-color');
            return e;
        };
 
        function addResizeBar($wrapping) {
            $wrapping.after('<div id="resizebar"></div>');
 
            var dragging = false;
            function startDrag(e) {
                if ($(e.target).attr('id') == 'resizebar')
                    dragging = true;
                if (dragging && typeof e.preventDefault != 'undefined')
                    e.preventDefault();
            };
            function endDrag(e) {
                dragging = false;
            };
            function drag(e, x, y) {
                e.stopPropagation();
                if (dragging) {
                    var h = y - $wrapping.offset().top - 5;
                    $wrapping.height(h < 100? 100 : h);
                }
            };
            function onSelectStart() {
                if (dragging) return false;
            };
 
            function isCorrectFrame(f) {
                try {
                    frame.document.location.href;
                    return true;
                } catch(err) {
                    return false;
                }
            }
            var index = 0;
            var frame = window.frames[index++];
            while (!isCorrectFrame(frame)) {
                frame = window.frames[index++];
            }
            $(frame.document)
                .mousemove(function(e) {
                    var x = e.pageX + $wrapping.offset().left;
                    var y = e.pageY + $wrapping.offset().top;
                    drag(e, x, y);
                })
                .mouseup(function(e) { endDrag(e); })
                .select(onSelectStart);
 
            $(document)
                .mousemove(function(e) { drag(e, e.pageX, e.pageY); })
                .mousedown(function(e) { startDrag(e); })
                .mouseup(function(e) { endDrag(e); })
                .select(onSelectStart);
        };
        var basicEditor;
        function inlineEditor(height, line, col) {
            if (!basicEditor)
                basicEditor = newEditor(height);
            function showCaret() {
                var $lines = $('#wrapping_basic div.CodeMirror-line-numbers').children();
                if (basicEditor.editor && $lines.size() > 5) {
                    if (line) {
                        if (col) {
                            var ln = basicEditor.nthLine(line);
                            basicEditor.selectLines(ln, col);
                        } else
                            basicEditor.jumpToLine(line);
                    } else
                        basicEditor.jumpToLine(1);
                } else
                    setTimeout(showCaret, 200);
            };
            showCaret();
        };
        function getLineNumber(s, token) {
            var line = 1;
            var pos = s.indexOf(token);
            for (var i=0; i < pos; i++) {
                if (s.charAt(i) == '\n')
                    line++;
            }
            return line;
        };
        function formatError(msg) {
            var h = msg.replace(/</g, "&lt;");
            h = h.replace(/>/g, "&gt;");
            h = h.replace(/\n/g, "<br/>");
            h = h.replace(/(\t)|([ ]{6})/g, "<span style='padding:0 2em'>&nbsp;</span>");
            h = '<img src="/images/icon_alert_sm.png" class="image16"/> ' + h;
            return h;
        };
        ]]>
    </script>
</macro>