orbit-theme-sdk/app/assets/javascripts/main.js

136 lines
3.5 KiB
JavaScript

(function(){
$.ajax({
url : "/data.json",
type : "get",
dataType : "json"
}).done(function(data){
$(document).ready(function(){
new dataApi(data);
})
})
// data api
var dataApi = function(data){
var api = this,
jsonData = data;
var initialize = function(){
var urlVars = getUrlVars();
if(urlVars["id"]){
var temp = window.location.pathname;
temp = temp.split("/");
show = (temp[temp.length - 1] == "show" ? true : false)
if(show){
var module = temp[temp.length - 2]
renderShowPage(module,urlVars["id"]);
}else{
renderIndexPage();
}
}else{
renderIndexPage();
}
}
var renderShowPage = function(module,id){
var content_area = $("*[data-content]"),
inner_html = content_area.html(),
d = getDataFromId(data[module],id),
re = new RegExp(/{{([^}]+)}}/g),
fields = inner_html.match(re);
if(typeof d == "undefined"){
throw new invalidShowId();
}
for(x = 0; x < fields.length; x++){
var t = fields[x].replace("{{","").replace("}}","");
inner_html = inner_html.replace(fields[x],d[t]);
}
content_area.html(inner_html);
}
var getDataFromId = function(data,id){
return data.filter(function(x){return x.id == id})[0]
}
var renderIndexPage = function(){
var elements = $("*[data-repeat]");
elements.each(function(){
var el = $(this),
inner_html = el.html(),
times = el.data("repeat"),
type = el.data("module") || null;
el.html("");
if(!type){
throw new dataTypeError();
}else{
var data = jsonData[type];
if(times > data.length){
throw new invalidShowId()
}
if(times === 0){
times = data.length - 1;
}
for(i = 0; i < times; i++){
var temp_html = inner_html,
d = (times === 0 ? data[i] : getRandomData(data)),
re = new RegExp(/{{([^}]+)}}/g),
fields = temp_html.match(re);
for(x = 0; x < fields.length; x++){
var t = fields[x].replace("{{","").replace("}}","");
switch(t){
case "link_to_show":
t = "id";
temp_html = temp_html.replace(fields[x],"/module/" + type + "/show?id=" + d[t]);
break;
case "link_to_index":
temp_html = temp_html.replace(fields[x],"/module/" + type);
break;
default:
temp_html = temp_html.replace(fields[x],d[t]);
break;
}
}
el.append(temp_html);
}
}
})
}
var getUrlVars = function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++){
hash = hashes[i].split('=');
vars.push(decodeURIComponent(hash[0]));
vars[decodeURIComponent(hash[0])] = decodeURIComponent(hash[1]);
}
if(vars[0] == window.location.href){
vars =[];
}
return vars;
}
var getRandomData = function(data){
var x = getRandomArbitrary(1,data.length) - 1;
return data[x];
}
var getRandomArbitrary = function(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
initialize();
}
var dataTypeError = function(){
this.name = "DataTypeError";
this.message = "Invalid module type. Please specify data-module='announcment|faq|..'";
}
var invalidShowId = function(){
this.name = "InvalidShowID";
this.message = "Invalid id for the data-module";
}
dataTypeError.prototype = new Error();
invalidShowId.prototype = new Error();
})();