diff --git a/bower.json b/bower.json index dc7430ca..7e486068 100644 --- a/bower.json +++ b/bower.json @@ -27,6 +27,7 @@ "pako": "~0.2.5", "google-diff-match-patch": "~0.1.0", "daffy": "~1.0.1", - "jsSHA": "~1.5.0" + "jsSHA": "~1.5.0", + "load": "~1.1.2" } } diff --git a/lib/client/edward.js b/lib/client/edward.js index 7f8cf373..e958cbd2 100644 --- a/lib/client/edward.js +++ b/lib/client/edward.js @@ -1,6 +1,6 @@ -var join, ace, Util, DOM, CloudCmd; +var join, ace, load, Util, DOM, CloudCmd; -(function(global, join, DOM, exec, load, loadRemote, Files) { +(function(global, join, DOM, exec, loadRemote) { 'use strict'; if (typeof module !== 'undefined' && module.exports) @@ -16,6 +16,7 @@ var join, ace, Util, DOM, CloudCmd; Modelist, JSHintConfig, Emmet, + Options, edward = function(el, callback) { Element = el || document.body; @@ -32,12 +33,13 @@ var join, ace, Util, DOM, CloudCmd; ace.require('ace/ext/language_tools'); Modelist = ace.require('ace/ext/modelist'); - Files.get('edit', function(error, config) { - var options = config.options; - + load.json('/json/edit.json', function(error, config) { + var options = config.options; + + Options = options; edward.setOptions(options); + callback(); }); - callback(); }, ]); }; @@ -212,28 +214,26 @@ var join, ace, Util, DOM, CloudCmd; }; function setEmmet() { - Files.get('edit', function(error, config) { - var dir = '/modules/ace-builds/src-noconflict/', - extensions = config.extensions, - isEmmet = extensions.emmet; - - if (isEmmet) - exec.if(Emmet, function() { - edward.setOption('enableEmmet', true); - }, function(callback) { - var url; + var dir = '/modules/ace-builds/src-noconflict/', + extensions = Options.extensions, + isEmmet = extensions.emmet; + + if (isEmmet) + exec.if(Emmet, function() { + edward.setOption('enableEmmet', true); + }, function(callback) { + var url; + + url = join([ + dir + 'emmet.js', + dir + 'ext-emmet.js' + ]); + + load.js(url, function() { + Emmet = ace.require('ace/ext/emmet'); + Emmet.setCore(window.emmet); - url = join([ - dir + 'emmet.js', - dir + 'ext-emmet.js' - ]); - - load.js(url, function() { - Emmet = ace.require('ace/ext/emmet'); - Emmet.setCore(window.emmet); - - callback(); - }); + callback(); }); }); } @@ -251,15 +251,13 @@ var join, ace, Util, DOM, CloudCmd; }; exec.if(JSHintConfig, func, function() { - DOM.load.ajax({ - url : JSHINT_PATH, - success : function(data) { - exec.try(function() { - JSHintConfig = JSON.parse(data); - }); + load.json(JSHINT_PATH, function(error, json) { + if (error) + alert(error); + else + JSHintConfig = json; func(); - } }); }); } @@ -284,24 +282,48 @@ var join, ace, Util, DOM, CloudCmd; } function loadFiles(callback) { - var css = '/css/edward.css', - js = '/restafary.js', - dir = '/modules/ace-builds/src-noconflict/', - url = join([ - 'theme-tomorrow_night_blue', - 'ext-language_tools', - 'ext-searchbox', - 'ext-modelist' + var css = '/css/edward.css', + js = '/restafary.js', + + modules = '/modules/', + ace = modules + 'ace-builds/src-noconflict/', + + url = join([ + ace + 'theme-tomorrow_night_blue', + ace + 'ext-language_tools', + ace + 'ext-searchbox', + ace + 'ext-modelist' ].map(function(name) { - return dir + name + '.js'; + return name + '.js'; })); - loadRemote('ace', function() { - load.parallel([url, js, css], callback); - }); + exec.series([ + function(callback) { + loadScript(modules + 'load/load.js', callback); + }, + + function(callback) { + loadRemote('ace', callback); + }, + + function(callback) { + load.parallel([url, js, css], callback); + }, + + callback + ]); + } + + function loadScript(src, callback) { + var element = document.createElement('script'); + + element.src = src; + element.addEventListener('load', callback); + + document.body.appendChild(element); } return edward; } -})(this, join, DOM, Util.exec, DOM.load, DOM.loadRemote, DOM.Files); +})(this, join, DOM, Util.exec, DOM.loadRemote); diff --git a/modules/load/.bower.json b/modules/load/.bower.json new file mode 100644 index 00000000..71264d19 --- /dev/null +++ b/modules/load/.bower.json @@ -0,0 +1,27 @@ +{ + "name": "load", + "main": "load.js", + "version": "1.1.2", + "description": "Dynamically loading external JavaScript and CSS files", + "homepage": "https://github.com/coderaiser/load.js", + "authors": [ + "coderaiser " + ], + "moduleType": [ + "globals" + ], + "keywords": [ + "load" + ], + "license": "MIT", + "_release": "1.1.2", + "_resolution": { + "type": "version", + "tag": "v1.1.2", + "commit": "967bd65c6b5af21da3d85ea4a2144014a28eee39" + }, + "_source": "git://github.com/coderaiser/load.js.git", + "_target": "~1.1.2", + "_originalSource": "load", + "_direct": true +} \ No newline at end of file diff --git a/modules/load/ChangeLog b/modules/load/ChangeLog new file mode 100644 index 00000000..dcff9ff3 --- /dev/null +++ b/modules/load/ChangeLog @@ -0,0 +1,5 @@ +2015.01.13, v1.1.2 + +feature: +- (load) json: rm data + diff --git a/modules/load/LICENSE b/modules/load/LICENSE new file mode 100644 index 00000000..95387f3b --- /dev/null +++ b/modules/load/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2014-2015 coderaiser + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/modules/load/README.md b/modules/load/README.md new file mode 100644 index 00000000..bd3feda8 --- /dev/null +++ b/modules/load/README.md @@ -0,0 +1,63 @@ +# Load.js + +Dynamically loading external JavaScript and CSS files + +## Install + +``` +bower install load +``` + +## How use? +Create `html` page with `js` connected. + +```html + +``` + +Than try one of this: + +```js +load.js('jquery.js', function(error) { + /* you could use jquery functions here */ +}); + +load.css('menu.css', function(error) { + /* load menu css and then do some magic */ +}); + +/* recognition file type by extension */ +load('css-or-.js', function(error, event) { + console.log(error || event); +}); + +load('template/footer.html', function(error, footer) { + console.log(error || footer); +}); + +load.json('config.json', function(error, config) { + console.log(error || config); +}); + +load.ajax('template/header.html', function(error, header) { + console.log(error || header); +}); + +/* load one-by-one */ +load.series([ + 'jquery.js', + 'jq-console.js', +], function() { +}); + +/* load all together and call callback */ +load.parallel([ + 'menu.css', + 'menu.js', +], function() { +}); +``` + +## License + +MIT diff --git a/modules/load/bower.json b/modules/load/bower.json new file mode 100644 index 00000000..23939f6a --- /dev/null +++ b/modules/load/bower.json @@ -0,0 +1,17 @@ +{ + "name": "load", + "main": "load.js", + "version": "1.1.2", + "description": "Dynamically loading external JavaScript and CSS files", + "homepage": "https://github.com/coderaiser/load.js", + "authors": [ + "coderaiser " + ], + "moduleType": [ + "globals" + ], + "keywords": [ + "load" + ], + "license": "MIT" +} diff --git a/modules/load/load.js b/modules/load/load.js new file mode 100644 index 00000000..54fe5017 --- /dev/null +++ b/modules/load/load.js @@ -0,0 +1,170 @@ +(function(window) { + 'use strict'; + + window.load = new LoadProto(); + + function LoadProto() { + function load(src, callback) { + var ext = getExt(src); + + switch (ext) { + case '.js': + load.js(src, callback); + break; + + case '.css': + load.css(src, callback); + break; + + case '.json': + load.json(src, callback); + break; + + default: + load.ajax(src, callback); + } + } + + load.js = function(src, callback) { + var id = getIdBySrc(src), + el = document.getElementById(id); + + if (el) { + callback(); + } else { + el = document.createElement('script'); + el.id = id; + el.src = src; + + el.addEventListener('load', function(event) { + callback(null, event); + }); + + el.addEventListener('error', callback); + + document.body.appendChild(el); + } + + return el; + }; + + load.css = function(src, callback) { + var id = getIdBySrc(src), + el = document.getElementById(id); + + if (el) { + callback(); + } else { + el = document.createElement('link'); + el.id = getIdBySrc(src); + el.rel = 'stylesheet'; + el.href = src; + + el.addEventListener('load', function(event) { + callback(null, event); + }); + + el.addEventListener('error', callback); + + document.head.appendChild(el); + } + + return el; + }; + + load.ajax = function(url, callback) { + var request = new XMLHttpRequest(); + + request.open('GET', url, true); + + request.addEventListener('load', function() { + var data; + + if (request.status >= 200 && request.status < 400){ + data = request.responseText; + callback(null, data); + } + }); + + request.addEventListener('error', callback); + + request.send(); + }; + + load.json = function(url ,callback) { + load.ajax(url, function(error, data) { + var json; + + if (!error) + json = JSON.parse(data); + + callback(error, json); + }); + }; + + load.series = function(urls, callback) { + var url = urls.shift(); + + if (!url) + callback(); + else + load(url, function(error) { + if (error) + callback(error); + else + load.series(urls, callback); + }); + }; + + load.parallel = function(urls, callback) { + var i = urls.length, + func = function(error) { + --i; + + if (error) + i = 0; + + if (!i) + callback(error); + }; + + urls.forEach(function(url) { + load(url, func); + }); + }; + + function getExt(name) { + var ret = '', + dot, + isStr = typeof name === 'string'; + + if (isStr) { + dot = name.lastIndexOf('.'); + + if (~dot) + ret = name.substr(dot); + } + + return ret; + } + + function getIdBySrc(src) { + var num, sub, id, + isStr = typeof src === 'string'; + + if (isStr) { + if (~src.indexOf(':')) + src += '-join'; + + num = src.lastIndexOf('/') + 1; + sub = src.substr(src, num); + id = src.replace(sub, '') + .replace(/\./g, '-'); + } + + return id; + } + + return load; + } +})(window); diff --git a/modules/load/load.min.js b/modules/load/load.min.js new file mode 100644 index 00000000..19c946d8 --- /dev/null +++ b/modules/load/load.min.js @@ -0,0 +1 @@ +!function(e){"use strict";function n(){function e(t,r){var s=n(t);switch(s){case".js":e.js(t,r);break;case".css":e.css(t,r);break;case".json":e.json(t,r);break;default:e.ajax(t,r)}}function n(e){var n,t="",r="string"==typeof e;return r&&(n=e.lastIndexOf("."),~n&&(t=e.substr(n))),t}function t(e){var n,t,r,s="string"==typeof e;if(s)for(~e.indexOf(":")&&(e+="-join"),n=e.lastIndexOf("/")+1,t=e.substr(e,n),r=e.replace(t,"");r.indexOf(".")>0;)r=r.replace(".","-");return r}return e.js=function(e,n){var r=t(e),s=document.getElementById(r);return s?n():(s=document.createElement("script"),s.id=r,s.src=e,s.addEventListener("load",function(e){n(null,e)}),s.addEventListener("error",n),document.body.appendChild(s)),s},e.css=function(e,n){var r=t(e),s=document.getElementById(r);return s?n():(s=document.createElement("link"),s.id=t(e),s.rel="stylesheet",s.href=e,s.addEventListener("load",function(e){n(null,e)}),s.addEventListener("error",n),document.head.appendChild(s)),s},e.ajax=function(e,n){var t=new XMLHttpRequest;t.open("GET",e,!0),t.addEventListener("load",function(){var e;t.status>=200&&t.status<400&&(e=t.responseText,n(null,e))}),t.addEventListener("error",n),t.send()},e.json=function(n,t){e.ajax(n,function(e,n){var r;e||(r=JSON.parse(n)),t(e,r||n)})},e.series=function(n,t){var r=n.shift();r?e(r,function(){e.series(n,t)}):t()},e.parallel=function(n,t){var r=n.length,s=function(){--r,r||t()};n.forEach(function(n){e(n,s)})},e}e.load=new n}(window);