-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.coffee
383 lines (352 loc) · 10.5 KB
/
main.coffee
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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
resize = ->
width = $('#main').width()
height = $('#main').height()
px = Math.floor height / 24
# px = _.min [Math.floor(height / 24), Math.floor(width / 40)]
# if px % 2 == 1
# px--
$('.screen').css
'font-size': "#{px}px"
setup_address_book = ->
$('#quick-connect').empty()
$('#connect-book').empty()
for {name, host, port, protocol, module, icon, user_defined}, i in webterm.address_book.get_list()
css = if user_defined then 'user_defined' else ''
if icon
li = "<li connect='#{i}' class='#{css}'><a href='#'><img src='#{icon}'/>#{name}</a></li>"
else
li = "<li connect='#{i}' class='#{css}'><a href='#'>#{name}</a></li>"
$('#quick-connect').append li
$('#connect-book').append li
update_address_book = ->
setup_address_book()
$('#quick-connect').menu 'refresh'
$('#connect-book').menu 'refresh'
setup_quick_connect = ->
$('#quick-connect').menu
select: (event, ui) ->
selected = ui.item.attr('connect')
if selected == 'test'
webterm.test.new_tab()
else
address_index = parseInt selected
webterm.address_book.connect address_index
$('#quick-connect').hide()
menu_show = -> $('#quick-connect').show()
menu_hide = -> $('#quick-connect').hide()
$('#new-menu').hover menu_show, menu_hide
setup_connect_dialog = ->
tip = null
$("#connect-dialog").dialog
autoOpen: false
# height: 300
width: 380
modal: true
resizable: false
buttons: [
id: 'connect-add'
text: '添加此地址'
click: ->
$('#connect-add-name').dialog 'open'
name = $('#connect-dialog').dialog('option', 'title').match('^连接到(.+)$')?[1]
if name
$('#connect-add-name input').val name
$('#connect-add-name input').select()
,
# id: 'connect-manage'
# text: '管理'
# click: ->
# $(@).dialog 'close'
# ,
id: 'connect-cancel'
text: '取消'
click: ->
$(@).dialog 'close'
,
id: 'connect-ok'
text: '连接'
click: ->
webterm.tabs.bbs
icon: $('#connect-icon > img').attr 'src'
name: $('#connect-dialog').dialog('option', 'title').match('^连接到(.+)$')?[1] ? $('#connect-host').val()
host: $('#connect-host').val()
port: parseInt $('#connect-port').val()
module: $('#connect-type').val().toLowerCase()
$(@).dialog 'close'
]
open: ->
tip = webterm.status_bar.tip '按上下键选择收藏夹中的站点'
close: ->
tip?.close()
tip = null
apply_addres = (address) ->
$('#connect-host').val address.host
$('#connect-port').val address.port
$('#connect-icon > img').attr 'src', address.icon
$('#connect-type').val switch address.module
when webterm.bbs.smth then 'SMTH'
when webterm.bbs.lily then 'LILY'
when webterm.bbs.firebird then 'Firebird'
$('#connect-dialog').dialog "option", "title", "连接到#{address.name}"
switch_address = (n) ->
current = parseInt $('#connect-host').attr('connect') ? 0
selected = (current + n + webterm.address_book.size()) % webterm.address_book.size()
apply_addres webterm.address_book.nth selected
$('#connect-host').attr 'connect', selected
$('#connect-host').keydown (e) ->
k = keymap.event_to_virtual_key e
if k == 'up'
switch_address -1
e.preventDefault()
else if k == 'down'
switch_address 1
e.preventDefault()
else if k == 'enter'
$('#connect-ok').click()
$('#connect-host').on 'input', (e) ->
host = $(@).val()
address = webterm.address_book.lookup_host host
$('#connect-dialog').dialog "option", "title", "连接到#{address?.name ? ''}"
$('#connect-icon > img').attr 'src', address?.icon ? 'lib/webterm.png'
$('#connect-book').menu
select: (event, ui) ->
selected = ui.item.attr('connect')
address_index = parseInt selected
address = webterm.address_book.nth address_index
apply_addres address
$('#connect-host').attr 'connect', address_index
$('#connect-book').hide()
menu_show = -> $('#connect-book').show()
menu_hide = -> $('#connect-book').hide()
$('#connect-icon').hover menu_show, menu_hide
switch_address 0 # init dialog to address #0
$("#connect-add-name").dialog
autoOpen: false
height: 120
modal: true
buttons: [
text: '添加'
click: ->
new_index = webterm.address_book.add
icon: $('#connect-icon > img').attr 'src'
name: $(@).find('input').val()
host: $('#connect-host').val()
port: parseInt $('#connect-port').val()
module: $('#connect-type').val().toLowerCase()
update_address_book()
old_index = parseInt $('#connect-host').attr('connect') ? 0
if new_index <= old_index
$('#connect-host').attr 'connect', old_index + 1
$(@).dialog 'close'
$('#connect-host').focus()
,
text: '取消'
click: ->
$(@).dialog 'close'
$('#connect-host').focus()
]
$.contextMenu
selector: '#connect-host'
build: ($trigger, e) ->
menu =
callback: ->
index = $('#connect-host').attr('connect') ? 0
removed = webterm.address_book.remove index
if removed?
update_address_book()
webterm.status_bar.info '删除地址成功'
switch_address index
else
webterm.status_bar.info '不能删除此地址(此地址可能为内置地址)'
items: [
id: 'delete'
name: '删除此地址'
icon: 'delete'
]
return menu
setup_settings_dialog = ->
$('#settings').click ->
$('#settings-dialog').dialog 'open'
$("#settings-dialog").dialog
autoOpen: false
modal: true
width: 400
height: 200
resizeStop: ->
init_editor.resize()
session_editor.resize()
$('#settings-tabs').tabs
activate: (event, ui) ->
id = $(ui.newPanel.selector + '.ace_editor').attr 'id'
if id?
editor = ace.edit id
editor.focus()
# init
init_editor = webterm.editors.ace_coffee_editor
id: 'settings-init-script-editor'
code: webterm.settings.get("scripts.init")?.coffeescript
listener: (code) -> webterm.settings.set "scripts.init", code
# session init
session_editor = webterm.editors.ace_coffee_editor
id: 'settings-session-script-editor'
code: webterm.settings.get("scripts.session")?.coffeescript
listener: (code) -> webterm.settings.set "scripts.session", code
setup_menu = ->
$('#menu ul').menu
select: (event, ui) ->
id = ui.item.find('a').attr('href')
if id == '#input-dialog'
$('#input-dialog').dialog 'open'
else if id == '#script-dialog'
$('#script-dialog').dialog 'open'
else if id == '#status-bar'
webterm.status_bar.show()
else if id == '#about-dialog'
$('#about-dialog').dialog 'open'
menu_hide()
menu_show = -> $('#menu ul').show()
menu_hide = -> $('#menu ul').hide()
$('#menu').hover menu_show, menu_hide
setup_input_dialog = ->
post = ->
text = $('#input-dialog textarea').val()
webterm.active?.screen?.events.send_text text
$("#input-dialog").dialog
autoOpen: false
modal: false
width: 400
height: 200
buttons: [
text: '插入'
click: ->
post()
$(@).find('textarea').focus()
,
text: '清除'
click: ->
$(@).find('textarea').val('')
$(@).find('textarea').focus()
,
text: '关闭'
click: ->
$(@).dialog 'close'
]
$('#input-dialog textarea').keydown (e) ->
k = keymap.event_to_virtual_key e
if k == 'ctrl-enter'
post()
else if k == 'ctrl-w'
post()
$('#input-dialog').dialog 'close'
setup_script_dialog = ->
editor = webterm.editors.ace_coffee_editor
id: 'script-editor'
$("#script-dialog").dialog
autoOpen: false
modal: false
width: 400
height: 200
buttons: [
text: '打开'
click: ->
webterm.dialogs.file_open accepts: [extensions: ['coffee']], (script) ->
editor.getSession().setValue script
editor.focus()
,
text: '运行'
click: ->
webterm.eval editor.getSession().getValue()
editor.focus()
,
text: '清除'
click: ->
editor.getSession().setValue()
editor.focus()
,
text: '关闭'
click: ->
$(@).dialog 'close'
]
resizeStop: ->
editor.resize()
webterm.dnd.drag_data_to '#script-dialog', (data) ->
items = _.filter data.items, (item) -> item.kind == 'file'
if items.length == 0
return
if items.length > 1
console.log 'too many files'
item = items[0]
chosenFileEntry = item.webkitGetAsEntry()
chrome.fileSystem.getDisplayPath chosenFileEntry, (path) ->
if not path.match /\.coffee$/
console.log 'not a .coffee file'
return
chosenFileEntry.file (file) ->
reader = new FileReader()
reader.onerror = (e) ->
console.log 'open file error!', arguments
reader.onload = (e) ->
editor.getSession().setValue e.target.result
reader.readAsText file
$('#script-dialog').on 'dragover', (e) ->
e.originalEvent.preventDefault()
setup_about = ->
$('#about-dialog').dialog
autoOpen: false
modal: true
width: 400
open: ->
webterm.resources.get_text 'manifest.json', (text) ->
webterm_version = JSON.parse(text).version
chrome_version = window.navigator.appVersion.match(/Chrom(?:e|ium)\/\S+/)?[0] ? 'Chrome version unknown'
$('#about-version').text "Webterm/#{webterm_version}, #{chrome_version}"
setup = ->
Object.defineProperty webterm, 'active',
get: ->
webterm.tabs.active?.session
Object.defineProperty webterm, 'screen',
get: ->
webterm.active?.screen
webterm.keys.root.chain = (e) ->
webterm.active?.screen.events.on_keyboard e
webterm.tabs.on 'new', ->
# TODO: customize behavior
$("#connect-dialog").dialog 'open'
webterm.keys.on 'alt-f4', -> webterm.windows.safe_close()
webterm.keys.on 'ctrl-shift-i', -> $('#input-dialog').dialog 'open'
webterm.keys.on 'f5', -> $('#script-dialog').dialog 'open'
webterm.keys.on 'f8', -> $('#settings-dialog').dialog 'open'
webterm.keys.on 'f12', webterm.windows.toggle_full_screen
# webterm.test.new_tab() # for testing
$("#connect-dialog").dialog 'open'
webterm.keys.on 'ctrl-n', webterm.test.new_tab # for testing
webterm.keys.on 'ctrl-shift-s', webterm.test.save_current_screen # for testing
webterm.keys.on 'ctrl-shift-l', webterm.test.load # for testing
# $("#connect-dialog").dialog 'open' # for testing
# $("#settings-dialog").dialog 'open' # for testing
webterm.windows.on_closed ->
# FIXME: XXX: this doesn't work?
webterm.cache.save()
setInterval webterm.cache.save, 30000 # XXX: auto save every 30s -- just a workaround to above
setup_font = ->
resize()
$(window).resize resize
# $(window).resize ->
# $('.screen').css 'z-index': 1
webterm.tabs.on 'create', resize
init = ->
init = webterm.settings.get("scripts.init")
if init?
webterm.eval init.coffeescript
webterm.init ->
setup_address_book()
setup_quick_connect()
setup_connect_dialog()
setup_settings_dialog()
setup_menu()
setup_input_dialog()
setup_script_dialog()
setup_about()
setup()
setup_font()
init()