forked from usablica/widearea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widearea.js
269 lines (234 loc) · 7.97 KB
/
widearea.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
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
/**
* WideArea v0.1.2
* https://github.com/usablica/widearea
* MIT licensed
*
* Copyright (C) 2013 usabli.ca - By Afshin Mehrabani (@afshinmeh)
*/
(function (root, factory) {
if (typeof exports === 'object') {
// CommonJS
factory(exports);
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['exports'], factory);
} else {
// Browser globals
factory(root);
}
} (this, function (exports) {
//Default config/variables
var VERSION = '0.1.2';
/**
* WideArea main class
*
* @class WideArea
*/
function WideArea(obj) {
this._targetElement = obj;
this._options = {
wideAreaAttr: 'data-widearea',
exitOnEsc: true,
defaultColorScheme: 'light',
closeIconLabel: 'Close WideArea',
changeThemeIconLabel: 'Toggle Color Scheme',
fullScreenIconLabel: 'WideArea Mode'
};
_enable.call(this);
}
/**
* Enable the WideArea
*
* @api private
* @method _enable
*/
function _enable() {
var self = this,
//select all textareas in the target element
textAreaList = this._targetElement.querySelectorAll('textarea[' + this._options.wideAreaAttr + '=\'enable\']'),
// don't make functions within a loop.
fullscreenIconClickHandler = function() {
_enableFullScreen.call(self, this);
};
//then, change all textareas to widearea
for (var i = textAreaList.length - 1; i >= 0; i--) {
var currentTextArea = textAreaList[i];
//create widearea wrapper element
var wideAreaWrapper = document.createElement('div'),
wideAreaIcons = document.createElement('div'),
fullscreenIcon = document.createElement('a');
wideAreaWrapper.className = 'widearea-wrapper';
wideAreaIcons.className = 'widearea-icons';
fullscreenIcon.className = 'widearea-icon fullscreen';
fullscreenIcon.title = this._options.fullScreenIconLabel;
//hack!
fullscreenIcon.href = 'javascript:void(0);';
//bind to click event
fullscreenIcon.onclick = fullscreenIconClickHandler;
//clone current textarea
var newTextArea = currentTextArea.cloneNode();
newTextArea.value = currentTextArea.value;
wideAreaWrapper.appendChild(newTextArea);
wideAreaIcons.appendChild(fullscreenIcon);
wideAreaWrapper.appendChild(wideAreaIcons);
//add the wrapper to element
currentTextArea.parentNode.replaceChild(wideAreaWrapper, currentTextArea);
}
}
/**
* FullScreen the textarea
*
* @api private
* @method _enableFullScreen
* @param {Object} link
*/
function _enableFullScreen(link) {
var self = this;
//I don't know whether is this correct or not, but I think it's not a bad way
var targetTextarea = link.parentNode.parentNode.querySelector("textarea");
//clone current textarea
var currentTextArea = targetTextarea.cloneNode();
//add proper css class names
currentTextArea.className = ('widearea-fullscreen ' + targetTextarea.className).replace(/^\s+|\s+$/g, "");
targetTextarea.className = ('widearea-fullscreened ' + targetTextarea.className).replace(/^\s+|\s+$/g, "");
var controlPanel = document.createElement('div');
controlPanel.className = 'widearea-controlPanel';
//create close icon
var closeIcon = document.createElement('a');
closeIcon.href = 'javascript:void(0);';
closeIcon.className = 'widearea-icon close';
closeIcon.title = this._options.closeIconLabel;
closeIcon.onclick = function(){
_disableFullScreen.call(self);
};
//create close icon
var changeThemeIcon = document.createElement('a');
changeThemeIcon.href = 'javascript:void(0);';
changeThemeIcon.className = 'widearea-icon changeTheme';
changeThemeIcon.title = this._options.changeThemeIconLabel;
changeThemeIcon.onclick = function() {
_toggleColorScheme.call(self);
};
controlPanel.appendChild(closeIcon);
controlPanel.appendChild(changeThemeIcon);
//create overlay layer
var overlayLayer = document.createElement('div');
overlayLayer.className = 'widearea-overlayLayer ' + this._options.defaultColorScheme;
//add controls to overlay layer
overlayLayer.appendChild(currentTextArea);
overlayLayer.appendChild(controlPanel);
//finally add it to the body
document.body.appendChild(overlayLayer);
//set the focus to textarea
currentTextArea.focus();
//set the value of small textarea to fullscreen one
currentTextArea.value = targetTextarea.value;
//bind to keydown event
this._onKeyDown = function(e) {
if (e.keyCode === 27 && self._options.exitOnEsc) {
//escape key pressed
_disableFullScreen.call(self);
}
if (e.keyCode == 9) {
// tab key pressed
e.preventDefault();
var selectionStart = currentTextArea.selectionStart;
currentTextArea.value = currentTextArea.value.substring(0, selectionStart) + "\t" + currentTextArea.value.substring(currentTextArea.selectionEnd);
currentTextArea.selectionEnd = selectionStart + 1;
}
};
if (window.addEventListener) {
window.addEventListener('keydown', self._onKeyDown, true);
} else if (document.attachEvent) { //IE
document.attachEvent('onkeydown', self._onKeyDown);
}
}
/**
* Change/Toggle color scheme of WideArea
*
* @api private
* @method _toggleColorScheme
*/
function _toggleColorScheme() {
var overlayLayer = document.querySelector(".widearea-overlayLayer");
if(/dark/gi.test(overlayLayer.className)) {
overlayLayer.className = overlayLayer.className.replace('dark', 'light');
} else {
overlayLayer.className = overlayLayer.className.replace('light', 'dark');
}
}
/**
* Close FullScreen
*
* @api private
* @method _disableFullScreen
*/
function _disableFullScreen() {
var smallTextArea = document.querySelector("textarea.widearea-fullscreened");
var overlayLayer = document.querySelector(".widearea-overlayLayer");
var fullscreenTextArea = overlayLayer.querySelector("textarea");
//change the focus
smallTextArea.focus();
//set fullscreen textarea to small one
smallTextArea.value = fullscreenTextArea.value;
//reset class for targeted text
smallTextArea.className = smallTextArea.className.replace(/widearea-fullscreened/gi, "");
//and then remove the overlay layer
overlayLayer.parentNode.removeChild(overlayLayer);
//clean listeners
if (window.removeEventListener) {
window.removeEventListener('keydown', this._onKeyDown, true);
} else if (document.detachEvent) { //IE
document.detachEvent('onkeydown', this._onKeyDown);
}
}
/**
* Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
*
* @param obj1
* @param obj2
* @returns obj3 a new object based on obj1 and obj2
*/
function _mergeOptions(obj1, obj2) {
var obj3 = {}, attrname;
for (attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}
var wideArea = function (selector) {
if (typeof (selector) === 'string') {
//select the target element with query selector
var targetElement = document.querySelector(selector);
if (targetElement) {
return new WideArea(targetElement);
} else {
throw new Error('There is no element with given selector.');
}
} else {
return new WideArea(document.body);
}
};
/**
* Current WideArea version
*
* @property version
* @type String
*/
wideArea.version = VERSION;
//Prototype
wideArea.fn = WideArea.prototype = {
clone: function () {
return new WideArea(this);
},
setOption: function(option, value) {
this._options[option] = value;
return this;
},
setOptions: function(options) {
this._options = _mergeOptions(this._options, options);
return this;
}
};
exports.wideArea = wideArea;
return wideArea;
}));