js/utils.js
'use strict';
/**
* noop a empty function
*/
function noop() {}
/**
* validate if is not validate will return an Error with the message
*
* @param {boolean} isValid
* @param {string} message
*/
function validate(isValid, message) {
return isValid ? null : new Error(message);
}
/**
* callbackTimeout if the onSuccess is not called and returns true in the timelimit then onTimeout will be called
*
* @param {number} timer
* @param {function} onSuccess
* @param {function} onTimeout
*/
function callbackTimeout(timer, onSuccess, onTimeout) {
var callback, timeout;
timeout = setTimeout(function () {
onSuccess = noop;
onTimeout();
}, timer);
callback = function () {
// TODO avoid leaking arguments
// https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments
if (onSuccess.apply(this, arguments)) {
clearTimeout(timeout);
}
};
return callback;
}
/**
* createElementInEl
*
* @param {HTMLElement} parent
* @param {string} tagName
* @param {string} id
*/
function createElementInEl(parent, tagName, id) {
var nEl = document.createElement(tagName);
if (id) nEl.id = id;
parent.appendChild(nEl);
return nEl;
}
/**
* createIframeWithContent
*
* @param {HTMLElement} parent
* @param {string} template simple template using {{var}}
* @param {object} data
*/
function createIframeWithContent(parent, template, data) {
var iframe = createIframe(parent, null, data.zIndex);
if (!setIframeContent(iframe, simpleTemplate(template, data))) return;
return iframe;
}
/**
* createIframe
*
* @param {HTMLElement} parent
* @param {string} url
*/
function createIframe(parent, url, zIndex) {
var nEl = document.createElement('iframe');
nEl.src = url || 'about:blank';
nEl.marginWidth = '0';
nEl.marginHeight = '0';
nEl.frameBorder = '0';
nEl.width = '100%';
nEl.height = '100%';
setFullSizeStyle(nEl);
if(zIndex){
nEl.style.zIndex = zIndex;
}
nEl.setAttribute('SCROLLING','NO');
parent.innerHTML = '';
parent.appendChild(nEl);
return nEl;
}
function setFullSizeStyle(element) {
if(element) {
element.style.position = 'absolute';
element.style.left = '0';
element.style.top = '0';
element.style.margin = '0px';
element.style.padding = '0px';
element.style.border = 'none';
element.style.width = '100%';
element.style.height = '100%';
}
}
/**
* simpleTemplate
*
* @param {string} template
* @param {object} data
*/
function simpleTemplate(template, data) {
Object.keys(data).forEach(function (key) {
var value = (typeof value === 'object') ? JSON.stringify(data[key]) : data[key];
template = template.replace(new RegExp('{{' + key + '}}', 'g'), value);
});
return template;
}
/**
* setIframeContent
*
* @param {HTMLIframeElement} iframeEl
* @param content
*/
function setIframeContent(iframeEl, content) {
var iframeDoc = iframeEl.contentWindow && iframeEl.contentWindow.document;
if (!iframeDoc) return false;
iframeDoc.write(content);
return true;
}
/**
* extend object with keys from another object
*
* @param {object} toExtend
* @param {object} fromSource
*/
function extend(toExtend, fromSource) {
Object.keys(fromSource).forEach(function(key) {
toExtend[key] = fromSource[key];
});
return toExtend;
}
/**
* unique will create a unique string everytime is called, sequentially and prefixed
*
* @param {string} prefix
*/
function unique(prefix) {
var count = -1;
return function () {
return prefix + '_' + (++count);
};
}
module.exports = {
noop: noop,
validate: validate,
callbackTimeout: callbackTimeout,
createElementInEl: createElementInEl,
createIframeWithContent: createIframeWithContent,
createIframe: createIframe,
setFullSizeStyle: setFullSizeStyle,
simpleTemplate: simpleTemplate,
setIframeContent: setIframeContent,
extend: extend,
unique: unique
};