Advertisement
Guest User

Javascript introduction

a guest
Oct 22nd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(context){
  2.     /**
  3.      * Basic app configuration
  4.     */
  5.     var config = {
  6.         DEBUG:            true,
  7.         WRITER_SIGNATURE: "write",
  8.         LANGUAGE:         "en-US" // default language
  9.     };
  10.  
  11.     /**
  12.      * Hardcoded translation data
  13.     */
  14.     var translationData = {
  15.         "en-US": {
  16.             "hello_world":       "Hello World!", // greeting in main view
  17.             "invocation":        "invoked", // date of invokation
  18.             "styled_invocation": "[%str%]" // some decoration for better style
  19.         }
  20.     };
  21.  
  22.     /**
  23.      * Internationalization module
  24.      * Supports dynamic formatting and language pick after load
  25.     */
  26.     var i18n = (function(){
  27.         return {
  28.             format: function(source, info){ // properly formats a i18n resource
  29.                 return source.replace("%str%", info);
  30.             },
  31.             originTranslate: function(origin){
  32.                 var invoc_stf = i18n.translate("invocation") + " " + origin.lastModified;
  33.                 return i18n.format(i18n.translate("styled_invocation"), invoc_stf);
  34.             },
  35.             translate: function(message){
  36.                 var localData = translationData[config.LANGUAGE];
  37.                 return localData[message];
  38.             },
  39.             get: function(message, origin){
  40.                 var timestamp = origin.lastModified;
  41.                 if(config.DEBUG)
  42.                     return i18n.translate(message) + " " + i18n.originTranslate(origin);
  43.                 else
  44.                     return i18n.translate(message);
  45.             }
  46.         };
  47.     }());
  48.  
  49.     /**
  50.      * A clone of a document-wrapper containing valid, ready DOM
  51.     */
  52.     var fallbackDocument = function(){
  53.         var _document = function(){
  54.             this.native_context = context;
  55.             this.modules = new Array();
  56.         };
  57.         _document.prototype.clear = function(){
  58.             for(var i = 0; i < this.modules.length; i++){
  59.                 var module = this.modules[i];
  60.                 module.signalClear();
  61.             };
  62.         };
  63.  
  64.         return _document;
  65.     };
  66.  
  67.     /**
  68.      * Provides a native document, scoped to the context
  69.      * Uses a fallback if document not initialized or not supported
  70.     */
  71.     var provideDocument = function(){
  72.         if(typeof context.document == "undefined")
  73.             context.document = new fallbackDocument();
  74.         context.document.lastModified = new context.Date();
  75.         context.document.exception = function(string_exception){
  76.             this.origin = context.navigator;
  77.             this.serialized = string_exception;
  78.         };
  79.  
  80.         return context.document;
  81.     };
  82.  
  83.     /**
  84.      * Sends a data request, and tries to call the document writer
  85.     */
  86.     var documentPrinter = function(document, dataCallback){
  87.         if(dataCallback == null)
  88.             throw new document.exception("Did not receive a data callback!");
  89.         data = i18n.get(dataCallback(), document); // translate data into proper lang.
  90.         if(typeof document[config.WRITER_SIGNATURE] == "undefined")
  91.             throw new document.exception("Document provides no valid writer!");
  92.  
  93.         var writer = document[config.WRITER_SIGNATURE];
  94.         writer.apply(document, [data]); //apply the writer using correct context
  95.     };
  96.  
  97.     /**
  98.      * Produces a "Hello world" message-box
  99.      * Warning! Message may vary depending on initial configuration
  100.     */
  101.     var HelloWorldFactory = (function(){
  102.         return function(){
  103.             this.produceMessage = function(){
  104.                 this.validDocument = provideDocument();
  105.                 new documentPrinter(this.validDocument, function(){
  106.                     return "hello_world";
  107.                 });
  108.             };
  109.         };
  110.     }());
  111.  
  112.     context.onload = function(){ // f**k yeah! we are ready
  113.         try{
  114.         var newFactory = new HelloWorldFactory();
  115.         newFactory.produceMessage();
  116.         } catch(err){
  117.             console.log(err); // silently log the error
  118.         };
  119.     };
  120. }(window || {}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement