pyc-website
main website for pyc inc.
git clone https://9o.is/git/pyc-website.git
commit 80556d769e44c259e80083a7405fdb0a2644535c parent 97a4e067d860392af5e5659e6c3f7faff5316638 Author: Jul <jul@9o.is> Date: Sun, 4 May 2014 06:34:21 -0400 ui-router now in charge of loading cometajax and max concurrent connections set to 10 Diffstat:
| M | src/main/scala/bootstrap/liftweb/Boot.scala | | | 5 | +++++ |
| M | src/main/scala/inc/pyc/lib/NgUIRouter.scala | | | 91 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- |
2 files changed, 86 insertions(+), 10 deletions(-)
diff --git a/src/main/scala/bootstrap/liftweb/Boot.scala b/src/main/scala/bootstrap/liftweb/Boot.scala @@ -69,6 +69,11 @@ class Boot extends Loggable { // don't include the liftAjax.js code. It's served statically. LiftRules.autoIncludeAjaxCalc.default.set(() => (session: LiftSession) => false) + + // don't include cometajax.js. served by ui-router module + LiftRules.autoIncludeComet = ((session: LiftSession) => false) + + LiftRules.maxConcurrentRequests.default.set((request: Req) =>10) // Mailer Mailer.devModeSend.default.set((m: MimeMessage) => logger.info("Dev mode message:\n" + prettyPrintMime(m))) diff --git a/src/main/scala/inc/pyc/lib/NgUIRouter.scala b/src/main/scala/inc/pyc/lib/NgUIRouter.scala @@ -6,6 +6,7 @@ import net.liftweb._ import sitemap._ import common._ import http._ +import js._ import sitemap.Loc.{LocGroup, AnyLocParam} import util._ import Helpers._ @@ -65,7 +66,6 @@ trait NgUIRouterSnip extends SnippetHelper { app <- S.attr("ngApp") ?~ "ngApp name is missing" } yield "* *" #> { import NgUIRouterFactory._ - type Cmd = String // the default route val otherwise = @@ -95,27 +95,98 @@ trait NgUIRouterSnip extends SnippetHelper { ;}) """ + def init = "var statesVisited = []; var lift_toWatch = {};" + // updates lift_toWatch js variable - val updateLiftWatch: Cmd = + val updateLiftWatch = """var el = angular.element(document.querySelectorAll(".lift-roundtrip"));"""+ """for (var i=0;i<el.length;++i) { window.lift_toWatch[el[i].getAttribute("id")] = el[i].getAttribute("when") };""" - // updates all client side js promises - val updatePromises: Cmd = + // evaluates the javascript that was placed in the ajax-uploaded html page + val evalRenderedJS = """var promises = angular.element(document.querySelectorAll(".lift-promise"));"""+ """for (var i=0;i<promises.length;++i) { eval(promises[i].innerHTML) };""" - val log: Cmd = "console.log('$viewContentLoaded is running now ... ');" + val liftCometEntry = """window.liftComet.lift_handlerSuccessFunc();""" + + val log = if(Props.devMode) "console.log('$viewContentLoaded is running now ... ');" else "" - // ng ui-router run when $viewContentLoaded - def runFunc(cmds: Cmd*): Cmd = - """.run(['$rootScope', function($rootScope) {$rootScope.$on('$viewContentLoaded', function() {"""+ + // ng ui-router run when $viewContentLoaded (on regular and ajax page load) + def onViewContentLoaded(cmds: String*): String = + """.run(['$rootScope', '$state', function($rootScope, $state) {$rootScope.$on('$viewContentLoaded', function() {"""+ cmds.mkString+"""}); }])""" + + def ifStateNotVisited(cmds: String*): String = """ + var visited = false; + angular.forEach(statesVisited, function(state){ + if(state === $state.current.name) {visited = true;} + }); + if(!visited){ + statesVisited.push($state.current.name); + """+cmds.mkString+""" + } + """ + + def func(body: String): String = "(function() {"+body+"});" - app + config + runFunc(updateLiftWatch, updatePromises) + ";" - // TODO if the first normal page load does not have any roundtrips, cometajax.js is ignored. + s"""// <![CDATA[ + ${init + app + config + onViewContentLoaded(log, ifStateNotVisited(updateLiftWatch, evalRenderedJS, liftCometEntry))}; + $cometScript + //]]>""" } + /** + * Renders the default JS comet script + */ + private def cometScript: String = """ + (function() { + window.liftComet = { + lift_handlerSuccessFunc: function() { + setTimeout("liftComet.lift_cometEntry();",100); + }, + + lift_unlistWatch : function(watchId) { + var ret = []; + for (item in lift_toWatch) { + if (item !== watchId) { + ret.push(item); + } + } + lift_toWatch = ret; + }, + + lift_handlerFailureFunc: function() { + setTimeout("liftComet.lift_cometEntry();",""" + LiftRules.cometFailureRetryTimeout + """); + }, + + + lift_cometError: function(e) { + if (console && typeof console.error == 'function') + console.error(e.stack || e); + throw e; + }, + + lift_sessionLost: function() { window.location = '/' }, + + lift_cometEntry: function() { + var isEmpty = function(){for (var i in lift_toWatch) {return false} return true}(); + if (!isEmpty) { + liftAjax.lift_uriSuffix = undefined; + """ + + LiftRules.jsArtifacts.comet(AjaxInfo(JE.JsRaw("lift_toWatch"), + "GET", + LiftRules.cometGetTimeout, + false, + "script", + Full("liftComet.lift_handlerSuccessFunc"), + Full("liftComet.lift_handlerFailureFunc"))) + + """ + } + } + + }})(); + """ + /** * Conditional Template Surround Snippet. Allows pages to be surrounded by a