5a920362 |
1 | /** |
2 | * This code is mostly from the old Etherpad. Please help us to comment this code. |
3 | * This helps other people to understand this code better and helps them to improve it. |
4 | * TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED |
5 | */ |
6 | |
7 | /** |
8 | * Copyright 2009 Google Inc. |
9 | * |
10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
11 | * you may not use this file except in compliance with the License. |
12 | * You may obtain a copy of the License at |
13 | * |
14 | * http://www.apache.org/licenses/LICENSE-2.0 |
15 | * |
16 | * Unless required by applicable law or agreed to in writing, software |
17 | * distributed under the License is distributed on an "AS-IS" BASIS, |
18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
19 | * See the License for the specific language governing permissions and |
20 | * limitations under the License. |
21 | */ |
22 | |
23 | var padcookie = require('/pad_cookie').padcookie; |
24 | var padutils = require('/pad_utils').padutils; |
25 | |
26 | var padeditor = (function() |
27 | { |
28 | var Ace2Editor = undefined; |
29 | var pad = undefined; |
30 | var settings = undefined; |
31 | var self = { |
32 | ace: null, |
33 | // this is accessed directly from other files |
34 | viewZoom: 100, |
35 | init: function(readyFunc, initialViewOptions, _pad) |
36 | { |
37 | Ace2Editor = require('/ace').Ace2Editor; |
38 | pad = _pad; |
39 | settings = pad.settings; |
40 | |
41 | function aceReady() |
42 | { |
43 | $("#editorloadingbox").hide(); |
44 | if (readyFunc) |
45 | { |
46 | readyFunc(); |
47 | } |
48 | } |
49 | |
50 | self.ace = new Ace2Editor(); |
51 | self.ace.init("editorcontainer", "", aceReady); |
52 | self.ace.setProperty("wraps", true); |
53 | if (pad.getIsDebugEnabled()) |
54 | { |
55 | self.ace.setProperty("dmesg", pad.dmesg); |
56 | } |
57 | self.initViewOptions(); |
58 | self.setViewOptions(initialViewOptions); |
59 | |
60 | // view bar |
61 | self.initViewZoom(); |
62 | $("#viewbarcontents").show(); |
63 | }, |
64 | initViewOptions: function() |
65 | { |
66 | padutils.bindCheckboxChange($("#options-linenoscheck"), function() |
67 | { |
68 | pad.changeViewOption('showLineNumbers', padutils.getCheckbox($("#options-linenoscheck"))); |
69 | }); |
70 | padutils.bindCheckboxChange($("#options-colorscheck"), function() |
71 | { |
72 | padcookie.setPref('showAuthorshipColors', padutils.getCheckbox("#options-colorscheck")); |
73 | pad.changeViewOption('showAuthorColors', padutils.getCheckbox("#options-colorscheck")); |
74 | }); |
75 | $("#viewfontmenu").change(function() |
76 | { |
77 | pad.changeViewOption('useMonospaceFont', $("#viewfontmenu").val() == 'monospace'); |
78 | }); |
79 | }, |
80 | setViewOptions: function(newOptions) |
81 | { |
82 | function getOption(key, defaultValue) |
83 | { |
84 | var value = String(newOptions[key]); |
85 | if (value == "true") return true; |
86 | if (value == "false") return false; |
87 | return defaultValue; |
88 | } |
89 | |
90 | self.ace.setProperty("showsauthorcolors", !settings.noColors); |
91 | |
92 | self.ace.setProperty("rtlIsTrue", settings.rtlIsTrue); |
93 | |
94 | var v; |
95 | |
96 | v = getOption('showLineNumbers', true); |
97 | self.ace.setProperty("showslinenumbers", v); |
98 | padutils.setCheckbox($("#options-linenoscheck"), v); |
99 | |
100 | v = getOption('showAuthorColors', true); |
101 | self.ace.setProperty("showsauthorcolors", v); |
102 | padutils.setCheckbox($("#options-colorscheck"), v); |
103 | |
104 | v = getOption('useMonospaceFont', false); |
105 | self.ace.setProperty("textface", (v ? "monospace" : "Arial, sans-serif")); |
106 | $("#viewfontmenu").val(v ? "monospace" : "normal"); |
107 | }, |
108 | initViewZoom: function() |
109 | { |
110 | var viewZoom = Number(padcookie.getPref('viewZoom')); |
111 | if ((!viewZoom) || isNaN(viewZoom)) |
112 | { |
113 | viewZoom = 100; |
114 | } |
115 | self.setViewZoom(viewZoom); |
116 | $("#viewzoommenu").change(function(evt) |
117 | { |
118 | // strip initial 'z' from val |
119 | self.setViewZoom(Number($("#viewzoommenu").val().substring(1))); |
120 | }); |
121 | }, |
122 | setViewZoom: function(percent) |
123 | { |
124 | if (!(percent >= 50 && percent <= 1000)) |
125 | { |
126 | // percent is out of sane range or NaN (which fails comparisons) |
127 | return; |
128 | } |
129 | |
130 | self.viewZoom = percent; |
131 | $("#viewzoommenu").val('z' + percent); |
132 | |
133 | var baseSize = 13; |
134 | self.ace.setProperty('textsize', Math.round(baseSize * self.viewZoom / 100)); |
135 | |
136 | padcookie.setPref('viewZoom', percent); |
137 | }, |
138 | dispose: function() |
139 | { |
140 | if (self.ace) |
141 | { |
142 | self.ace.destroy(); |
143 | self.ace = null; |
144 | } |
145 | }, |
146 | disable: function() |
147 | { |
148 | if (self.ace) |
149 | { |
150 | self.ace.setProperty("grayedOut", true); |
151 | self.ace.setEditable(false); |
152 | } |
153 | }, |
154 | restoreRevisionText: function(dataFromServer) |
155 | { |
156 | pad.addHistoricalAuthors(dataFromServer.historicalAuthorData); |
157 | self.ace.importAText(dataFromServer.atext, dataFromServer.apool, true); |
158 | } |
159 | }; |
160 | return self; |
161 | }()); |
162 | |
163 | exports.padeditor = padeditor; |