Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
185 lines (177 sloc) 8.73 KB
if (!"console" in window) {
window.console = {log:function() {
}}
}
APOTOMO = window.APOTOMO || {};
(function() {
if (APOTOMO && APOTOMO.Widget) {
return;
}
APOTOMO.Widget = function(options) {
// Initialize
this.init(options)
};
// Widget helper methods and vars
APOTOMO.Widget.loadedStylesheet = false;
APOTOMO.Widget.WIDGET_NUMBER = -1;
APOTOMO.Widget.jsonP = function(source, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = source;
document.getElementsByTagName("head")[0].appendChild(script);
callback(script);
return script;
};
//Creates a script element in the documents head
APOTOMO.Widget.requestJavascript = function(source) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = source;
document.getElementsByTagName("head")[0].appendChild(script);
};
//Creates a script/stylesheet element in the documents head
APOTOMO.Widget.requestStylesheet = function(stylesheet_url, id) {
if (!APOTOMO.Widget.loadedStylesheet) {
var stylesheet = document.createElement("link");
stylesheet.id = id;
stylesheet.rel = "stylesheet";
stylesheet.type = "text/css";
stylesheet.href = stylesheet_url;
stylesheet.media = "all";
document.lastChild.firstChild.appendChild(stylesheet);
APOTOMO.Widget.loadedStylesheet = true;
return stylesheet;
}
};
//Removes Element
APOTOMO.removeElement = function(element) {
try {
element.parentNode.removeChild(element)
} catch(f) {
}
};
//Finds first mathing child depending from selector function
APOTOMO.elementFindFirstMatchingChild = function(element, selector) {
var childCount = element.childNodes.length;
for (var i = 0; i < childCount; i++) {
var child = element.childNodes[i];
if (child.nodeType == 1) {
if (selector(child)) {
return child;
}
var result = APOTOMO.elementFindFirstMatchingChild(child, selector);
if (result) {
return result;
}
}
}
return null;
};
APOTOMO.getElementsByClass = function (searchClass, node, tag) {
var classElements = new Array();
if (node == null)
node = document;
if (tag == null)
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
for (var i = 0,j = 0; i < elsLen; i++) {
if (pattern.test(els[i].className)) {
classElements[j] = els[i];
j++;
}
}
return classElements;
};
//Gets first element by class that is inside the provided element
APOTOMO.getFirstElementByClass = function(classname, element) {
return APOTOMO.elementFindFirstMatchingChild(element,
function(element) {
return element.className == classname;
});
};
APOTOMO.Widget.prototype = function() {
// constants
var BASE_URL = '<%= HOME_PAGE_URL %>';
var WIDGETS_PATH = '/widgets/render_event_response?';
var STYLESHEET = BASE_URL + '/stylesheets/compiled/widget_apotomo.css';
var MINIMUM_REFRESH_INTERVAL = 300000; //5 minutes
// constructor
return{init:function(options) {
var self = this;
APOTOMO.Widget.WIDGET_NUMBER += 1;
this.widget_number = APOTOMO.Widget.WIDGET_NUMBER;
var theme = options.theme ? options.theme : 'default';
//APOTOMO.Widget.requestStylesheet(STYLESHEET + theme + '.css');
APOTOMO.Widget.requestStylesheet(STYLESHEET, 'apotomo_widget_css');
this.script_element = null;
document.write('<%=escape_javascript(render_widget('apotomo_widget')) %>');
this.refresh_interval = (options.refresh_interval >= MINIMUM_REFRESH_INTERVAL) ? options.refresh_interval : MINIMUM_REFRESH_INTERVAL;
if (document.getElementsByClassName) {
this.widget_container = document.getElementsByClassName('apotomo_widget')[this.widget_number];
} else {
this.widget_container = APOTOMO.getElementsByClass('apotomo_widget')[this.widget_number];
}
this.widget_container.className = this.widget_container.className + ' ' + theme;
this.posts = APOTOMO.getFirstElementByClass('apotomo_posts', this.widget_container);
this.refresh_img = this.setRefreshOnClick();
this.refresh_timer_id = null;
this.is_updating = false;
this.autoRefresh();
// Update news callback
APOTOMO.Widget["updatePosts_" + this.widget_number] = function(data) {
self.is_updating = true;
clearTimeout(self.refresh_timer_id);
self.autoRefresh();
APOTOMO.removeElement(self.script_element);
if (data.length > 0) {
self.posts.innerHTML = '';
var posts = [];
for (var i = 0; i < data.length; i++) {
posts.push('<a href="' + data[i].url + '" target="_blank">' + data[i].title + '</a>');
}
self.posts.innerHTML = posts.join('');
}
self.hideUpdateSpinner();
self.is_updating = false;
};
},
setRefreshOnClick:function() {
var self = this;
var refresh_img = APOTOMO.getFirstElementByClass('apotomo_refresh_img', self.widget_container);
refresh_img.onclick = function() {
if (!self.is_updating) {
self.update();
}
};
return refresh_img;
},
autoRefresh:function() {
var self = this;
if (!self.is_updating) {
self.update();
}
self.refresh_timer_id = setTimeout(function() {
self.autoRefresh.call(self);
}, self.refresh_interval);
},
update:function() {
var self = this;
self.is_updating = true;
self.showUpdateSpinner();
var callback_path = 'callback=APOTOMO.Widget.updatePosts_' + self.widget_number + '&source=apotomo_widget&type=update';
APOTOMO.Widget.jsonP(BASE_URL + WIDGETS_PATH + callback_path, function(script_element) {
APOTOMO.removeElement(self.script_element);
self.script_element = script_element;
});
},
showUpdateSpinner:function() {
this.refresh_img.src = BASE_URL + '/images/ajax_loader.gif';
},
hideUpdateSpinner:function() {
this.refresh_img.src = BASE_URL + '/images/ajax_loader_static.gif';
}
}
}();
})();