Skip to content

Commit

Permalink
keep (function(){...})()
Browse files Browse the repository at this point in the history
The form (function(){...})() is a function literal that is executed
immediately. What this means is that you have defined a function and
you are calling it immediately.

This form is useful for information hiding and encapsulation since
anything you define inside that function remains local to that function
and inaccessible from the outside world.
  • Loading branch information
vasconsaurus committed Aug 16, 2023
1 parent 8499ad0 commit a88d01e
Showing 1 changed file with 74 additions and 72 deletions.
146 changes: 74 additions & 72 deletions public/javascripts/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,80 @@
// This happens on the child window
var Pender = {};

'use strict';

// Alert parent window when the height changes
var htmlHeight = 0;
if (!Pender.id) {
Pender.id = md5(document.location.search.replace(/^\?url=/, ''));
}
var checkHTMLHeight = function() {
var height = document.getElementsByTagName('BODY')[0].offsetHeight;
if (height !== htmlHeight) {
htmlHeight = height;
window.parent.postMessage(['setHeight', Pender.id, htmlHeight].join(';'), '*');
}
setTimeout(checkHTMLHeight, 100);
};

var isElementOnViewPort = function(el, data) {
var rect = el.getBoundingClientRect(),
h = data[0], w = data[1], tp = data[2], left = data[3];

var offset = 500;

return (
rect.top + tp >= 0 &&
rect.left + left >= 0 &&
rect.bottom + tp <= (h + offset) &&
rect.right + left <= w
);
};

var lazyLoad = function(data) {
// Nothing to do right now
};

var messageCallback = function(e) {
var data = e.data.toString().split(';'),
type = data.shift();

for (var i = 0; i < data.length; i++) {
data[i] = parseInt(data[i], 10);
}

switch (type) {
// Lazy load
case 'lazyLoad':
lazyLoad(data);
break;
(function() {
'use strict';

// Alert parent window when the height changes
var htmlHeight = 0;
if (!Pender.id) {
Pender.id = md5(document.location.search.replace(/^\?url=/, ''));
}
};

var resizeOrScrollCallback = function() {
var h = window.innerHeight || document.documentElement.clientHeight,
w = window.innerWidth || document.documentElement.clientWidth,
data = [h, w, 0, 0, 0, 0];
lazyLoad(data);
};

if (!window.addEventListener) {
window.attachEvent('onmessage', messageCallback);
if (window === window.parent) {
window.attachEvent('onscroll', resizeOrScrollCallback);
window.attachEvent('onresize', resizeOrScrollCallback);
window.attachEvent('onload', resizeOrScrollCallback);
var checkHTMLHeight = function() {
var height = document.getElementsByTagName('BODY')[0].offsetHeight;
if (height !== htmlHeight) {
htmlHeight = height;
window.parent.postMessage(['setHeight', Pender.id, htmlHeight].join(';'), '*');
}
setTimeout(checkHTMLHeight, 100);
};

var isElementOnViewPort = function(el, data) {
var rect = el.getBoundingClientRect(),
h = data[0], w = data[1], tp = data[2], left = data[3];

var offset = 500;

return (
rect.top + tp >= 0 &&
rect.left + left >= 0 &&
rect.bottom + tp <= (h + offset) &&
rect.right + left <= w
);
};

var lazyLoad = function(data) {
// Nothing to do right now
};

var messageCallback = function(e) {
var data = e.data.toString().split(';'),
type = data.shift();

for (var i = 0; i < data.length; i++) {
data[i] = parseInt(data[i], 10);
}

switch (type) {
// Lazy load
case 'lazyLoad':
lazyLoad(data);
break;
}
};

var resizeOrScrollCallback = function() {
var h = window.innerHeight || document.documentElement.clientHeight,
w = window.innerWidth || document.documentElement.clientWidth,
data = [h, w, 0, 0, 0, 0];
lazyLoad(data);
};

if (!window.addEventListener) {
window.attachEvent('onmessage', messageCallback);
if (window === window.parent) {
window.attachEvent('onscroll', resizeOrScrollCallback);
window.attachEvent('onresize', resizeOrScrollCallback);
window.attachEvent('onload', resizeOrScrollCallback);
}
window.attachEvent('onload', checkHTMLHeight);
}
window.attachEvent('onload', checkHTMLHeight);
}
else {
window.addEventListener('message', messageCallback, false);
if (window === window.parent) {
window.addEventListener('scroll', resizeOrScrollCallback, false);
window.addEventListener('resize', resizeOrScrollCallback, false);
window.addEventListener('load', resizeOrScrollCallback, false);
else {
window.addEventListener('message', messageCallback, false);
if (window === window.parent) {
window.addEventListener('scroll', resizeOrScrollCallback, false);
window.addEventListener('resize', resizeOrScrollCallback, false);
window.addEventListener('load', resizeOrScrollCallback, false);
}
window.addEventListener('load', checkHTMLHeight, false);
}
window.addEventListener('load', checkHTMLHeight, false);
}
}())

0 comments on commit a88d01e

Please sign in to comment.