forked from GetmeUK/ContentTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.coffee
More file actions
127 lines (98 loc) · 3.81 KB
/
Copy pathlink.coffee
File metadata and controls
127 lines (98 loc) · 3.81 KB
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
class ContentTools.LinkDialog extends ContentTools.AnchoredDialogUI
# An anchored dialog to support inserting/modifying a link
# The target that will be set by the link tool if the open in new window
# option is selected.
NEW_WINDOW_TARGET = '_blank'
constructor: (href='', target='') ->
super()
# The initial value to set the href and target attribute
# of the link as (e.g if we're editing a link).
@_href = href
@_target = target
mount: () ->
# Mount the widget
super()
# Create the input element for the link
@_domInput = document.createElement('input')
@_domInput.setAttribute('class', 'ct-anchored-dialog__input')
@_domInput.setAttribute('name', 'href')
@_domInput.setAttribute(
'placeholder',
ContentEdit._('Enter a link') + '...'
)
@_domInput.setAttribute('type', 'text')
@_domInput.setAttribute('value', @_href)
@_domElement.appendChild(@_domInput)
# Create a toggle button to allow users to toogle between no target and
# TARGET (open in a new window).
@_domTargetButton = @constructor.createDiv([
'ct-anchored-dialog__target-button'])
@_domElement.appendChild(@_domTargetButton)
# Check if the new window target has already been set for the link
if @_target == NEW_WINDOW_TARGET
ContentEdit.addCSSClass(
@_domTargetButton,
'ct-anchored-dialog__target-button--active'
)
# Create the confirm button
@_domButton = @constructor.createDiv(['ct-anchored-dialog__button'])
@_domElement.appendChild(@_domButton)
# Add interaction handlers
@_addDOMEventListeners()
save: () ->
# Save the link. This method triggers the save method against the dialog
# allowing the calling code to listen for the `save` event and manage
# the outcome.
if not @isMounted
return @trigger('save', '')
linkAttr = {}
linkAttr.href = @_domInput.value.trim()
linkAttr.target = @_target if @_target
@trigger('save', linkAttr)
show: () ->
# Show the widget
super()
# Once visible automatically give focus to the link input
@_domInput.focus()
# If a there's an intially value then select it so it can be easily
# replaced.
if @_href
@_domInput.select()
unmount: () ->
# Unmount the component from the DOM
# Unselect any content
if @isMounted()
@_domInput.blur()
super()
@_domButton = null
@_domInput = null
# Private methods
_addDOMEventListeners: () ->
# Add event listeners for the widget
# Add support for saving the link whenever the `return` key is pressed
# or the button is selected.
# Input
@_domInput.addEventListener 'keypress', (ev) =>
if ev.keyCode is 13
@save()
# Toggle the target attribute for the link ('' or TARGET)
@_domTargetButton.addEventListener 'click', (ev) =>
ev.preventDefault()
# No target
if @_target == NEW_WINDOW_TARGET
@_target = ''
ContentEdit.removeCSSClass(
@_domTargetButton,
'ct-anchored-dialog__target-button--active'
)
# Target TARGET
else
@_target = NEW_WINDOW_TARGET
ContentEdit.addCSSClass(
@_domTargetButton,
'ct-anchored-dialog__target-button--active'
)
# Button
@_domButton.addEventListener 'click', (ev) =>
ev.preventDefault()
@save()