-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
237 lines (235 loc) · 6.52 KB
/
page.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
(function ($) {
$.page = function (data) {
var body = $('body');
if (data.title) {
$('head').append($('<title></title>').text(data.title));
}
$.page.append(data.items, body);
body.addContextmenu({
items: [
{
type: 'item',
label: 'Reload',
action: function () {
window.location.reload(true);
},
contextmenu: false,
},
{
type: 'item',
label: 'Reload page from cache',
action: function () {
window.location.reload(false);
},
contextmenu: false,
},
]
})
};
$.page.append = function (itemlist, parent) {
for (var i = 0; i < itemlist.length; i++) {
var type = itemlist[i].type;
var item = {
};
$.extend(item, $.page[type].defaults ? $.page[type].defaults : {
}, itemlist[i])
parent.append($.page[type](item));
}
};
$.page.menu = function (data) {
var menu = $('<div class="ui menu"></div>');
menu.addClass(data.menutype);
$.page.append(data.items, menu);
return menu;
};
$.page.menu.defaults = {
menutype: 'fixed',
};
$.page.item = function (data) {
var item = $('<a class="item"></a>').html(data.label).attr('href', data.url);
try {
item.attr(data.attr);
} catch (err) {
}
if (data.action) {
item.on('click', data.action);
}
if (item.contextmenu) {
item.addContextmenu({
items: [
{
type: 'item',
label: 'Copy link',
action: function () {
$.copyTextToClipboard(data.href);
},
contextmenu: false,
},
{
type: 'item',
label: 'Open link in new tab',
href: data.href,
attr: {
target: '_blank'
},
contextmenu: false,
},
]
})
}
return item;
};
$.page.item.defaults = {
label: '',
url: '#',
contextmenu: true,
attr: {
},
};
$.page.search = function (data) {
var search = $('<div class="ui category search item"><div class="ui transparent icon input"><input class="prompt" type="text"><i class="search link icon"></i></div><div class="results"></div></div>');
search.results = search.find('.results');
search.submit = search.find('i');
search.input = search.find('input');
search.input.attr('placeholder', data.label);
var submit = function () {
var searchData = {
value: search.input[0].value,
stopLoading: function () {
search.removeClass('loading');
},
showResults: function (results) {
search.removeClass('loading');
},
}
data.action(searchData);
search.addClass('loading');
};
search.input.on('keypress', function (event) {
if (event.which === 13) {
submit(event);
}
});
search.submit.on('click', submit);
return search;
};
$.page.search.defaults = {
label: 'Search...'
};
$.page.text = function (data) {
var text = $('<p class="item"></p>').html($.markup(data.text));
return text;
};
$.page.text.defaults = {
text: ''
};
$.fn.addContextmenu = function (data) {
$(this).on('contextmenu', function (event) {
if (event.timeStamp !== $.contextmenu.lastTimestamp) {
$.contextmenu.menu.blur();
$.contextmenu.lastTimestamp = event.timeStamp;
}
if (data.stopPropagation) {
event.preventDefault();
event.stopPropagation();
}
console.log(data);
$.page.append(data.items, $.contextmenu.menu);
});
};
$.contextmenu = {
};
$.contextmenu.lastTimestamp = '';
$.contextmenu.menu = $('<div class="ui fixed menu vertical transition hidden"></div>');
$.contextmenu.menu.on('blur', function () {
$.contextmenu.menu.removeClass('visible animating slide down in');
$.contextmenu.menu.addClass('hidden');
$.contextmenu.menu.html('');
});
$(function () {
$('html').append($.contextmenu.menu);
$(document).on('click', function () {
$.contextmenu.menu.blur();
})
$(document).on('contextmenu', function (event) {
event.preventDefault();
if (event.timeStamp !== $.contextmenu.lastTimestamp) {
$.contextmenu.menu.blur();
$.contextmenu.lastTimestamp = event.timeStamp;
}
if ($.contextmenu.menu.html()) {
$.contextmenu.menu.addClass('animating slide down in');
$.contextmenu.menu.css('left', event.pageX);
$.contextmenu.menu.css('top', event.pageY);
setTimeout(function () {
$.contextmenu.menu.removeClass('animating slide down in');
if ($.contextmenu.menu.html()) {
$.contextmenu.menu.addClass('visible');
$.contextmenu.menu.focus();
}
}, 500);
}
})
})
$.copyTextToClipboard = function (text) {
var textArea = $('<textarea></textarea>').css({
position: 'fixed',
top: 0,
left: 0,
width: '2em',
height: '2em',
padding: 0,
border: 'none',
outline: 'none',
boxShadow: 'none',
background: 'transparent',
}).attr('value', text).appendTo('body') [0];
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
//console.log('Copying text command was ' + msg);
} catch (err) {
//console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
};
$.markup = function (text) {
var keys = $.markup.keys;
var key = '';
for(var i in keys){
key += i;
}
console.log(key);
var regex = new RegExp('([' + key + ']+)()?(.+?)\\2?\\1','g');
//var regex = /([*`_^]+)()?(.+?)\2?\1/g;
var result = text;
var m;
var start = '';
var end = '';
while ((m = regex.exec(text)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
} //do the replacemnts
for (var i = 0; i < m[1].length; i++) {
start = start + keys[m[1][i]][0];
end = keys[m[1][i]][1] + end;
}
result = result.replace(m[0], start + m[3] + end);
console.log(result);
console.log(m);
console.log(start);
start = '';
end = '';
}
return result;
};
$.markup.keys = {
'*': ['<b>','</b>'],
'`': ['<i>','</i>'],
'_': ['<u>','</u>'],
'^': ['<i class="',' icon"></i>'],
};
return;
}) ($)