forked from mycolorway/simditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolbar.coffee
99 lines (70 loc) · 2.64 KB
/
toolbar.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
class Toolbar extends Plugin
@className: 'Toolbar'
opts:
toolbar: true
toolbarFloat: true
_tpl:
wrapper: '<div class="simditor-toolbar"><ul></ul></div>'
separator: '<li><span class="separator"></span></li>'
constructor: (args...) ->
super args...
@editor = @widget
_init: ->
return unless @opts.toolbar
unless $.isArray @opts.toolbar
@opts.toolbar = ['bold', 'italic', 'underline', 'strikethrough', '|', 'ol', 'ul', 'blockquote', 'code', '|', 'link', 'image', '|', 'indent', 'outdent']
@_render()
@list.on 'click', (e) =>
false
@wrapper.on 'mousedown', (e) =>
@list.find('.menu-on').removeClass('.menu-on')
$(document).on 'mousedown.simditor', (e) =>
@list.find('.menu-on').removeClass('.menu-on')
if @opts.toolbarFloat
@wrapper.width @wrapper.outerWidth()
@wrapper.css 'left', @wrapper.offset().left
$(window).on 'scroll.simditor-' + @editor.id, (e) =>
topEdge = @editor.wrapper.offset().top
bottomEdge = topEdge + @editor.wrapper.outerHeight() - 80
scrollTop = $(document).scrollTop()
if scrollTop <= topEdge or scrollTop >= bottomEdge
@editor.wrapper.removeClass('toolbar-floating')
else
@editor.wrapper.addClass('toolbar-floating')
@editor.on 'selectionchanged focus', =>
@toolbarStatus()
@editor.on 'destroy', =>
@buttons.length = 0
$(document).on 'mousedown.simditor-' + @editor.id, (e) =>
@list.find('li.menu-on').removeClass('menu-on')
_render: ->
@buttons = []
@wrapper = $(@_tpl.wrapper).prependTo(@editor.wrapper)
@list = @wrapper.find('ul')
for name in @opts.toolbar
if name == '|'
$(@_tpl.separator).appendTo @list
continue
unless @constructor.buttons[name]
throw new Error 'simditor: invalid toolbar button "' + name + '"'
continue
@buttons.push new @constructor.buttons[name](@editor)
toolbarStatus: (name) ->
return unless @editor.inputManager.focused
buttons = @buttons[..]
@editor.util.traverseUp (node) =>
removeButtons = []
for button, i in buttons
continue if name? and button.name isnt name
removeButtons.push button if !button.status or button.status($(node)) is true
for button in removeButtons
i = $.inArray(button, buttons)
buttons.splice(i, 1)
return false if buttons.length == 0
#button.setActive false for button in buttons unless success
findButton: (name) ->
button = @list.find('.toolbar-item-' + name).data('button')
button ? null
@addButton: (btn) ->
@buttons[btn::name] = btn
@buttons: {}