-
Notifications
You must be signed in to change notification settings - Fork 0
/
diydudley-tools.js
189 lines (166 loc) · 5.18 KB
/
diydudley-tools.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
function $(e) { return document.getElementById(e); }
function get_documentOffsetTop(e) {
var i = 0;
if (e.offsetParent) {
do { i += e.offsetTop; } while (e = e.offsetParent);
}
return i;
}
function get_documentOffsetLeft(e) {
var i = 0;
if (e.offsetParent) {
do { i += e.offsetLeft; } while (e = e.offsetParent);
}
return i;
}
/* from http://james.padolsey.com/javascript/wordwrap-for-javascript/ */
function wordwrap(str, width, brk, cut) {
brk = brk || '\n';
width = width || 75;
cut = cut || false;
if (!str) { return str; }
var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
return str.match( RegExp(regex, 'g') ).join( brk );
}
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,'');
}
function htmlentities( s ){
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: htmlentities('Kevin & van Zonneveld');
// * returns 1: 'Kevin & van Zonneveld'
var div = document.createElement('div');
var text = document.createTextNode(s);
div.appendChild(text);
return div.innerHTML;
}
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
/* cookie functions from quirksmode.org, with tiny modifications */
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = dud_cookie_prefix + name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = dud_cookie_prefix + name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(dud_cookie_prefix + name,"",-1);
}
function setStorageData(name,value) {
if (typeof(localStorage) == 'undefined') return;
localStorage.setItem(dud_cookie_prefix + name, value);
}
function getStorageData(name) {
if (typeof(localStorage) == 'undefined') return null;
return localStorage.getItem(dud_cookie_prefix + name);
}
function eraseStorageData(name) {
if (typeof(localStorage) == 'undefined') return;
localStorage.removeItem(dud_cookie_prefix + name);
}
function insertAtCursor(elem, addval)
{
tmp = $(elem);
if (tmp == undefined) return;
//IE support
if (document.selection) {
tmp.focus();
sel = document.selection.createRange();
sel.text = addval;
}
//MOZILLA/NETSCAPE support
else if (tmp.selectionStart || tmp.selectionStart == '0') {
var startPos = tmp.selectionStart;
var endPos = tmp.selectionEnd;
tmp.value = tmp.value.substring(0, startPos) + addval + tmp.value.substring(endPos, tmp.value.length);
} else {
tmp.value += addval;
}
}
function button(text, onclick, usespan, hreffi, disable, id)
{
if (!hreffi) hreffi = '#';
if (usespan == undefined) {
usespan = 'a';
} else {
usespan = 'span';
}
var txt = "<"+usespan+" class='button"+((disable && (disable==1))?"_disabled":"")+"'";
if (usespan == 'a') " href='"+hreffi+"'";
if (onclick && !disable) txt += " onclick='" + onclick + "'";
if (id) txt += " id='"+id+"'";
txt += ">"+text+"</"+usespan+">";
return txt;
}
function button_disabled(text, onclick, usespan, hreffi)
{
return button(text, onclick, usespan, hreffi, 1);
}
function togglebutton(text, onclick, usespan, hreffi, disable, id)
{
if (!hreffi) hreffi = '#';
if (usespan == undefined) {
usespan = 'a';
} else {
usespan = 'span';
}
var txt = "<"+usespan+" class='togglebutton"+((disable && (disable==1))?"_on":"_off")+"'";
if (usespan == 'a') " href='"+hreffi+"'";
if (onclick) txt += " onclick='" + onclick + "'";
if (id) txt += " id='"+id+"'";
txt += ">"+text+"</"+usespan+">";
return txt;
}
function pen_getcolor(color)
{
var fg = color;
if (Object.prototype.toString.call(color) === '[object Array]') {
fg = color[Math.floor(Math.random() * color.length)];
}
if (fg == "random")
fg = colors[Math.floor(Math.random() * (colors.length-1))+1];
return fg;
}
function datspanclass(dat, forcebg)
{
var sclass = '';
if (dat.rev == 1) {
if (dat.fg == undefined) { sclass += " f_black b_gray"; } else { sclass += " f_black b_"+dat.fg; }
} else {
if (dat.fg == undefined) { sclass += " f_gray"; } else { sclass += " f_"+dat.fg; }
if (forcebg == 1) sclass += ' b_black';
}
if (dat.bold == 1) { sclass += " f_bold"; }
if (dat.ita == 1) { sclass += " f_ita"; }
if (dat.ul == 1) { sclass += " f_ul"; }
if (dat.cur) { sclass += " f_cur"; }
return sclass.trim();
}
function get_data_span(dat)
{
span = datspanclass(dat);
if (!dat.chr) { c = " "; } else { c = dat.chr; }
if (span) {
return "<span class='" + span + "'>" + c + "</span>";
} else {
return c;
}
}