-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyteclubswagsquad.js
307 lines (256 loc) · 7.72 KB
/
byteclubswagsquad.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
var hoveringOverLink = false;
var hoverBox = {
classes: {
box: '.hoverpediabox',
noOverflowDiv: '.hoverpediabox_noverflow',
loadingImg: '.hoverpediabox_loading'
},
ids: {
top: '#hoverpedia_top',
left: '#hoverpedia_left',
right: '#hoverpedia_right',
bottom: '#hoverpedia_bottom'
},
startHTML: function(id) {
return '<div class="hoverpediabox arrow_box" id="' + id + '">' +
'<div class="hoverpediabox_noverflow">' +
'<img class="hoverpediabox_loading" alt="Loading ...">'+
'<h2></h2>' +
'<p>';
},
endHTML: '</p>' +
'</div>' +
'</div>',
html: {
top: function() {
return hoverBox.startHTML('hoverpedia_top') + hoverBox.endHTML;
},
left: function() {
return hoverBox.startHTML('hoverpedia_left') + hoverBox.endHTML;
},
right: function() {
return hoverBox.startHTML('hoverpedia_right') + hoverBox.endHTML;
},
bottom: function() {
return hoverBox.startHTML('hoverpedia_bottom') + hoverBox.endHTML;
},
},
/**
* Sets the inner HTML of the box
* @param html
*/
setHTML: function(html) {
$(this.classes.box + ' > ' + this.classes.noOverflowDiv + ' > p').html(html);
},
/**
* Injects the HTML necessary to display the popup box
*/
appendBoxToBody: function () {
if (this.element_Top === null) {
$('body').append(this.html.top())
.append(this.html.left())
.append(this.html.right())
.append(this.html.bottom());
}
},
/**
* Injects the HTML and Creates the jquery element for the popup box
*/
initialiseHoverBoxElement: function() {
this.appendBoxToBody();
this.element_Top = $(this.ids.top);
this.element_Left = $(this.ids.left);
this.element_Right = $(this.ids.right);
this.element_Bottom = $(this.ids.bottom);
},
element_Top: null,
element_Right: null,
element_Left: null,
element_Bottom: null,
showElement: function(x, y) {
var boxWidth = hoverBox.element_Top.outerWidth();
var boxHeight = hoverBox.element_Top.outerHeight();
var wind = $(window);
if (x - boxWidth / 2 < 0) {
hoverBox.hideElements();
hoverBox.element_Right.show();
} else if (x + boxWidth / 2 > wind.width()) {
hoverBox.hideElements();
hoverBox.element_Left.show();
} else if (y - boxHeight - 30 < wind.scrollTop()) {
hoverBox.hideElements();
hoverBox.element_Bottom.show();
} else {
hoverBox.hideElements();
hoverBox.element_Top.show();
}
},
hideElements: function() {
hoverBox.element_Right.hide();
hoverBox.element_Left.hide();
hoverBox.element_Top.hide();
hoverBox.element_Bottom.hide();
},
/**
* Sets the x,y position of the popup box
* @param x
* @param y
*/
setPosition: function(x, y) {
var boxWidth = hoverBox.element_Top.outerWidth();
var boxHeight = hoverBox.element_Top.outerHeight();
var top = {
box: y - boxHeight - 30,
bottomBox: y + 30,
leftBox: y - boxHeight / 2,
rightBox: y - boxHeight / 2
};
var left = {
box: x - boxWidth / 2,
bottomBox: x - boxWidth / 2,
leftBox: x - boxWidth - 30,
rightBox: x + 30
};
this.element_Top.css(
{
top: top.box,
left: left.box
}
);
this.element_Right.css(
{
top: top.rightBox,
left: left.rightBox
}
);
this.element_Left.css(
{
top: top.leftBox,
left: left.leftBox
}
);
this.element_Bottom.css(
{
top: top.bottomBox,
left: left.bottomBox
}
);
}
};
/**
* Returns the Title and the first paragraph, shortened to fit within charLimit
* @param data Data in form of HTML
* @param charLimit The character limit to display in the popup box
* @returns {{title: string, paragraph: string }}
*/
function getTitleAndParagraph(data, charLimit) {
var paragraph = "", title = "open";
var content = document.body.textContent || document.body.innerText;
var teacherBlocked = "Access to the web page you were trying to visit has been blocked in accordance with company policy. Please contact your system administrator if you believe this is in error.";
var studentBlocked = "Access to this page has been blocked by your network administrator.";
var el = $('<div></div>'); //create dummy element
el.html(data);
var hasText = data.indexOf(teacherBlocked)!==-1 || data.indexOf(studentBlocked)!==-1;
if(hasText){
title = "blocked";
}
/*
var el = $('<div></div>'); //create dummy element
el.html(data);
var P = $('#mw-content-text > p', el).first();
if (P.text().match('Coordinates') !== null) {
P = $('#mw-content-text > p', el).eq(1);
}
var title = $('#firstHeading', el).text();
var paragraph = {
html: P.html(),
text: P.text()
};
if (paragraph.html == null || paragraph.html == undefined || paragraph.html == "") {
paragraph.html = "Can't find any content to fetch on this page";
}*/
return {
title: title,
paragraph: paragraph.html
};
}
/**
* Creates an AJAX request to page to fetch requested page title
* @param pageTitle
*/
function fetch(pageTitle) {
var charLimit = 400;
$.get(pageTitle,
function(data) {
var result = getTitleAndParagraph(data, charLimit);
hoverBox.setHTML(result.paragraph);
hoveringOverLink = true;
}
);
}
/**
* Controls mousing over a link
* @param anchor
*/
var mouseOverLink = function(anchor) {
var href = anchor.attr('href');
fetch(href);
};
/**
* Controls mousing off a link
*/
var mouseLeaveLink = function(anchor) {
hoveringOverLink = false;
removeAnchorOutlines(anchor);
};
function outlineAnchors(anchor) {
var href = anchor.attr('href');
anchor.css('outline', '2px dashed #ff6406');
}
function removeAnchorOutlines(anchor) {
anchor.css('outline', 'none');
}
/**
* When document is ready
*/
$( function() {
var a = $('a');
a.each(function(index) {
var anchor = $(this);
outlineAnchors(anchor);
});
hoverBox.initialiseHoverBoxElement();
/**
* hoverIntent is a delayed version of hover, provided by plugin
* It waits for the mouse to be nearly stationary before activating
*/
a.hoverIntent(
function() {
mouseOverLink($(this));
},
function() {
mouseLeaveLink($(this));
}
);
//To put an outline on the link as soon as mouse is over it, to indicate it's a "hoverpedia" link
a.hover(
function() {
var $this = $(this);
outlineAnchors($this);
},
function() {
mouseLeaveLink($(this));
}
);
/**
* Sets the popup box to the mouse cursor position every time mouse is moved
*/
$("body").mousemove(function(e) {
hoverBox.setPosition(e.pageX, e.pageY);
if (hoveringOverLink) {
hoverBox.showElement(e.pageX, e.pageY);
} else {
hoverBox.hideElements();
}
});
});