Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / CodeMirrorUtils.js
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.InplaceEditor}
34  */
35 WebInspector.CodeMirrorUtils = function()
36 {
37     WebInspector.InplaceEditor.call(this);
38 }
39
40 WebInspector.CodeMirrorUtils.prototype = {
41     /**
42      * @return {string}
43      */
44     editorContent: function(editingContext) {
45         return editingContext.codeMirror.getValue();
46     },
47
48     /**
49      * @param {?Event} e
50      */
51     _consumeCopy: function(e)
52     {
53         e.consume();
54     },
55
56     setUpEditor: function(editingContext)
57     {
58         var element = editingContext.element;
59         var config = editingContext.config;
60         loadScript("CodeMirrorTextEditor.js");
61         editingContext.cssLoadView = new WebInspector.CodeMirrorCSSLoadView();
62         editingContext.cssLoadView.show(element);
63         WebInspector.setCurrentFocusElement(element);
64         element.addEventListener("copy", this._consumeCopy, false);
65         var codeMirror = window.CodeMirror(element, {
66             mode: config.mode,
67             lineWrapping: config.lineWrapping,
68             smartIndent: config.smartIndent,
69             autofocus: true,
70             theme: config.theme,
71             value: config.initialValue
72         });
73         codeMirror.getWrapperElement().classList.add("source-code");
74         codeMirror.on("cursorActivity", function(cm) {
75             cm.display.cursor.scrollIntoViewIfNeeded(false);
76         });
77         editingContext.codeMirror = codeMirror;
78     },
79
80     closeEditor: function(editingContext)
81     {
82         editingContext.element.removeEventListener("copy", this._consumeCopy, false);
83         editingContext.cssLoadView.detach();
84     },
85
86     cancelEditing: function(editingContext)
87     {
88         editingContext.codeMirror.setValue(editingContext.oldText);
89     },
90
91     augmentEditingHandle: function(editingContext, handle)
92     {
93         function setWidth(editingContext, width)
94         {
95             var padding = 30;
96             var codeMirror = editingContext.codeMirror;
97             codeMirror.getWrapperElement().style.width = (width - codeMirror.getWrapperElement().offsetLeft - padding) + "px";
98             codeMirror.refresh();
99         }
100
101         handle.codeMirror = editingContext.codeMirror;
102         handle.setWidth = setWidth.bind(null, editingContext);
103     },
104
105     __proto__: WebInspector.InplaceEditor.prototype
106 }
107
108 /**
109  * @constructor
110  * @implements {WebInspector.TokenizerFactory}
111  */
112 WebInspector.CodeMirrorUtils.TokenizerFactory = function() { }
113
114 WebInspector.CodeMirrorUtils.TokenizerFactory.prototype = {
115     /**
116      * @param {string} mimeType
117      * @return {function(string, function(string, ?string, number, number))}
118      */
119     createTokenizer: function(mimeType)
120     {
121         var mode = CodeMirror.getMode({indentUnit: 2}, mimeType);
122         var state = CodeMirror.startState(mode);
123         function tokenize(line, callback)
124         {
125             var stream = new CodeMirror.StringStream(line);
126             while (!stream.eol()) {
127                 var style = mode.token(stream, state);
128                 var value = stream.current();
129                 callback(value, style, stream.start, stream.start + value.length);
130                 stream.start = stream.pos;
131             }
132         }
133         return tokenize;
134     }
135 }
136
137 /**
138  * This bogus view is needed to load/unload CodeMirror-related CSS on demand.
139  *
140  * @constructor
141  * @extends {WebInspector.View}
142  */
143 WebInspector.CodeMirrorCSSLoadView = function()
144 {
145     WebInspector.View.call(this);
146     this.element.classList.add("hidden");
147     this.registerRequiredCSS("cm/codemirror.css");
148     this.registerRequiredCSS("cm/cmdevtools.css");
149 }
150
151 WebInspector.CodeMirrorCSSLoadView.prototype = {
152     __proto__: WebInspector.View.prototype
153 }