forked from rightjs/rightjs-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.js
137 lines (115 loc) · 2.95 KB
/
tool.js
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
/**
* The basic tools class
*
* Copyright (C) 2010 Nikolay Nemshilov
*/
Rte.Tool = new Class(Element, {
shortcut: false, // shortcut string
command: false, // execCommand command
value: null, // execCommand value
block: true, // should the 'keypress' be blocked
blip: false, // whether it should 'blip' when used
/**
* Basic constructor
*
* @param Rte rte reference
* @return Rte.Tool this
*/
initialize: function(rte) {
// searching for the tool-name
var name = '', tools = Rte.Tool, klass = this.constructor;
for (name in tools) {
if (tools[name] === klass) { break; }
}
this.$super('div', {
'html': '<i></i>', // <- icon container
'class': 'tool icon '+ name.toLowerCase(),
'title': (Rte.i18n[name] || name) + (
this.shortcut ? " ("+ this.shortcut + ")" : ""
)
});
// registering the tool
this.rte = rte;
rte.tools[name] = this;
// hooking up the shortcuts
if (this.shortcut) {
rte.shortcuts[this.shortcut.toUpperCase().charCodeAt(0)] = this;
}
// connecting the mousedown the way that the editor din't loose the focus
this.onMousedown(function(e) {
e.stop(); this.mousedown();
});
// checking the command initial state
this.check();
// allowing some nice chains in the subclass
return this;
},
/**
* The entry point for the tools
*
* @return void
*/
exec: function() {
if (!this.disabled) {
if (this.blip) { this.highlight(); }
this.rte.editor.focus().exec(
this.command, this.value
);
this.rte.status.update();
}
},
/**
* Checks the command's status
*
* @return void
*/
check: function() {
this._.className = this._.className.replace(' disabled', '');
this.disabled = false;
if (this.enabled()) {
this._.className = this._.className.replace(' active', '');
if (this.active()) {
this._.className += ' active';
}
} else {
this._.className += ' disabled';
this.disabled = true;
}
},
/**
* Checks if the command is enabled at all
*
* @return boolean check result
*/
enabled: function() {
return !this.command || document.queryCommandEnabled(this.command);
},
/**
* Queries if the command is in active state
*
* @return boolean check result
*/
active: function() {
try {
if (this.value) {
return document.queryCommandValue(this.command) == this.value;
} else {
return document.queryCommandState(this.command);
}
} catch(e) {}
return false;
},
/**
* Replacing the highlight method with some css stuff instead of an Fx
*
* @return Rte.Tool this
*/
highlight: function() {
R(this.addClass('highlight').removeClass).bind(this, 'highlight').delay(100);
},
// protected
// mousedown event receiver (might be replaced in subclasses)
mousedown: function() {
this.exec();
}
});