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 padutils = require('/pad_utils').padutils; |
24 | var padeditor = require('/pad_editor').padeditor; |
25 | var padsavedrevs = require('/pad_savedrevs').padsavedrevs; |
26 | |
27 | function indexOf(array, value) { |
28 | for (var i = 0, ii = array.length; i < ii; i++) { |
29 | if (array[i] == value) { |
30 | return i; |
31 | } |
32 | } |
33 | return -1; |
34 | } |
35 | |
36 | var padeditbar = (function() |
37 | { |
38 | |
39 | var syncAnimation = (function() |
40 | { |
41 | var SYNCING = -100; |
42 | var DONE = 100; |
43 | var state = DONE; |
44 | var fps = 25; |
45 | var step = 1 / fps; |
46 | var T_START = -0.5; |
47 | var T_FADE = 1.0; |
48 | var T_GONE = 1.5; |
49 | var animator = padutils.makeAnimationScheduler(function() |
50 | { |
51 | if (state == SYNCING || state == DONE) |
52 | { |
53 | return false; |
54 | } |
55 | else if (state >= T_GONE) |
56 | { |
57 | state = DONE; |
58 | $("#syncstatussyncing").css('display', 'none'); |
59 | $("#syncstatusdone").css('display', 'none'); |
60 | return false; |
61 | } |
62 | else if (state < 0) |
63 | { |
64 | state += step; |
65 | if (state >= 0) |
66 | { |
67 | $("#syncstatussyncing").css('display', 'none'); |
68 | $("#syncstatusdone").css('display', 'block').css('opacity', 1); |
69 | } |
70 | return true; |
71 | } |
72 | else |
73 | { |
74 | state += step; |
75 | if (state >= T_FADE) |
76 | { |
77 | $("#syncstatusdone").css('opacity', (T_GONE - state) / (T_GONE - T_FADE)); |
78 | } |
79 | return true; |
80 | } |
81 | }, step * 1000); |
82 | return { |
83 | syncing: function() |
84 | { |
85 | state = SYNCING; |
86 | $("#syncstatussyncing").css('display', 'block'); |
87 | $("#syncstatusdone").css('display', 'none'); |
88 | }, |
89 | done: function() |
90 | { |
91 | state = T_START; |
92 | animator.scheduleAnimation(); |
93 | } |
94 | }; |
95 | }()); |
96 | |
97 | var self = { |
98 | init: function() |
99 | { |
100 | $("#editbar .editbarbutton").attr("unselectable", "on"); // for IE |
101 | $("#editbar").removeClass("disabledtoolbar").addClass("enabledtoolbar"); |
102 | }, |
103 | isEnabled: function() |
104 | { |
105 | // return !$("#editbar").hasClass('disabledtoolbar'); |
106 | return true; |
107 | }, |
108 | disable: function() |
109 | { |
110 | $("#editbar").addClass('disabledtoolbar').removeClass("enabledtoolbar"); |
111 | }, |
112 | toolbarClick: function(cmd) |
113 | { |
114 | if (self.isEnabled()) |
115 | { |
116 | if(cmd == "showusers") |
117 | { |
118 | self.toogleDropDown("users"); |
119 | } |
120 | else if (cmd == 'settings') |
121 | { |
122 | self.toogleDropDown("settingsmenu"); |
123 | } |
124 | else if (cmd == 'embed') |
125 | { |
126 | self.setEmbedLinks(); |
127 | $('#linkinput').focus().select(); |
128 | self.toogleDropDown("embed"); |
129 | } |
130 | else if (cmd == 'import_export') |
131 | { |
132 | self.toogleDropDown("importexport"); |
133 | } |
134 | else if (cmd == 'save') |
135 | { |
136 | padsavedrevs.saveNow(); |
137 | } |
138 | else |
139 | { |
140 | padeditor.ace.callWithAce(function(ace) |
141 | { |
142 | if (cmd == 'bold' || cmd == 'italic' || cmd == 'underline' || cmd == 'strikethrough') ace.ace_toggleAttributeOnSelection(cmd); |
143 | else if (cmd == 'undo' || cmd == 'redo') ace.ace_doUndoRedo(cmd); |
144 | else if (cmd == 'insertunorderedlist') ace.ace_doInsertUnorderedList(); |
145 | else if (cmd == 'insertorderedlist') ace.ace_doInsertOrderedList(); |
146 | else if (cmd == 'indent') |
147 | { |
148 | if (!ace.ace_doIndentOutdent(false)) |
149 | { |
150 | ace.ace_doInsertUnorderedList(); |
151 | } |
152 | } |
153 | else if (cmd == 'outdent') |
154 | { |
155 | ace.ace_doIndentOutdent(true); |
156 | } |
157 | else if (cmd == 'clearauthorship') |
158 | { |
159 | if ((!(ace.ace_getRep().selStart && ace.ace_getRep().selEnd)) || ace.ace_isCaret()) |
160 | { |
161 | if (window.confirm("Clear authorship colors on entire document?")) |
162 | { |
163 | ace.ace_performDocumentApplyAttributesToCharRange(0, ace.ace_getRep().alltext.length, [ |
164 | ['author', ''] |
165 | ]); |
166 | } |
167 | } |
168 | else |
169 | { |
170 | ace.ace_setAttributeOnSelection('author', ''); |
171 | } |
172 | } |
173 | }, cmd, true); |
174 | } |
175 | } |
176 | if(padeditor.ace) padeditor.ace.focus(); |
177 | }, |
178 | toogleDropDown: function(moduleName) |
179 | { |
180 | var modules = ["settingsmenu", "importexport", "embed", "users"]; |
181 | |
182 | //hide all modules |
183 | if(moduleName == "none") |
184 | { |
185 | $("#editbar ul#menu_right > li").removeClass("selected"); |
186 | for(var i=0;i<modules.length;i++) |
187 | { |
188 | //skip the userlist |
189 | if(modules[i] == "users") |
190 | continue; |
191 | |
192 | var module = $("#" + modules[i]); |
193 | |
194 | if(module.css('display') != "none") |
195 | { |
196 | module.slideUp("fast"); |
197 | } |
198 | } |
199 | } |
200 | else |
201 | { |
202 | var nth_child = indexOf(modules, moduleName) + 1; |
203 | if (nth_child > 0 && nth_child <= 3) { |
204 | $("#editbar ul#menu_right li:not(:nth-child(" + nth_child + "))").removeClass("selected"); |
205 | $("#editbar ul#menu_right li:nth-child(" + nth_child + ")").toggleClass("selected"); |
206 | } |
207 | //hide all modules that are not selected and show the selected one |
208 | for(var i=0;i<modules.length;i++) |
209 | { |
210 | var module = $("#" + modules[i]); |
211 | |
212 | if(module.css('display') != "none") |
213 | { |
214 | module.slideUp("fast"); |
215 | } |
216 | else if(modules[i]==moduleName) |
217 | { |
218 | module.slideDown("fast"); |
219 | } |
220 | } |
221 | } |
222 | }, |
223 | setSyncStatus: function(status) |
224 | { |
225 | if (status == "syncing") |
226 | { |
227 | syncAnimation.syncing(); |
228 | } |
229 | else if (status == "done") |
230 | { |
231 | syncAnimation.done(); |
232 | } |
233 | }, |
234 | setEmbedLinks: function() |
235 | { |
236 | if ($('#readonlyinput').is(':checked')) |
237 | { |
238 | var basePath = document.location.href.substring(0, document.location.href.indexOf("/p/")); |
239 | var readonlyLink = basePath + "/ro/" + clientVars.readOnlyId; |
240 | $('#embedinput').val("<iframe src='" + readonlyLink + "?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false' width=600 height=400>"); |
241 | $('#linkinput').val(readonlyLink); |
242 | $('#embedreadonlyqr').attr("src","https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=|0&chl=" + readonlyLink); |
243 | } |
244 | else |
245 | { |
246 | var padurl = window.location.href.split("?")[0]; |
247 | $('#embedinput').val("<iframe src='" + padurl + "?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false' width=600 height=400>"); |
248 | $('#linkinput').val(padurl); |
249 | $('#embedreadonlyqr').attr("src","https://chart.googleapis.com/chart?chs=200x200&cht=qr&chld=|0&chl=" + padurl); |
250 | } |
251 | } |
252 | }; |
253 | return self; |
254 | }()); |
255 | |
256 | exports.padeditbar = padeditbar; |