This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fff.widget.js
595 lines (513 loc) · 17.2 KB
/
fff.widget.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
var finurligeFaktaWidget = (function() {
"use strict";
// Define global config object
var fff = {
'domain' : "//service.finurligefakta.dk/",
'widgetDomain' : '//service.finurligefakta.dk/widgets/',
'baseGuid' : null,
'GAProfile' : 'UA-33089188-1'
};
// Defaul conifguration options.
var options = {
'callback' : 'finurligeFaktaWidget.load',
'widget' : 'interactive',
'target' : '#fffwidget',
'style' : {
'type' : 'normal', // @todo: if style not given, fff homepage only build default is returned.
'color' : 'default'
},
'tracking' : true,
'button' : {
'reload' : true,
'create' : false
},
'event' : {
'loadComplet' : null
}
};
// Used to store widget settings for each widget index on GUID.
var settings = [];
// jQuery 1.4 or newer, so use this through out the object.
var jQ;
// Get url parameter
function getParameterByName(name) {
var match = new RegExp('[?&]' + name + '=([^&]*)')
.exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
// Enable Google Analytics tracking.
function trackEvent(events, trackPageview) {
// Add Google Analytics to the page.
if (window._gaq === undefined) {
// Build Google Analytics script tag.
var gat = jQ('<script />', {
'type' : 'text/javascript',
'async' : true,
'src' : ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'
});
// Add global tracking array.
var gaq = jQ('<script type="text/javascript">var _gaq = _gaq || [];</script>');
jQ('body').append(gat).append(gaq);
}
// Track page view.
if (trackPageview === true) {
_gaq.push(
['fff._setAccount', fff.GAProfile],
['fff._setDomainName', document.location.host],
['fff._trackPageview']
);
}
// Push track events to Google Analytics.
for (var event in events) {
_gaq.push(
['fff._setAccount', fff.GAProfile],
['fff._setDomainName', document.location.host],
['fff._trackEvent', 'Finurlig Fakta', event, events[event]]
);
}
}
// ----- / Define widget object -----
function Widget() {
this.widget = '';
this.data = {};
this.params = {};
this.reloaded = false;
this.init = function init(data, params) {
this.data = data;
this.params = params;
this.build();
this.style();
};
this.build = function build() {
this.widget = jQ("<div/>", {"class" : "fffW-" + this.params.widget + " fffW-widget"})
.append(jQ("<div />", {"class" : "fffW-innerwrapper"})
.append(jQ("<h2 />", {"class" : "fffW-title", "text" : this.data.title}))
.append(jQ("<p />", {"class" : "fffW-text"}).append(this.data.content))
);
// Add slogan
jQ('.fffW-innerwrapper', this.widget).append(jQ('<div />', { 'class' : 'fffW-slogan' })
.append(jQ('<p />', { 'class' : 'fffw-slogan-text', 'text' : 'Viden fra biblioteket' }))
);
// Check if reload button should be attached.
if (this.params.button.reload) {
var self = this;
var reload = jQ('<a class="fffw-button fffw-button-reload" href="#">Indlæs et nyt faktum</a>');
jQ('.fffW-innerwrapper', this.widget).prepend(reload);
reload.click(function (event) {
event.stopPropagation();
event.preventDefault();
// Should the event be send to GA.
if (self.params.tracking) {
trackEvent({
'Reloaded' : document.location.host
});
}
// Reset params and save ref. to this widget.
self.params.guid = null;
self.params.callback = 'finurligeFaktaWidget.reload';
jQ(self.params.target).data('widget', self);
getGuid(self.params);
});
}
if (this.params.button.create) {
var create = jQ('<a class="fffw-button fffw-button-create" href="#">Tilføj Fakta</a>');
jQ('.fffW-slogan', this.widget).prepend(create);
create.click(function(event) {
event.stopPropagation();
event.preventDefault();
// Should the event be send to GA.
if (self.params.tracking) {
trackEvent({
'New fact' : document.location.host
});
}
// Build the dialog window.
var popup = jQ('<div/>', { 'class' : 'fffw-dialog' })
.append(jQ('<h3/>', { 'class' : 'fffw-header' }).html('Bidrag med Finurlige Fakta'))
.append(jQ('<a/>', { 'class' : 'fffw-close', 'href' : '#' }).html('Close'))
.append(jQ('<iframe/>', { 'class' : 'fffw-dialog-iframe' }));
// Add overlay and insert the dialog.
jQ('body').append(jQ('<div/>', { 'class' : 'fffw-overlay' }));
jQ('body').append(popup);
// Load the form into the iframe from the homepage.
jQ('iframe', popup).attr('src', '//finurligefakta.dk/ajax/create/fact');
jQ('.fffw-close', popup).click(function() {
popup.remove();
jQ('.fffw-overlay').remove();
});
});
}
// Make sure that it now shown yet.
this.widget.hide();
};
// Apply some styling to the widget and add extra information based on
// style.
this.style = function style() {
switch (this.params.style.type) {
case 'none':
// Add source link(s).
jQ('.fffW-slogan', this.widget).before(this.getSourceLinks());
// Add author.
jQ('.fffW-slogan', this.widget).before(this.getAuthor());
// Add organization.
jQ('.fffW-slogan', this.widget).before(this.getOrganization());
break;
case 'minimal':
// Add CSS.
this.addCSS(this.params.widget, 'minimal', this.params.style.color);
// Add logo placeholder.
jQ('.fffW-innerwrapper', this.widget).prepend('<span class="fffw-logo"></span>');
break;
case 'normal':
// Add source link(s).
jQ('.fffW-slogan', this.widget).before(this.getSourceLinks());
// Add logo placeholder.
jQ('.fffW-innerwrapper', this.widget).prepend('<span class="fffw-logo"></span>');
// Add CSS.
this.addCSS(this.params.widget, 'normal', this.params.style.color);
break;
case 'full':
// Add source link(s).
jQ('.fffW-slogan', this.widget).before(this.getSourceLinks());
// Add author.
jQ('.fffW-slogan', this.widget).before(this.getAuthor());
// Add organization.
jQ('.fffW-slogan', this.widget).before(this.getOrganization());
// Add logo placeholder.
jQ('.fffW-innerwrapper', this.widget).prepend('<span class="fffw-logo"></span>');
// Add CSS.
this.addCSS(this.params.widget, 'full', this.params.style.color);
break;
}
};
this.addCSS = function addCSS(type, style, color) {
// Add CSS.
var css = fff.widgetDomain + 'css/fffw-' + type + '.' + style + '.' + color + '.css';
if (document.createStyleSheet){
document.createStyleSheet(css);
}
else {
jQ('head').append('<link media="all" rel="stylesheet" href="' + css + '" type="text/css" />');
}
};
this.getSourceLinks = function getSourceLinks() {
var sources = jQ('<div />', {
'class' : 'fffW-external-links fffW-extra'
}).append(jQ("<span />", {
'class' : 'fffW-label',
'text' : 'Læs mere:'
}));
for (var i in this.data.sources) {
sources.append(jQ("<a />", {'class' : 'fffw-link',
'rel' : 'external',
'href' : this.data.sources[i].url,
'text' : this.data.sources[i].title}));
}
return sources;
};
this.getAuthor = function getAuthor() {
return jQ('<div />', {
'class' : 'fffW-author fffW-extra'
}).append(jQ('<span />', {
'text' : 'Forfatter:',
'class' : 'fffW-label'
})).append(this.data.author);
};
this.getOrganization = function getOrganization() {
return jQ('<div />', {
'class' : 'fffW-orgnization fffW-extra'
}).append(jQ('<span />', {
'text' : 'Organisation:',
'class' : 'fffW-label'
})).append(this.data.organization);
};
// Insert the widget and fire loadComplet event.
this.insert = function insert() {
var target = jQ(this.params.target);
if (target.length != 1) {
// Target was not found on the page.
alert('The target div was not found on the page. Please add it to the page and try again.');
return -1;
}
target.html(this.widget);
// Fire loadComplet event.
if (this.params.event.loadComplet !== null) {
this.params.event.loadComplet();
}
// Show the widget.
this.show();
};
// Reload the widget with new fact from the data parameter.
this.reload = function reload(data) {
var self = this;
self.data = data;
self.reloaded = true;
self.hide(function () {
// Update title and text.
jQ('.fffW-title', self.widget).text(self.data.title);
jQ('.fffW-text', self.widget).html(self.data.content);
// Update source links etc.
switch (self.params.style.type) {
case 'none':
case 'normal':
case 'full':
jQ('.fffW-external-links', self.widget).html(self.getSourceLinks());
jQ('.fffW-author', self.widget).html(self.getAuthor());
jQ('.fffW-orgnization', self.widget).html(self.getOrganization());
break;
}
self.show();
});
};
// Hide the widget for the user.
this.hide = function hide(callback) {
this.widget.hide(0, function() {
if (callback) {
callback();
}
});
};
// Display the widget for the user.
this.show = function show(callback) {
this.widget.show(0, function() {
if (callback) {
callback();
}
});
};
}
/************
* Interactive inherit from widget
*/
function InteractiveWidget() {}
InteractiveWidget.prototype = new Widget();
// Override show method.
InteractiveWidget.prototype.show = function() {
this.widget.fadeIn();
};
// Override hide method.
InteractiveWidget.prototype.hide = function(callback) {
this.widget.fadeOut(function () {
if (callback) {
callback();
}
});
};
/************
* Slide in widget inherit from widget
*/
function SlideInWidget() {}
SlideInWidget.prototype = new Widget();
// Create target div to insert the widget into.
SlideInWidget.prototype.createTarget = function() {
var target;
if (this.params.target.charAt(0) === '#') {
target = jQ('<div />', {
'id' : this.params.target.substring(1)
});
}
else {
target =jQ('<div />', {
'class' : this.params.target.substring(1)
});
}
target.addClass('fffW-widget-wrapper');
jQ('body').append(target);
};
// Override show method.
SlideInWidget.prototype.show = function() {
var self = this;
// If at bottom when loaded, slide in the widget (with 10px buffer).
if (jQ(window).scrollTop() >= jQ(document).height() - (jQ(window).height() + 10)) {
self.slideIn();
}
// Slide in if bottom of page is reached.
jQ(window).scroll(function() {
if (jQ(window).scrollTop() >= jQ(document).height() - (jQ(window).height() + 10)) {
self.slideIn();
}
});
};
// Override hide method.
SlideInWidget.prototype.hide = function(callback) {
if (this.reloaded) {
this.widget.fadeOut(function () {
if (callback) {
callback();
}
});
}
else {
jQ('.fffW-slidein').animate({
width : '0px'
}, {
complete : function() {
jQ('.fffW-slidein').removeClass('active');
if (callback) {
callback();
}
}
}, 1000);
}
};
// Implementation of slide in aninmation.
SlideInWidget.prototype.slideIn = function() {
var self = this;
if (self.reloaded) {
// The widget have been reloaded so fade not slide.
self.widget.fadeIn();
self.reloaded = false;
}
else {
var slideIn = jQ('.fffW-slidein');
if (!slideIn.hasClass('active')) {
// The widget have not been slide in, so do it.
self.widget.show();
slideIn.width('0px').animate({
width : '370px'
}, {
complete : function() {
slideIn.addClass('active');
}
}, 1000);
}
}
};
// ----- / Create Widgets -----
function getGuid(params) {
if (params.guid !== null) {
getFactData(params);
} else {
jQ.getJSON(fff.domain + "?method=getGuid&api-key=714800b6f8ab26ce459c2d7ee11b15c3&callback=?", function(rtnjson) {
params.guid = rtnjson.guid;
// @todo: Add check for existing guids
getFactData(params);
});
}
}
// Reload callback that retives the widget and reloads it with new data.
function reloadWidget(data) {
var params = settings[data.guid];
var widget = jQ(params.target).data('widget');
widget.reload(data);
}
// Get data
function getFactData(params) {
var method = "getFact";
// Push parameters to settings object for later retrieval
settings[params.guid] = params;
jQ.ajax({
url: fff.domain,
cache: true,
data: {'guid' : params.guid, 'method' : method, 'api-key' : '714800b6f8ab26ce459c2d7ee11b15c3' },
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: params.callback
});
}
// Returns object from json
function loadFact(data) {
var params = settings[data.guid];
var widget;
// Call appropriate function to create widget
switch (params.widget) {
case 'interactive':
widget = new InteractiveWidget();
widget.init(data, params);
widget.insert();
break;
case 'slidein':
widget = new SlideInWidget();
widget.init(data, params);
widget.createTarget();
widget.insert();
break;
default:
alert('The selected firnulig fakta widget type is not supported');
break;
}
}
function initializeFramework(ncjQuery) {
// Set jQuery
jQ = ncjQuery;
// Expand configuration parameters
// Get GUID from url string
fff.baseGuid = getParameterByName("fffGuid");
// Start creating some widgets
// Loop through configuration array
jQ.each(fffWidgetConfig, function(i, params) {
// Legacy, what was the idea (force load content) ?
params.guid = fff.baseGuid;
// Merge default configuration into params.
params = jQ.extend({}, options, params);
// Fix default style.
if (!params.style.color) {
params.style.color = 'default';
}
if (!params.style.type) {
params.style.type = 'minimal';
}
// Enable event tracking (Google Analytics).
if (params.tracking) {
trackEvent({
'Loaded at' : document.location.host,
'Widget type' : params.widget,
'Widget style' : params.style.type,
'Widget color' : params.style.color
}, true);
}
// Activate the widget.
getGuid(params);
});
}
return {
init: initializeFramework,
load: loadFact,
reload: reloadWidget
};
}());
/************************************
** **
** Include JQuery problematically **
** **
************************************/
(function() {
"use strict";
// Helper function to load jQuery.
var load_javascript = function(url, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
if (script.readyState) { //IE fix
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
}
else { //Others
script.onload = function () {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
};
// Load jQuery 1.4.0 as that's the first known version to support jsonp as we
// uses it in the widgets to load facts. But only if an newer version is not
// found.
if (typeof(jQuery) !== "undefined" && parseFloat(jQuery.fn.jquery) >= 1.4) {
jQuery(document).ready(function() {
finurligeFaktaWidget.init(jQuery);
});
}
else {
load_javascript("https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js", function () {
var jQ = jQuery.noConflict(true);
// Start the widget parsing in jQuery 1.4.0.
jQ(document).ready(function() {
finurligeFaktaWidget.init(jQ);
});
});
}
})();