Advertisement
Tyler_Elric

render_stuff.js

Jan 7th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (function(glob){
  3.     var persist = {};
  4.     persist.Renderer = (function(base_url){
  5.         var that = this;
  6.         that.templates = {}; // url:<XML Object>
  7.         that.loaded_scripts = []; // [url]
  8.         that.loaded_styles = [];  // [url]
  9.         that.load_script = (function(url){
  10.             // add to the head.
  11.             var head = document.getElementsByTagName('head')[0];
  12.             var link = document.createElement('script');
  13.             link.rel = 'stylesheet';
  14.             link.type = 'text/javascript';
  15.             link.src = url;
  16.             head.appendChild(link);
  17.         });
  18.         that.load_style = (function(url){
  19.             // add to the head.
  20.             var head = document.getElementsByTagName('head')[0];
  21.             var link = document.createElement('link');
  22.             link.rel = 'stylesheet';
  23.             link.type = 'text/css';
  24.             link.href = url;
  25.             link.media = 'all';
  26.             head.appendChild(link);
  27.         });
  28.         that.load_template = (function(url){
  29.             // insert into that.templates
  30.             var req = new XMLHttpRequest();
  31.             req.open("GET",url,false);
  32.             req.send();
  33.             that.templates[url] = req.responseXML;
  34.         });
  35.         that.render_template = (function(template_node,datastack){
  36.             // creates DOM elements based on the template_node.
  37.             // any elements with a 'repeat' recurse this function,
  38.             // and add a level to the datastack (.push(node.repeat)).
  39.             // returns the root node.
  40.             var root = document.createElement(template_node.nodeName);
  41.             var current_data = datastack[datastack.length-1];
  42.             if(current_data instanceof Object || current_data instanceof Array) {
  43.                 for(var key in current_data)(function(key){
  44.                     var obj = current_data[key];
  45.                     var t = typeof obj;
  46.                     if(t=='string'||t=='number'||t=='boolean'||obj===null) {
  47.                         root.setAttribute(key,obj);
  48.                     }
  49.                 })(key);
  50.             }           console.log(template_node,current_data);
  51.             if(template_node.childNodes.length) {
  52.                 // there are children nodes.
  53.                 // this is not a leaf node.
  54.                 // don't set any text.
  55.                 // do, however, set data-attributes.
  56.                 for(var i=0;i<template_node.childNodes.length;i++)(function(){
  57.                     // render & append each child node.
  58.                     var child = template_node.childNodes.item(i);
  59.                     var attributes = child.attributes||null;
  60.                     if(attributes!==null&&attributes.getNamedItem("repeat")!==null) {
  61.                         // this one iterates a list.
  62.                         // create a copy of the datastack,
  63.                         // push the key specified by the "repeat" attribute.
  64.                         var ndata = current_data[attributes.getNamedItem("repeat").value];
  65.                         for(var ii=0;ii<ndata.length;ii++)(function(){
  66.                             var new_stack = datastack.slice();
  67.                             new_stack.push(ndata[ii]);
  68.                             root.appendChild(that.render_template(child,new_stack));
  69.                         })();
  70.                     } else if(attributes!==null&&attributes.getNamedItem("data-value")!==null) {
  71.                         var el = document.createElement(child.nodeName);
  72.                         var field = attributes.getNamedItem("data-value").nodeValue;
  73.                         el.setAttribute("data-value",field);
  74.                         var t = typeof current_data;
  75.                         var lbl = null;
  76.                         if(field=='$'&&(t=='string'||t=='number'||t=='boolean')) {
  77.                             lbl = current_data;
  78.                         } else {
  79.                             lbl = current_data[field];
  80.                         }
  81.                         var text = document.createTextNode(lbl);
  82.                         el.appendChild(text);
  83.                         root.appendChild(el);
  84.                     } else if(child.nodeName=='#text') {
  85.                         // text node.
  86.                         // just append it anyways, I guess.
  87.                         var el = document.createTextNode(child.nodeValue);
  88.                         root.appendChild(el);
  89.                     } else {
  90.                         var el = document.createElement(child.nodeName);
  91.                         if(child.childNodes.length) {
  92.                             // there are sub-nodes here.
  93.                             el = that.render_template(child,datastack);
  94.                         }
  95.                         root.appendChild(el);
  96.                     }
  97.                 })();
  98.             } else {
  99.                 var attributes = template_node.attributes||null;
  100.                 if(attributes!==null&&attributes.getNamedItem("data-value")!==null) {
  101.                     var field = attributes.getNamedItem("data-value").nodeValue;
  102.                     root.setAttribute("data-value",field);
  103.                     var t = typeof current_data;
  104.                     var lbl = null;
  105.                     if(field=='$'&&(t=='string'||t=='number'||t=='boolean')) {
  106.                         lbl = current_data;
  107.                     } else {
  108.                         lbl = current_data[field];
  109.                     }
  110.                     var text = document.createTextNode(lbl);
  111.                     root.appendChild(text);
  112.                 }
  113.             }
  114.             return root;
  115.         });
  116.         that.render_entity = (function(entity){
  117.             // entity required fields:
  118.             //   - base-template
  119.             //   - [scripts]
  120.             //   - [styles]
  121.             var template_url = entity.base_template;
  122.             var needed_scripts = entity.modules;
  123.             var needed_styles = entity.styles;
  124.             if((that.templates[template_url]||null)===null) {
  125.                 that.load_template(template_url);
  126.             }
  127.             for(var i=0;i<needed_scripts.length;i++) {
  128.                 if(that.loaded_scripts.indexOf(needed_scripts[i])<0) {
  129.                     that.load_script(needed_scripts[i]);
  130.                 }
  131.             }
  132.             for(var i=0;i<needed_styles.length;i++) {
  133.                 if(that.loaded_styles.indexOf(needed_styles[i])<0) {
  134.                     that.load_style(needed_styles[i]);
  135.                 }
  136.             }
  137.             var template = that.templates[template_url].childNodes.item(0);
  138.             return that.render_template(template,[entity]);
  139.         });
  140.         return that;
  141.     });
  142.     glob.persist = persist;
  143. })(window);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement