PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/coffee-script/browser.js

http://github.com/jashkenas/coffee-script
JavaScript | 132 lines | 120 code | 11 blank | 1 comment | 38 complexity | 61a8ea792bd1fe4e1709b073eb6bdfca MD5 | raw file
  1// Generated by CoffeeScript 1.10.0
  2(function() {
  3  var CoffeeScript, compile, runScripts,
  4    indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  5
  6  CoffeeScript = require('./coffee-script');
  7
  8  CoffeeScript.require = require;
  9
 10  compile = CoffeeScript.compile;
 11
 12  CoffeeScript["eval"] = function(code, options) {
 13    if (options == null) {
 14      options = {};
 15    }
 16    if (options.bare == null) {
 17      options.bare = true;
 18    }
 19    return eval(compile(code, options));
 20  };
 21
 22  CoffeeScript.run = function(code, options) {
 23    if (options == null) {
 24      options = {};
 25    }
 26    options.bare = true;
 27    options.shiftLine = true;
 28    return Function(compile(code, options))();
 29  };
 30
 31  if (typeof window === "undefined" || window === null) {
 32    return;
 33  }
 34
 35  if ((typeof btoa !== "undefined" && btoa !== null) && (typeof JSON !== "undefined" && JSON !== null)) {
 36    compile = function(code, options) {
 37      if (options == null) {
 38        options = {};
 39      }
 40      options.inlineMap = true;
 41      return CoffeeScript.compile(code, options);
 42    };
 43  }
 44
 45  CoffeeScript.load = function(url, callback, options, hold) {
 46    var xhr;
 47    if (options == null) {
 48      options = {};
 49    }
 50    if (hold == null) {
 51      hold = false;
 52    }
 53    options.sourceFiles = [url];
 54    xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new window.XMLHttpRequest();
 55    xhr.open('GET', url, true);
 56    if ('overrideMimeType' in xhr) {
 57      xhr.overrideMimeType('text/plain');
 58    }
 59    xhr.onreadystatechange = function() {
 60      var param, ref;
 61      if (xhr.readyState === 4) {
 62        if ((ref = xhr.status) === 0 || ref === 200) {
 63          param = [xhr.responseText, options];
 64          if (!hold) {
 65            CoffeeScript.run.apply(CoffeeScript, param);
 66          }
 67        } else {
 68          throw new Error("Could not load " + url);
 69        }
 70        if (callback) {
 71          return callback(param);
 72        }
 73      }
 74    };
 75    return xhr.send(null);
 76  };
 77
 78  runScripts = function() {
 79    var coffees, coffeetypes, execute, fn, i, index, j, len, s, script, scripts;
 80    scripts = window.document.getElementsByTagName('script');
 81    coffeetypes = ['text/coffeescript', 'text/literate-coffeescript'];
 82    coffees = (function() {
 83      var j, len, ref, results;
 84      results = [];
 85      for (j = 0, len = scripts.length; j < len; j++) {
 86        s = scripts[j];
 87        if (ref = s.type, indexOf.call(coffeetypes, ref) >= 0) {
 88          results.push(s);
 89        }
 90      }
 91      return results;
 92    })();
 93    index = 0;
 94    execute = function() {
 95      var param;
 96      param = coffees[index];
 97      if (param instanceof Array) {
 98        CoffeeScript.run.apply(CoffeeScript, param);
 99        index++;
100        return execute();
101      }
102    };
103    fn = function(script, i) {
104      var options, source;
105      options = {
106        literate: script.type === coffeetypes[1]
107      };
108      source = script.src || script.getAttribute('data-src');
109      if (source) {
110        return CoffeeScript.load(source, function(param) {
111          coffees[i] = param;
112          return execute();
113        }, options, true);
114      } else {
115        options.sourceFiles = ['embedded'];
116        return coffees[i] = [script.innerHTML, options];
117      }
118    };
119    for (i = j = 0, len = coffees.length; j < len; i = ++j) {
120      script = coffees[i];
121      fn(script, i);
122    }
123    return execute();
124  };
125
126  if (window.addEventListener) {
127    window.addEventListener('DOMContentLoaded', runScripts, false);
128  } else {
129    window.attachEvent('onload', runScripts);
130  }
131
132}).call(this);