Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.  
  3.     window.app = this;
  4.  
  5.     var config = {
  6.         config_file:'data/config.js'
  7.     };
  8.  
  9.     var lang = {loading:{config:'Loading application config...'}};
  10.  
  11.     var console = {
  12.         log:function(){
  13.             window.console.log.apply(window.console,arguments);
  14.         }
  15.     };
  16.  
  17.     function load_script(src,callback) {
  18.  
  19.         window.callback = callback;
  20.         $('.jsonp').remove();
  21.  
  22.         var script = document.createElement('script');
  23.             script.type = 'text/javascript';
  24.             script.src = src;
  25.             script.className = "jsonp";
  26.             script.async = false;
  27.  
  28.         $('head')[0].appendChild(script);
  29.  
  30.     }
  31.  
  32.  
  33.     /* Content module */
  34.  
  35.     var content = this.content = {
  36.  
  37.         set:function(content){
  38.             this.getElement().html(content);
  39.         },
  40.         getElement:function(){
  41.             return $('#main');
  42.         },
  43.         add:function(content){
  44.             this.getElement().append(content);
  45.         },
  46.         localize:function(){
  47.             $('[data-translate]').each(function(){
  48.                 var translate_value = $(this).attr('data-translate');
  49.                 if(lang.translate[translate_value]!=undefined){
  50.                     $(this).text(lang.translate[translate_value]);
  51.                 }
  52.             });
  53.         }
  54.     };
  55.  
  56.     var page = this.page = {
  57.  
  58.         init:function(page_data){
  59.             content.set('');
  60.  
  61.             $('#title').html(page_data.title);
  62.             $('#subtext').html(page_data.subtitle != undefined ? page_data.subtitle : '');
  63.  
  64.  
  65.             var generate_tags = this.generate_tags;
  66.  
  67.             page_data.data.forEach(function(el){
  68.                 if(el.type=='p'){
  69.                     content.add(generate_tags('p',el.data,el.attributes));
  70.                 }
  71.                 else if(block_processors[el.type] != undefined){
  72.                     block_processors[el.type](content,el);
  73.                 }
  74.                 else{
  75.                     console.log('Unknown type "'+el.type+'"');
  76.                 }
  77.             });
  78.         },
  79.  
  80.         load:function(filename){
  81.             load_script(config.paths.articles+filename+'.js',function(data){
  82.                 $('.toc li').removeClass('active');
  83.                 $('.toc li a[href="#'+filename+'"]').parent().addClass('active');
  84.                 page.afterLoad(data);
  85.             });
  86.         },
  87.  
  88.         afterLoad:function(data){
  89.             page.init(data);
  90.         },
  91.  
  92.         generate_tags:function(name,data,attributes){
  93.             if(typeof data != 'object'){
  94.                 var attributes_string = '';
  95.                 if(attributes != undefined){
  96.                     for(var key in attributes){
  97.                         attributes_string+=' '+key+'="'+attributes[key]+'"';
  98.                     }
  99.                 }
  100.                 return '<'+name+attributes_string+'>'+data+'</'+name+'>';
  101.             }
  102.  
  103.             else{
  104.                 var tags = '';
  105.                 data.forEach(function(el){
  106.                     tags += page.generate_tags(name, el, attributes);
  107.                 });
  108.                 return tags;
  109.             }
  110.         }
  111.     };
  112.  
  113.  
  114.     /* Terms of content module */
  115.  
  116.     var toc = this.toc = {
  117.         init:function(data){
  118.             var list = $('<ul class="toc"/>');
  119.             for(var index in data){
  120.                 var list_item = $('<li><a href="#'+data[index].file+'">'+data[index].title+'</a></li>');
  121.                 list.append(list_item);
  122.             }
  123.             $('#toc').append(list);
  124.         },
  125.         load:function(){
  126.             content.set(lang.loading.toc);
  127.             load_script(config.paths.data+config.paths.toc,function(toc_data){
  128.                 toc.init(toc_data);
  129.                 toc.afterLoad(toc_data);
  130.             });
  131.         },
  132.         afterLoad:function(data){
  133.             if(window.location.hash){
  134.                 page.load(window.location.hash.substr(1));
  135.             }
  136.             else{
  137.                 page.load(data[config.firstload].file);
  138.             }
  139.         }
  140.     };
  141.  
  142.     /* Plugins module */
  143.  
  144.     var block_processors = {};
  145.  
  146.     var plugins = this.plugins = {
  147.         load:function(plugins_list){
  148.             if(plugins_list.length>0){
  149.                 this.loadPlugin(plugins_list[0],function(){
  150.                     plugins.load(plugins_list.slice(1));
  151.                 });
  152.             }
  153.             else{
  154.                 plugins.afterLoad();
  155.             }
  156.  
  157.         },
  158.         loadPlugin:function(plugin_name,load_next_plugin){
  159.             load_script(config.paths.plugins+plugin_name+'.js',function(plugin_name,plugin_callback){
  160.                 block_processors[plugin_name] = plugin_callback;
  161.                 load_next_plugin();
  162.             });
  163.         },
  164.         afterLoad:function(){
  165.             toc.load();
  166.         }
  167.     };
  168.  
  169.     /* Startup functions */
  170.  
  171.     var init = this.init = function(){
  172.         content.set(lang.loading.config);
  173.         load_config();
  174.     };
  175.  
  176.     var load_config = function(){
  177.         load_script(config.config_file,function(data){
  178.             config = data;
  179.             load_lang();
  180.         });
  181.     };
  182.  
  183.     var load_lang = function(){
  184.         load_script(config.paths.data+config.paths.lang,function(language_data){
  185.             lang = language_data;
  186.             content.localize();
  187.             plugins.load(config.plugins);
  188.         });
  189.     };
  190.  
  191.     /* Running script on dom content ready */
  192.  
  193.     $(function(){
  194.         init();
  195.     });
  196.  
  197.     $(window).on('hashchange',function(a){
  198.         var location = window.location.hash.substr(1);
  199.         page.load(location);
  200.     });
  201.  
  202. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement