forked from yuku/textoverlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.overlay.js
274 lines (237 loc) · 7.37 KB
/
jquery.overlay.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
270
271
272
273
274
/*!
* jQuery.textoverlay.js
*
* Repository: https://github.com/yuku-t/jquery-textoverlay
* License: MIT
* Author: Yuku Takahashi
*/
;(function ($) {
'use strict';
/**
* Get the styles of any element from property names.
*/
var getStyles = (function () {
var color;
color = $('<div></div>').css(['color']).color;
if (typeof color !== 'undefined') {
return function ($el, properties) {
return $el.css(properties);
};
} else { // for jQuery 1.8 or below
return function ($el, properties) {
var styles;
styles = {};
$.each(properties, function (i, property) {
styles[property] = $el.css(property);
});
return styles
};
}
})();
var entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
}
var entityRegexe = /[&<>"'\/]/g
/**
* Function for escaping strings to HTML interpolation.
*/
var escape = function (str) {
if (typeof str !== 'undefined'){
return str.replace(entityRegexe, function (match) {
return entityMap[match];
})
}
};
/**
* Determine if the array contains a given value.
*/
var include = function (array, value) {
var i, l;
if (array.indexOf) return array.indexOf(value) != -1;
for (i = 0, l = array.length; i < l; i++) {
if (array[i] === value) return true;
}
return false;
};
var Overlay = (function () {
var html, css, textareaToWrapper, textareaToOverlay, allowedProps;
html = {
wrapper: '<div class="textoverlay-wrapper"></div>',
overlay: '<div class="textoverlay"></div>'
};
css = {
wrapper: {
margin: 0,
padding: 0,
overflow: 'hidden'
},
overlay: {
position: 'absolute',
color: 'transparent',
'white-space': 'pre-wrap',
'word-wrap': 'break-word',
overflow: 'hidden'
},
textarea: {
background: 'transparent',
position: 'relative',
outline: 0
}
};
// CSS properties transport from textarea to wrapper
textareaToWrapper = ['display'];
// CSS properties transport from textarea to overlay
textareaToOverlay = [
'margin-top',
'margin-right',
'margin-bottom',
'margin-left',
'padding-top',
'padding-right',
'padding-bottom',
'padding-left',
'font-family',
'font-weight',
'font-size',
'background-color'
];
function Overlay($textarea) {
var $wrapper, position;
// Setup wrapper element
position = $textarea.css('position');
if (position === 'static') position = 'relative';
$wrapper = $(html.wrapper).css(
$.extend({}, css.wrapper, getStyles($textarea, textareaToWrapper), {
position: position
})
);
// Setup overlay
this.textareaTop = parseInt($textarea.css('border-top-width'));
this.$el = $(html.overlay).css(
$.extend({}, css.overlay, getStyles($textarea, textareaToOverlay), {
top: this.textareaTop,
right: parseInt($textarea.css('border-right-width')),
bottom: parseInt($textarea.css('border-bottom-width')),
left: parseInt($textarea.css('border-left-width'))
})
);
// Setup textarea
this.$textarea = $textarea.css(css.textarea);
// Render wrapper and overlay
this.$textarea.wrap($wrapper).before(this.$el);
// Intercept val method
// Note that jQuery.fn.val does not trigger any event.
this.$textarea.origVal = $textarea.val;
this.$textarea.val = $.proxy(this.val, this);
// Bind event handlers
this.$textarea.on({
'input.overlay': $.proxy(this.onInput, this),
'change.overlay': $.proxy(this.onInput, this),
'focus.overlay': $.proxy(this.onInput, this),
'scroll.overlay': $.proxy(this.resizeOverlay, this),
'resize.overlay': $.proxy(this.resizeOverlay, this)
});
this.strategies = [];
}
$.extend(Overlay.prototype, {
val: function (value) {
return value == null ? this.$textarea.origVal() : this.setVal(value);
},
setVal: function (value) {
this.$textarea.origVal(value);
this.renderTextOnOverlay();
return this.$textarea;
},
onInput: function (e) {
this.renderTextOnOverlay();
},
renderTextOnOverlay: function () {
var text, i, l, strategy, match, style, attributes;
attributes = '';
text = $('<div></div>').text(this.$textarea.val());
// Apply all strategies
for (i = 0, l = this.strategies.length; i < l; i++) {
strategy = this.strategies[i];
match = strategy.match;
if ($.isArray(match)) {
match = $.map(match, function (str) {
return str.replace(/(\(|\)|\|)/g, '\$1');
});
match = new RegExp('(' + match.join('|') + ')', 'g');
}
// Style attribute's string
if (strategy.css) {
style = Object.keys(strategy.css).map(function(propCSS) {
return propCSS + ': ' + strategy.css[propCSS]
}).join(';')
attributes = 'style="' + style + '"'
}
if (strategy.cssClass) {
attributes += ' class="' + strategy.cssClass + '"'
}
text.contents().each(function () {
var text, html, str, prevIndex;
if (this.nodeType != Node.TEXT_NODE) return;
text = this.textContent;
html = '';
for (prevIndex = match.lastIndex = 0;; prevIndex = match.lastIndex) {
str = match.exec(text);
if (!str) {
if (prevIndex) html += escape(text.substr(prevIndex));
break;
}
str = str[0];
html += escape(text.substr(prevIndex, match.lastIndex - prevIndex - str.length));
html += '<span ' + attributes + '>' + escape(str) + '</span>';
};
if (prevIndex) $(this).replaceWith(html);
});
}
this.$el.html(text.contents());
return this;
},
resizeOverlay: function () {
this.$el.css({ top: this.textareaTop - this.$textarea.scrollTop() });
},
register: function (strategies) {
strategies = $.isArray(strategies) ? strategies : [strategies];
this.strategies = this.strategies.concat(strategies);
return this.renderTextOnOverlay();
},
destroy: function () {
var $wrapper;
this.$textarea.off('.overlay');
$wrapper = this.$textarea.parent();
$wrapper.after(this.$textarea).remove();
this.$textarea.removeData('overlay');
this.$textarea = null;
}
});
return Overlay;
})();
$.fn.overlay = function (strategies) {
var dataKey;
dataKey = 'overlay';
if (strategies === 'destroy') {
return this.each(function () {
var overlay = $(this).data(dataKey);
if (overlay) { overlay.destroy(); }
});
}
return this.each(function () {
var $this, overlay;
$this = $(this);
overlay = $this.data(dataKey);
if (!overlay) {
overlay = new Overlay($this);
$this.data(dataKey, overlay);
}
overlay.register(strategies);
});
};
})(window.jQuery);