Click here to Skip to main content
15,885,309 members
Articles / Web Development / HTML5

Anatomy of HTML5/JavaScript Single Page Application in Samples (basics, navigation, composition, communications with the server)

Rate me:
Please Sign up or sign in to vote.
4.99/5 (44 votes)
20 Dec 2012CPOL37 min read 195K   5.1K   143  
Describes Single Page Applications and a new BPF Framework that helps to develop SPAs.
"use strict";function SimpleEvent(){this.eventHandlers=[]}var isObjectArray=function(n){return Object.prototype.toString.call(n)==="[object Array]"?!0:!1},getCallableControl,bpf;Array.prototype.remove=function(n){var t=0;do this[t]===n?this.splice(t,1):t++;while(t<this.length)},Array.prototype.addArray=function(n){for(var t=0;t<n.length;t++)this.push(n[t])},Array.prototype.insert=function(n,t){this.splice(n,0,t)},Array.prototype.firstIndexOf=function(n){var t=0;do{if(this[t]===n)return t;t++}while(t<this.length)},Array.prototype.lastIndexOf=function(n){var t=this.length-1;do{if(this[t]===n)return t;t--}while(t>=0)},Array.prototype.clear=function(){this.length=0},Array.prototype.copy=function(n,t){var i,u,r;for(n||(n=0),t?(i=n+t,i>this.length&&(i=this.length)):i=this.length,u=[],r=n;r<i;r++)u.push(this[r]);return u},bpf=bpf||{},bpf.nav=bpf.nav||{},bpf.nav.CheckboxNavAdaptor=function(n,t){var i=this,r=n,o=t;i.onSelectionChanged=new SimpleEvent;var f=function(){return r[0]},u=function(n){return n===!0?"selected_"+t:"unselected_"+t},e=function(){return u(f().checked)};r.bind("change",function(){i.fireSelectedHashChanged()}),i.fireSelectedHashChanged=function(){i.onSelectionChanged.fire(i,i.getSelectedKey())},i.getSelectedKey=function(){return e()},i.select=function(n){var t;n===u(!0)?t=!0:n===u(!1)&&(t=!1),(t||t===!1)&&(f().checked=t,r.trigger("change"))},i.unselect=function(){}},bpf=bpf||{},bpf.cmpst=bpf.cmpst||{},bpf.cmpst.pluginCache={cache:{},getPlugin:function(n){return bpf.cmpst.pluginCache.cache[n]},addPlugin:function(n,t){bpf.cmpst.pluginCache.cache[n]=t},hasPlugin:function(n){var t=bpf.cmpst.pluginCache.cache[n];return t?!0:!1}},bpf.cmpst.rootPluginInfo={};var replaceChildren=function(n,t){return n.contents().remove(),n.append(t),n},getScriptTextFromHTML=function(n){var i=n.filter("script[data-type='script-interface']"),t;if(i&&i.length!==0)return(t=i[0],!t)?void 0:t.text||t.textContent||t.innerHTML},hasCodeBehindInASeparateFile=function(n){var t=n.filter("script[data-has-external-code-behind='true']");return t.length?!0:!1},downloadPluginAndAddToCache=function(n,t){$.ajax({url:n,async:!0,success:function(i){var r={},o;r.downloadedHtmlContainer=function(){var n,t;return{getDownloadedHtml:function(){return n},setDownloadedHtml:function(i){n=i,t=$(n)},getDownloadedHtmlDOM:function(){return t},setDownloadedHtmlDOM:function(i){t=i,n=t.outerHTML()}}}(),r.downloadedHtmlContainer.setDownloadedHtml(i);var u=r.downloadedHtmlContainer.getDownloadedHtmlDOM(),f=function(scriptText){r.codeBehind=eval(scriptText),bpf.cmpst.pluginCache.addPlugin(n,r),t.fire(r)},e=getScriptTextFromHTML(u),s=hasCodeBehindInASeparateFile(u);if(u=u.not("script"),r.downloadedHtmlContainer.setDownloadedHtmlDOM(u),e){f(e);return}if(s){o=n+".js",$.getScript(o,function(n){"use strict";f(n);return});return}f()}})},processAndInsertPluginFromCacheImpl=function(n,t,i,r,u,f,e){var s=bpf.cmpst.pluginCache.getPlugin(i),o=s.codeBehind,h,c,l;o&&o.preRender&&o.preRender.call(s.downloadedHtmlContainer,e);var a=s.downloadedHtmlContainer.getDownloadedHtml(),y=s.downloadedHtmlContainer.getDownloadedHtmlDOM(),v=a;n||(n=bpf.cmpst.rootPluginInfo),h=n.currentDOM,c=h?h.find(t):$(t),replaceChildren(c,v),o&&(n.pluginMap||(n.pluginMap={}),l={currentDOM:c,codeBehind:o,postRenderArguments:f},n.pluginMap[t]=l,o.postRender&&o.postRender.call(l,u)),r.fire(null,!0)};bpf.cmpst.getPlugin=function(n,t,i,r,u,f){var s=new SimpleEvent,e,o;if(r.addCallback(s,n),e=function(){processAndInsertPluginFromCacheImpl(t,i,n,s,r,u,f)},bpf.cmpst.pluginCache.hasPlugin()){e();return}o=new SimpleEvent,o.addSimpleEventHandler(function(){e()}),downloadPluginAndAddToCache(n,o)},getCallableControl=function(n){return{currentDOMPluginInfo:n,call:function(t){var i=Array.prototype.copy.call(arguments,1);n.codeBehind[t].apply(n,i)}}},bpf.control=function(n,t){t||(t=bpf.cmpst.rootPluginInfo);var i=t.pluginMap[n];return getCallableControl(i)},bpf=bpf||{},bpf.utils=bpf.utils||{},bpf.utils.EventBarrier=function(){var r=new SimpleEvent,u={},n=0,i=!0,t=0;this.getNumberEvents=function(){return t},this.addSimpleEventHandler=function(n){r.addSimpleEventHandler(n)},this.addCallback=function(f,e){var s=this,o;n++,o={id:n,event:f},e&&(o.name=e),u[n]=o,t++,f.addSimpleEventHandler(function(f){delete u[n],t--,i=i&&f,t===0&&r.fire(s,i)})},this.createChildEventBarrier=function(n){var t=new bpf.utils.EventBarrier,i=new SimpleEvent;return this.addCallback(i,n),t.addSimpleEventHandler(function(){i.fire()}),t}},bpf=bpf||{},bpf.utils=bpf.utils||{},bpf.utils.segmentSeparationCharacter=".",bpf.utils.stripFirstPound=function(n){return n?n.charAt(0)==="#"?n.slice(1):n:n},bpf.utils.stripTrailingDot=function(n){return n?n.charAt(n.length-1)==="."?n.slice(0,n.length-1):n:n},bpf.utils.fillBlanks=function(n){return n?n.replace(/\s+/g,"-"):n},bpf=bpf||{},bpf.utils=bpf.utils||{},bpf.utils.Iterator=function(n){var t=this,i=0;t.current=function(){return n[i]},t.isCurrentValid=function(){return n?isObjectArray(n)?i<n.length?!0:!1:!1:!1},t.moveToNext=function(){i++},t.getCurrentIdx=function(){return i}},bpf=bpf||{},bpf.nav=bpf.nav||{},bpf.nav.JQTabsNavAdaptor=function(n){var t=this,r=function(){return bpf.utils.stripFirstPound(n.find(".ui-tabs-active a").attr("href"))},i;t.onSelectionChanged=new SimpleEvent,n.bind("tabsselect",function(r,u){var f=n[0];r.target===f&&(t.selectedKey=bpf.utils.stripFirstPound(u.tab.hash),i())}),i=function(){t.onSelectionChanged.fire(t,t.selectedKey)},t.getSelectedKey=function(){if(t.selectedKey)return t.selectedKey;return r()},t.select=function(t){n.tabs("select",t)},t.unselect=function(){}},bpf.nav.getJQTabsNode=function(n){var t=new bpf.nav.JQTabsNavAdaptor(n);return new bpf.nav.Node(t)},bpf.nav.addJQTabsChild=function(n,t,i){var r=new bpf.nav.JQTabsNavAdaptor(i);return n.addAdaptedChild(t,r)},$.ui&&($.ui.setSpinner=function(n){var t=$(n).spinner({min:0,max:1e3});$(t).spinner().bind("spinstop",function(){$(this).spinner().trigger("change")})}),$.fn.outerHTMLForSimpleJQObj=function(){var n=$(this),t;return"outerHTML"in n[0]?n[0].outerHTML:(t=n.wrap("<div><\/div>").parent().html(),n.unwrap(),t)},$.fn.outerHTML=function(){for(var i=$(this),r="",t,n=0;n<i.length;n++)t=i[n],r+=t instanceof Text?t.data:$(t).outerHTMLForSimpleJQObj();return r},bpf=bpf||{},bpf.nav=bpf.nav||{},bpf.nav.KoObservableNavAdaptor=function(n,t,i){var r=this,u,f=t,o=i,e;r.onSelectionChanged=new SimpleEvent,n.subscribe(function(n){u!==n&&(u=n,e())}),e=function(){r.onSelectionChanged.fire(r,r.selectedKey)},r.getSelectedKey=function(){if(u)return o(u)},r.getChildObjectByKey=function(n){return f(n)},r.select=function(t){u=f(t),u?n(u):r.unselect()},r.unselect=function(){u=null,n("")}},bpf=bpf||{},bpf.nav=bpf.nav||{},bpf.nav.FactoryNode=function(n,t,i){var r=this,f=n,e=i,u;bpf.nav.Node.call(r,n,t),r.getOnSelectionChangedEvent().addSimpleEventHandler(function(){var n=r.getSelectedKey();n&&u(n)}),u=function(n){var u=r.getChildren(),t;if((!u||!u.containsKey(n))&&i){if(t=i(n,f),!t)return;r.addChildNode(n,t)}},r.select=function(n){r.getSelectedKey()!==n&&(r.data.select(n),u(n))}},bpf=bpf||{},bpf.nav=bpf.nav||{},bpf.nav.Node=function(n,t){var i=this,r;bpf.nav.NodeBase.call(i,t),r=function(n){i.data=n,n.onSelectionChanged.addSimpleEventHandler(function(){i.fireSelectionChanged()})},r(n),i.addChildren=function(n,t,r){var u=n();return _(u).each(function(n){var u=r(n),f=t(n);i.addAdaptedChild(u,f)}),_childNodes},i.getSelectedKey=function(){return i.data.getSelectedKey()},i.unselect=function(){i.data.unselect()},i.chainUnselect=function(){var n=i.getSelectedChild();n&&n.chainUnselect(),i.unselect()},i.select=function(n){i.getSelectedKey()!==n&&i.data.select(n)},i.getSelectedChild=function(){return i.getChild(i.getSelectedKey())},i.setSelectedKeySegmentsRecursive=function(n){var f=[".","(",")"],t,r,e=n.getStrUpTo(f,function(n,i){r=n,t=i}),u;return(i.select(e),u=i.getSelectedChild(),!u)?t:(r==="."||r==="("?t=u.setSelectedKeySegmentsRecursive(t):u.chainUnselect(),r===")"&&(t=r+t),t)},i.getUrlRecursive=function(){var n="",u=i.data.getSelectedKey(),t,r;if(u)n+=u;else return n;return t=i.getSelectedChild(),t&&(r=t.getUrlRecursive(),r&&(n+=bpf.utils.segmentSeparationCharacter+r)),n}},bpf.nav.createNode=function(n,t){var i=t(n);return new bpf.nav.Node(i)},bpf=bpf||{},bpf.nav=bpf.nav||{},bpf.nav.NodeBase=function(n){var t=this,u=new SimpleEvent,r=n,i;t.setParentNode=function(n){r=n},t.getOnSelectionChangedEvent=function(){return u},t.fireSelectionChanged=function(){r?r.fireSelectionChanged():u.fire()},t.addChildNode=function(n,r){i||(i=new bpf.utils.OrderedMap),r.setParentNode(t),i.add(n,r)},t.addAdaptedChild=function(n,i){var r=new bpf.nav.Node(i);return t.addChildNode(n,r),r},t.addChild=function(n,i,r){var u=r(i);return t.addAdaptedChild(n,u)},t.getChild=function(n){if(n)return i?i.objByKey(n):void 0},t.getChildren=function(){return i},t.getTotalHash=function(){return"#"+t.getUrlRecursive()+bpf.utils.segmentSeparationCharacter},t.setSelectedKeySegments=function(n){n=bpf.utils.stripFirstPound(n),n=bpf.utils.stripTrailingDot(n),t.setSelectedKeySegmentsRecursive(n)}},bpf.nav.setTotalHash=function(n){window.location.hash=n.getTotalHash()},bpf.nav.setKeySegmentToHash=function(n){var t=n.getTotalHash();return t===window.location.hash?!1:(n.setSelectedKeySegments(window.location.hash),bpf.nav.setTotalHash(n),!0)},bpf.nav.connectToUrlHash=function(n){$(window).bind("hashchange",function(){bpf.nav.setKeySegmentToHash(n)}),n.getOnSelectionChangedEvent().addSimpleEventHandler(function(){bpf.nav.setTotalHash(n)})},bpf=bpf||{},bpf.utils=bpf.utils||{},bpf.utils.extendObj=function(n){var i=function(n){var i={},t;for(t in n)i[t]=n[t];return i},r=function(n){var r={},t,i;for(t in n)(i=n[t],typeof i!="function")&&(r[t]=i);return r},t=i(n);return t.cloneObj=function(){return i(t)},t.unextendObj=function(){return r(t)},t.removeKey=function(n){delete t[n]},t.containsKey=function(n){return t.hasOwnProperty(n)},t.printKeys=function(){for(var n in t)console.log(n+" ")},t},bpf=bpf||{},bpf.utils=bpf.utils||{},bpf.utils.OrderedMap=function(){var n=this,t={},i=[];n.add=function(n,r){i.push(n),t[n]=r},n.objByKey=function(n){return t[n]},n.getIterator=function(){var t=new bpf.utils.Iterator(i);return t.currentKey=function(){return i[t.getCurrentIdx()]},t.current=function(){var i=t.currentKey();return n.objByKey(i)},t},n.getMapClone=function(){return bpf.utils.extendObj(t)},n.containsKey=function(n){return t.hasOwnProperty(n)},n.reset=n.clear=function(){i=[],t={}}},bpf=bpf||{},bpf.nav=bpf.nav||{},bpf.nav.ProductNode=function(n){var t=this;bpf.nav.NodeBase.call(t,n),t.chainUnselect=function(){for(var r=t.getChildren(),i,n=r.getIterator();n.isCurrentValid();n.moveToNext())i=n.current(),i.chainUnselect()},t.setSelectedKeySegmentsRecursive=function(n){for(var i=n,u=t.getChildren(),e=u.getMapClone(),f,o,s,r;i.length>0;){if(i.startsWith(")"))return i.removePrefix(")");if(!i.startsWith("("))return;i=i.removePrefix("("),f=i.getStrUpTo("/",function(n,t){i=t}),e.removeKey(f),r=u.objByKey(f),i=r.setSelectedKeySegmentsRecursive(i)}o=e.unextendObj();for(s in o)r=u.objByKey(s),r.chainUnselect();return i},t.getUrlRecursive=function(){for(var i="",u=t.getChildren(),n=u.getIterator();n.isCurrentValid();n.moveToNext()){var f=n.currentKey(),e=n.current(),r=e.getUrlRecursive();r&&(i+="("+f+"/"+r+")")}return i}},SimpleEvent.prototype={addSimpleEventHandler:function(n){this.eventHandlers.push(n)},removeSimpleEventHandler:function(n){this.eventhandlers.remove(n)},clearSimpleEventHandlers:function(){this.eventHandlers.clear()},setSimpleEventHandler:function(n){this.clearEventHandlers(),this.eventHandlers.addEventHandler(n)},fire:function(n){var i,n,r,t;for(arguments.length>0&&(n=arguments[0]),r=Array.prototype.copy.call(arguments,1),t=0;t<this.eventHandlers.length;t++)i=this.eventHandlers[t].apply(n,r);return i}},String.prototype.startsWith=function(n){if(n.length>this.length)return!1;var t=this.substr(0,n.length);return t===n?!0:!1},String.prototype.removePrefix=function(n){return this.startsWith(n)?this.substr(n.length,this.length-n.length):this},String.prototype.getStrUpTo=function(n,t){var i=this.length,r,o=[],u,e,f,s,h;for(isObjectArray(n)?o=n:o.push(n),u=0;u<n.length;u++)(e=n[u],f=this.indexOf(e),f<0)||f<=i&&(i=f,r=e);return s=this.substr(0,i),r&&(i+=r.length),t&&(h=this.substr(i,this.length-i),t(r,h)),s},String.prototype.getStrBetween=function(n,t,i,r){var u,f,e,o;if(i||(i=0),u=this.indexOf(n,i),u<0){r&&r(-1);return}return u+=n.length,f=this.indexOf(t,u+1),f<0&&(f=s.length),e=f+1,o=this.substr(u,f-u),r&&r(e),o}
//@ sourceMappingURL=bpf.min.js.map

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect AWebPros
United States United States
I am a software architect and a developer with great passion for new engineering solutions and finding and applying design patterns.

I am passionate about learning new ways of building software and sharing my knowledge with others.

I worked with many various languages including C#, Java and C++.

I fell in love with WPF (and later Silverlight) at first sight. After Microsoft killed Silverlight, I was distraught until I found Avalonia - a great multiplatform package for building UI on Windows, Linux, Mac as well as within browsers (using WASM) and for mobile platforms.

I have my Ph.D. from RPI.

here is my linkedin profile

Comments and Discussions