From a15440ff7162c8cc0731e260cf4cacab5b998c62 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Tue, 28 May 2013 13:07:05 +0200 Subject: [PATCH] Editor: Introducing requirejs --- entrypoint.js | 63 +++++++++++++++++++++++++ modules/data.js | 7 ++- modules/rng.js | 8 +++- modules/skelton.js | 8 +++- modules/sourceEditor.js | 8 +++- modules/tabsManager.js | 8 +++- modules/visualEditor.js | 9 ++-- modules/visualEditor.transformations.js | 15 ++---- require.js | 36 ++++++++++++++ rng.js | 4 +- runner.js | 16 +++++-- 11 files changed, 151 insertions(+), 31 deletions(-) create mode 100644 entrypoint.js create mode 100644 require.js diff --git a/entrypoint.js b/entrypoint.js new file mode 100644 index 0000000..30052c2 --- /dev/null +++ b/entrypoint.js @@ -0,0 +1,63 @@ +(function() { + + requirejs.config({ + baseUrl: '/static', + + shim: { + 'jquery-1.9.1.min': { + exports: '$', + }, + 'underscore-min': { + exports: '_' + }, + 'bootstrap/js/bootstrap.min': { + deps: ['jquery-1.9.1.min'] + } + }, + + paths: { + modules: '/static/editor/modules' + } + }); + + + var dependenciesList = [ + 'jquery-1.9.1.min', + 'editor/runner', + 'editor/rng', + + 'modules/data', + 'modules/rng', + 'modules/skelton', + 'modules/sourceEditor', + 'modules/tabsManager', + 'modules/visualEditor', + 'modules/sourceEditor', + + 'bootstrap/js/bootstrap.min' + ]; + + requirejs(dependenciesList, function ($, runner, rng) { + var args = arguments; + + var getModulesFromArguments = function() { + var toret = {}; + var isModule = function(idx) {return dependenciesList[idx].substr(0, 'modules/'.length) === 'modules/';}; + var moduleName = function(idx) {return dependenciesList[idx].split('/')[1]}; + + for(var i = 0; i < args.length; i++) { + if(isModule(i)) + toret[moduleName(i)] = args[i]; + } + return toret; + } + + $(function() { + var app = new runner.Runner(rng, getModulesFromArguments()); + app.setBootstrappedData('data', RNG_BOOTSTRAP_DATA); + app.start({rootSelector:'#editor_root'}); + }); + + }); + +})(); \ No newline at end of file diff --git a/modules/data.js b/modules/data.js index 36ba172..527b928 100644 --- a/modules/data.js +++ b/modules/data.js @@ -1,4 +1,6 @@ -rng.modules.data = function(sandbox) { +define(function() { + +return function(sandbox) { var doc = sandbox.getBootstrappedData().document; var document_id = sandbox.getBootstrappedData().document_id; @@ -61,5 +63,6 @@ rng.modules.data = function(sandbox) { } } +}; -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/modules/rng.js b/modules/rng.js index 36bd2f8..ffb228d 100644 --- a/modules/rng.js +++ b/modules/rng.js @@ -1,4 +1,6 @@ -rng.modules.rng = function(sandbox) { +define(function() { + +return function(sandbox) { function addTab(title, slug, view) { sandbox.getModule('tabsManager').addTab(title, slug, view); @@ -94,4 +96,6 @@ rng.modules.rng = function(sandbox) { } } } -}; \ No newline at end of file +}; + +}); \ No newline at end of file diff --git a/modules/skelton.js b/modules/skelton.js index 3141459..28780f2 100644 --- a/modules/skelton.js +++ b/modules/skelton.js @@ -1,4 +1,6 @@ -rng.modules.skelton = function(sandbox) { +define(function() { + +return function(sandbox) { var view = $(sandbox.getTemplate('main')()); @@ -28,4 +30,6 @@ rng.modules.skelton = function(sandbox) { view.find('[data-cmd='+cmd+']').removeClass('disabled'); } } -}; \ No newline at end of file +}; + +}); \ No newline at end of file diff --git a/modules/sourceEditor.js b/modules/sourceEditor.js index 5a62eaf..248c02d 100644 --- a/modules/sourceEditor.js +++ b/modules/sourceEditor.js @@ -1,4 +1,6 @@ -rng.modules.sourceEditor = function(sandbox) { +define(function() { + +return function(sandbox) { var view = $(sandbox.getTemplate('main')()); var isDirty = false; @@ -36,4 +38,6 @@ rng.modules.sourceEditor = function(sandbox) { } } -}; \ No newline at end of file +}; + +}); \ No newline at end of file diff --git a/modules/tabsManager.js b/modules/tabsManager.js index 4ede9a0..a204858 100644 --- a/modules/tabsManager.js +++ b/modules/tabsManager.js @@ -1,4 +1,6 @@ -rng.modules.tabsManager = function(sandbox) { +define(function() { + +return function(sandbox) { var $ = sandbox.$; @@ -54,4 +56,6 @@ rng.modules.tabsManager = function(sandbox) { } } -}; \ No newline at end of file +}; + +}); \ No newline at end of file diff --git a/modules/visualEditor.js b/modules/visualEditor.js index d5ff4b5..c30c282 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -1,5 +1,6 @@ -rng.modules.visualEditor = function(sandbox) { - var transformations = rng.modules.visualEditor.transformations; +define(['./visualEditor.transformations'], function(transformations) { + +return function(sandbox) { var view = { node: $(sandbox.getTemplate('main')()), @@ -493,4 +494,6 @@ rng.modules.visualEditor = function(sandbox) { } } -}; \ No newline at end of file +}; + +}); \ No newline at end of file diff --git a/modules/visualEditor.transformations.js b/modules/visualEditor.transformations.js index 8a32831..71b0ac5 100644 --- a/modules/visualEditor.transformations.js +++ b/modules/visualEditor.transformations.js @@ -1,8 +1,4 @@ -if(typeof module !== 'undefined' && module.exports) { - var $ = require('jquery'); -} - -(function($) { +define(['jquery-1.9.1.min'], function($) { var transformations = {}; @@ -107,11 +103,6 @@ if(typeof module !== 'undefined' && module.exports) { } } + return transformations; - if(typeof module !== 'undefined' && module.exports) { - module.exports = transformations; - } else { - rng.modules.visualEditor.transformations = transformations; - } - -})($); \ No newline at end of file +}); \ No newline at end of file diff --git a/require.js b/require.js new file mode 100644 index 0000000..b2dfc25 --- /dev/null +++ b/require.js @@ -0,0 +1,36 @@ +/* + RequireJS 2.1.6 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved. + Available via the MIT or new BSD license. + see: http://github.com/jrburke/requirejs for details +*/ +var requirejs,require,define; +(function(ba){function J(b){return"[object Function]"===N.call(b)}function K(b){return"[object Array]"===N.call(b)}function z(b,c){if(b){var d;for(d=0;dthis.depCount&&!this.defined){if(J(n)){if(this.events.error&&this.map.isDefine||h.onError!==ca)try{e=k.execCb(c,n,b,e)}catch(d){a=d}else e=k.execCb(c,n,b,e);this.map.isDefine&&((b=this.module)&&void 0!==b.exports&&b.exports!== +this.exports?e=b.exports:void 0===e&&this.usingExports&&(e=this.exports));if(a)return a.requireMap=this.map,a.requireModules=this.map.isDefine?[this.map.id]:null,a.requireType=this.map.isDefine?"define":"require",w(this.error=a)}else e=n;this.exports=e;if(this.map.isDefine&&!this.ignore&&(r[c]=e,h.onResourceLoad))h.onResourceLoad(k,this.map,this.depMaps);y(c);this.defined=!0}this.defining=!1;this.defined&&!this.defineEmitted&&(this.defineEmitted=!0,this.emit("defined",this.exports),this.defineEmitComplete= +!0)}}else this.fetch()}},callPlugin:function(){var a=this.map,b=a.id,d=l(a.prefix);this.depMaps.push(d);u(d,"defined",v(this,function(e){var n,d;d=this.map.name;var g=this.map.parentMap?this.map.parentMap.name:null,C=k.makeRequire(a.parentMap,{enableBuildCallback:!0});if(this.map.unnormalized){if(e.normalize&&(d=e.normalize(d,function(a){return c(a,g,!0)})||""),e=l(a.prefix+"!"+d,this.map.parentMap),u(e,"defined",v(this,function(a){this.init([],function(){return a},null,{enabled:!0,ignore:!0})})), +d=m(q,e.id)){this.depMaps.push(e);if(this.events.error)d.on("error",v(this,function(a){this.emit("error",a)}));d.enable()}}else n=v(this,function(a){this.init([],function(){return a},null,{enabled:!0})}),n.error=v(this,function(a){this.inited=!0;this.error=a;a.requireModules=[b];H(q,function(a){0===a.map.id.indexOf(b+"_unnormalized")&&y(a.map.id)});w(a)}),n.fromText=v(this,function(e,c){var d=a.name,g=l(d),i=Q;c&&(e=c);i&&(Q=!1);s(g);t(j.config,b)&&(j.config[d]=j.config[b]);try{h.exec(e)}catch(D){return w(B("fromtexteval", +"fromText eval for "+b+" failed: "+D,D,[b]))}i&&(Q=!0);this.depMaps.push(g);k.completeLoad(d);C([d],n)}),e.load(a.name,C,n,j)}));k.enable(d,this);this.pluginMaps[d.id]=d},enable:function(){W[this.map.id]=this;this.enabling=this.enabled=!0;z(this.depMaps,v(this,function(a,b){var c,e;if("string"===typeof a){a=l(a,this.map.isDefine?this.map:this.map.parentMap,!1,!this.skipMap);this.depMaps[b]=a;if(c=m(P,a.id)){this.depExports[b]=c(this);return}this.depCount+=1;u(a,"defined",v(this,function(a){this.defineDep(b, +a);this.check()}));this.errback&&u(a,"error",v(this,this.errback))}c=a.id;e=q[c];!t(P,c)&&(e&&!e.enabled)&&k.enable(a,this)}));H(this.pluginMaps,v(this,function(a){var b=m(q,a.id);b&&!b.enabled&&k.enable(a,this)}));this.enabling=!1;this.check()},on:function(a,b){var c=this.events[a];c||(c=this.events[a]=[]);c.push(b)},emit:function(a,b){z(this.events[a],function(a){a(b)});"error"===a&&delete this.events[a]}};k={config:j,contextName:b,registry:q,defined:r,urlFetched:V,defQueue:I,Module:$,makeModuleMap:l, +nextTick:h.nextTick,onError:w,configure:function(a){a.baseUrl&&"/"!==a.baseUrl.charAt(a.baseUrl.length-1)&&(a.baseUrl+="/");var b=j.pkgs,c=j.shim,e={paths:!0,config:!0,map:!0};H(a,function(a,b){e[b]?"map"===b?(j.map||(j.map={}),S(j[b],a,!0,!0)):S(j[b],a,!0):j[b]=a});a.shim&&(H(a.shim,function(a,b){K(a)&&(a={deps:a});if((a.exports||a.init)&&!a.exportsFn)a.exportsFn=k.makeShimExports(a);c[b]=a}),j.shim=c);a.packages&&(z(a.packages,function(a){a="string"===typeof a?{name:a}:a;b[a.name]={name:a.name, +location:a.location||a.name,main:(a.main||"main").replace(ka,"").replace(fa,"")}}),j.pkgs=b);H(q,function(a,b){!a.inited&&!a.map.unnormalized&&(a.map=l(b))});if(a.deps||a.callback)k.require(a.deps||[],a.callback)},makeShimExports:function(a){return function(){var b;a.init&&(b=a.init.apply(ba,arguments));return b||a.exports&&da(a.exports)}},makeRequire:function(a,f){function d(e,c,g){var i,j;f.enableBuildCallback&&(c&&J(c))&&(c.__requireJsBuild=!0);if("string"===typeof e){if(J(c))return w(B("requireargs", +"Invalid require call"),g);if(a&&t(P,e))return P[e](q[a.id]);if(h.get)return h.get(k,e,a,d);i=l(e,a,!1,!0);i=i.id;return!t(r,i)?w(B("notloaded",'Module name "'+i+'" has not been loaded yet for context: '+b+(a?"":". Use require([])"))):r[i]}M();k.nextTick(function(){M();j=s(l(null,a));j.skipMap=f.skipMap;j.init(e,c,g,{enabled:!0});E()});return d}f=f||{};S(d,{isBrowser:A,toUrl:function(b){var d,f=b.lastIndexOf("."),g=b.split("/")[0];if(-1!==f&&(!("."===g||".."===g)||1g.attachEvent.toString().indexOf("[native code"))&&!Z?(Q=!0,g.attachEvent("onreadystatechange",b.onScriptLoad)):(g.addEventListener("load",b.onScriptLoad,!1),g.addEventListener("error",b.onScriptError,!1)),g.src=d,M=g,E?y.insertBefore(g,E):y.appendChild(g), +M=null,g;if(ea)try{importScripts(d),b.completeLoad(c)}catch(l){b.onError(B("importscripts","importScripts failed for "+c+" at "+d,l,[c]))}};A&&O(document.getElementsByTagName("script"),function(b){y||(y=b.parentNode);if(L=b.getAttribute("data-main"))return s=L,u.baseUrl||(F=s.split("/"),s=F.pop(),ga=F.length?F.join("/")+"/":"./",u.baseUrl=ga),s=s.replace(fa,""),h.jsExtRegExp.test(s)&&(s=L),u.deps=u.deps?u.deps.concat(s):[s],!0});define=function(b,c,d){var h,g;"string"!==typeof b&&(d=c,c=b,b=null); +K(c)||(d=c,c=null);!c&&J(d)&&(c=[],d.length&&(d.toString().replace(ma,"").replace(na,function(b,d){c.push(d)}),c=(1===d.length?["require"]:["require","exports","module"]).concat(c)));if(Q){if(!(h=M))R&&"interactive"===R.readyState||O(document.getElementsByTagName("script"),function(b){if("interactive"===b.readyState)return R=b}),h=R;h&&(b||(b=h.getAttribute("data-requiremodule")),g=G[h.getAttribute("data-requirecontext")])}(g?g.defQueue:U).push([b,c,d])};define.amd={jQuery:!0};h.exec=function(b){return eval(b)}; +h(u)}})(this); diff --git a/rng.js b/rng.js index 09ca0e4..e30a633 100644 --- a/rng.js +++ b/rng.js @@ -1,8 +1,8 @@ -var rng = { +define({ modules: {}, initModules: ['rng'], permissions: { 'skelton': ['getDOM'], 'rng': ['getModule', 'handleEvents'] } -}; \ No newline at end of file +}); \ No newline at end of file diff --git a/runner.js b/runner.js index b7b0f65..d3b111b 100644 --- a/runner.js +++ b/runner.js @@ -1,7 +1,9 @@ -var Runner = function($, _, app) { +define(['jquery-1.9.1.min', 'underscore-min'], function($, _) { + +var Runner = function(app, modules) { function getModuleInstance(moduleName) { - var module = moduleInstances[moduleName] = (moduleInstances[moduleName] || app.modules[moduleName](new Sandbox(moduleName))); + var module = moduleInstances[moduleName] = (moduleInstances[moduleName] || modules[moduleName](new Sandbox(moduleName))); return module; } @@ -10,7 +12,7 @@ var Runner = function($, _, app) { moduleInstances = {}, eventListeners = []; - _.each(_.keys(app.modules || {}), function(moduleName) { + _.each(_.keys(modules || {}), function(moduleName) { if(_.contains(app.permissions[moduleName] || [], 'handleEvents')) { eventListeners.push(moduleName) } @@ -66,4 +68,10 @@ var Runner = function($, _, app) { getModuleInstance(moduleName).start(); }); } -}; \ No newline at end of file +} + +return { + Runner: Runner +} + +}); \ No newline at end of file -- 2.20.1