Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

change to use director instead of jquery.dispatch.js and improve live…

… search handling
  • Loading branch information...
commit bc9036a360fc815b686a52c741014a9dfb79eec6 1 parent 674578c
@caolan authored
View
5 js/director.min.js
@@ -0,0 +1,5 @@
+//
+// Generated on Tue Dec 06 2011 04:47:21 GMT-0500 (EST) by Nodejitsu, Inc (Using Codesurgeon).
+// Version 1.0.7
+//
+(function(a){function e(a,b){for(var c=0;c<a.length;c+=1)if(b(a[c],c,a)===!1)return}function f(a){var b=[];for(var c=0,d=a.length;c<d;c++)b=b.concat(a[c]);return b}function g(a,b,c){if(!a.length)return c();var d=0;(function e(){b(a[d],function(b){b||b===!1?(c(b),c=function(){}):(d+=1,d===a.length?c():e())})})()}function h(a,b,c){c=a;for(var d in b)if(b.hasOwnProperty(d)){c=b[d](a);if(c!==a)break}return c===a?"([a-zA-Z0-9-]+)":c}function i(a,b){~a.indexOf("*")&&(a=a.replace(/\*/g,"([_.()!\\ %@&a-zA-Z0-9-]+)"));var c=a.match(/:([^\/]+)/ig),d;if(c){d=c.length;for(var e=0;e<d;e++)a=a.replace(c[e],h(c[e],b))}return a}Array.prototype.filter||(Array.prototype.filter=function(a,b){var c=[],d;for(var e=0,f=this.length;e<f;e++)e in this&&a.call(b,d=this[e],e,this)&&c.push(d);return c}),Array.isArray||(Array.isArray=function(a){return Object.prototype.toString.call(a)==="[object Array]"});var b=document.location,c={mode:"modern",hash:b.hash,check:function(){var a=b.hash;a!=this.hash&&(this.hash=a,this.onHashChanged())},fire:function(){this.mode==="modern"?window.onhashchange():this.onHashChanged()},init:function(a){function c(){for(var a=0,b=window.Router.listeners.length;a<b;a++)window.Router.listeners[a]()}var b=this;window.Router.listeners||(window.Router.listeners=[]);if("onhashchange"in window&&(document.documentMode===undefined||document.documentMode>7))window.onhashchange=c,this.mode="modern";else{var d=document.createElement("iframe");d.id="state-frame",d.style.display="none",document.body.appendChild(d),this.writeFrame(""),"onpropertychange"in document&&"attachEvent"in document&&document.attachEvent("onpropertychange",function(){event.propertyName==="location"&&b.check()}),window.setInterval(function(){b.check()},50),this.onHashChanged=c,this.mode="legacy"}return window.Router.listeners.push(a),this.mode},destroy:function(a){if(!window.Router||!window.Router.listeners)return;var b=window.Router.listeners;for(var c=b.length-1;c>=0;c--)b[c]===a&&b.splice(c,1)},setHash:function(a){return this.mode==="legacy"&&this.writeFrame(a),b.hash=a[0]==="/"?a:"/"+a,this},writeFrame:function(a){var b=document.getElementById("state-frame"),c=b.contentDocument||b.contentWindow.document;c.open(),c.write("<script>_hash = '"+a+"'; onload = parent.listener.syncHash;<script>"),c.close()},syncHash:function(){var a=this._hash;return a!=b.hash&&(b.hash=a),this},onHashChanged:function(){}},d=a.Router=function(a){if(!(this instanceof d))return new d(a);this.params={},this.routes={},this.methods=["on","once","after","before"],this._methods={},this._insert=this.insert,this.insert=this.insertEx,this.configure(),this.mount(a||{})};d.prototype.init=function(a){var d=this;return this.handler=function(){var a=b.hash.replace(/^#/,"");d.dispatch("on",a)},b.hash===""&&a&&(b.hash=a),b.hash.length>0&&this.handler(),c.init(this.handler),this},d.prototype.explode=function(){var a=b.hash;return a[1]==="/"&&(a=a.slice(1)),a.slice(1,a.length).split("/")},d.prototype.setRoute=function(a,b,d){var e=this.explode();return typeof a=="number"&&typeof b=="string"?e[a]=b:typeof d=="string"?e.splice(a,b,s):e=[a],c.setHash(e.join("/")),e},d.prototype.insertEx=function(a,b,c,d){return a==="once"&&(a="on",c=function(a){var b=!1;return function(){if(b)return;return b=!0,a.apply(this,arguments)}}(c)),this._insert(a,b,c,d)},d.prototype.getState=function(){return this.state},d.prototype.getRoute=function(a){var b=a;if(typeof a=="number")b=this.explode()[a];else if(typeof a=="string"){var c=this.explode();b=c.indexOf(a)}else b=this.explode();return b},d.prototype.destroy=function(){return c.destroy(this.handler),this},d.prototype.configure=function(a){a=a||{};for(var b=0;b<this.methods.length;b++)this._methods[this.methods[b]]=!0;return this.recurse=a.recurse||this.recurse||!1,this.async=a.async||!1,this.delimiter=a.delimiter||"/",this.strict=typeof a.strict=="undefined"?!0:a.strict,this.notfound=a.notfound,this.resource=a.resource,this.every={after:a.after||null,before:a.before||null,on:a.on||null},this},d.prototype.param=function(a,b){a[0]!==":"&&(a=":"+a);var c=new RegExp(a,"g");this.params[a]=function(a){return a.replace(c,b.source||b)}},d.prototype.on=d.prototype.route=function(a,b,c){var d=this;!c&&typeof b=="function"&&(c=b,b=a,a="on"),b.source&&(b=b.source.replace(/\\\//ig,"/"));if(Array.isArray(a))return a.forEach(function(a){d.on(a.toLowerCase(),b,c)});this.insert(a,this.scope.concat(b.split(new RegExp(this.delimiter))),c)},d.prototype.dispatch=function(a,b,c){function h(){d.last=e.after,d.invoke(d.runlist(e),d,c)}var d=this,e=this.traverse(a,b,this.routes,""),f=this._invoked,g;return this._invoked=!0,!e||e.length===0?(this.last=[],typeof this.notfound=="function"&&this.invoke([this.notfound],{method:a,path:b},c),!1):(this.recurse==="forward"&&(e=e.reverse()),g=this.every&&this.every.after?[this.every.after].concat(this.last):[this.last],g&&g.length>0&&f?(this.async?this.invoke(g,this,h):(this.invoke(g,this),h()),!0):(h(),!0))},d.prototype.invoke=function(a,b,c){var d=this;this.async?g(a,function(c,d){typeof c=="function"&&c.apply(b,a.captures.concat(d))},function(){c&&c.apply(b,arguments)}):e(a,function f(c){if(Array.isArray(c))return e(c,f);if(typeof c=="function")return c.apply(b,a.captures||null);typeof c=="string"&&d.resource&&d.resource[c].apply(b,a.captures||null)})},d.prototype.traverse=function(a,b,c,d){var e=[],f,g,h,i,j;if(b===this.delimiter&&c[a])return i=[[c.before,c[a]].filter(Boolean)],i.after=[c.after].filter(Boolean),i.matched=!0,i.captures=[],i;for(var k in c)if(c.hasOwnProperty(k)&&(!this._methods[k]||this._methods[k]&&typeof c[k]=="object"&&!Array.isArray(c[k]))){f=g=d+this.delimiter+k,this.strict||(g+="["+this.delimiter+"]?"),h=b.match(new RegExp("^"+g));if(!h)continue;if(h[0]&&h[0]==b&&c[k][a])return i=[[c[k].before,c[k][a]].filter(Boolean)],i.after=[c[k].after].filter(Boolean),i.matched=!0,i.captures=h.slice(1),this.recurse&&c===this.routes&&(i.push([c.before,c.on].filter(Boolean)),i.after=i.after.concat([c.after].filter(Boolean))),i;i=this.traverse(a,b,c[k],f);if(i.matched)return i.length>0&&(e=e.concat(i)),this.recurse&&(e.push([c[k].before,c[k].on].filter(Boolean)),i.after=i.after.concat([c[k].after].filter(Boolean)),c===this.routes&&(e.push([c.before,c.on].filter(Boolean)),i.after=i.after.concat([c.after].filter(Boolean)))),e.matched=!0,e.captures=i.captures,e.after=i.after,e}return!1},d.prototype.insert=function(a,b,c,d){var e,f,g,h,j;b=b.filter(function(a){return a&&a.length>0}),d=d||this.routes,j=b.shift(),/\:|\*/.test(j)&&!/\\d|\\w/.test(j)&&(j=i(j,this.params));if(b.length>0)return d[j]=d[j]||{},this.insert(a,b,c,d[j]);if(!j&&!b.length&&d===this.routes){e=typeof d[a];switch(e){case"function":d[a]=[d[a],c];return;case"object":d[a].push(c);return;case"undefined":d[a]=c;return}return}f=typeof d[j],g=Array.isArray(d[j]);if(d[j]&&!g&&f=="object"){e=typeof d[j][a];switch(e){case"function":d[j][a]=[d[j][a],c];return;case"object":d[j][a].push(c);return;case"undefined":d[j][a]=c;return}}else if(f=="undefined"){h={},h[a]=c,d[j]=h;return}throw new Error("Invalid route context: "+f)},d.prototype.extend=function(a){var b=this,c=a.length,d;for(d=0;d<c;d++)(function(a){b._methods[a]=!0,b[a]=function(){var c=arguments.length===1?[a,""]:[a];b.on.apply(b,c.concat(Array.prototype.slice.call(arguments)))}})(a[d])},d.prototype.runlist=function(a){var b=this.every&&this.every.before?[this.every.before].concat(f(a)):f(a);return this.every&&this.every.on&&b.push(this.every.on),b.captures=a.captures,b.source=a.source,b},d.prototype.mount=function(a,b){function d(b,d){var e=b,f=b.split(c.delimiter),g=typeof a[b],h=f[0]===""||!c._methods[f[0]],i=h?"on":e;h&&(e=e.slice(c.delimiter.length),f.shift());if(h&&g==="object"&&!Array.isArray(a[b])){d=d.concat(f),c.mount(a[b],d);return}h&&(d=d.concat(e.split(c.delimiter))),c.insert(i,d,a[b])}if(!a||typeof a!="object"||Array.isArray(a))return;var c=this;b=b||[];for(var e in a)a.hasOwnProperty(e)&&d(e,b.slice(0))}})(window);
View
12 js/jquery.dispatch.min.js
@@ -1,12 +0,0 @@
-/**
- * jquery.dispatch.js
- * ==================
- *
- * A lightweight jQuery plugin for client-side URL handling. Dispatches
- * hash-based URLs to view functions, supports nested pattern objects,
- * regular expressions and :named parameters.
- *
- * Caolan McMahon
- * http://caolanmcmahon.com
- *
- */(function(a){function b(a,c,d,e){return c=c||[],d=d||"",Object.keys(a).forEach(function(e){typeof a[e]=="function"?c.push([d+e,a[e]]):b(a[e],c,d+e)}),c}function c(a){return a.map(function(a){var b=a[0].replace(/\/:\w+/g,"(?:/([^/]+))");return a[0]=new RegExp("^"+b+"$"),a})}a.dispatch=function(d){var e=c(b(d)),f=function(){for(var a=0,b=e.length;a<b;a++){var c=e[a],d=c[0].exec(window.location.hash.substr(1));if(d){c[1].apply(null,d.slice(1));return}}};a(window).bind("hashchange",f)}})(window.jQuery);
View
20 ui/app.js
@@ -1,11 +1,15 @@
exports.init = function (url) {
- $.dispatch({
- '': require('./views/home'),
- '/': require('./views/home'),
- '/signup': require('./views/signup'),
- '/login': require('./views/login'),
- '/login/:next': require('./views/login'),
- '/profile/:id': require('./views/profile')
+ var router = Router({
+ '/': require('./views/home'),
+ '/search/*': require('./views/home'),
+ '/signup': require('./views/signup'),
+ '/login': require('./views/login'),
+ '/login/*': require('./views/login'),
+ '/profile/*': require('./views/profile')
});
- $(window).trigger('hashchange');
+ router.init();
+ if (!window.location.hash || window.location.hash === '#') {
+ window.location = '#/';
+ $(window).trigger('hashchange');
+ }
};
View
69 ui/views/home.js
@@ -4,32 +4,58 @@ var templates = require('handlebars').templates,
cfg = require('settings/root');
-// max number of profiles to list on page
-var PAGE_LENGTH = 5;
+var PAGE_LENGTH = 5, // max number of profiles to list on page
+ prev_search = null; // to stop queries for the same results
-module.exports = function () {
- $('#content').html(
- templates['home.html']({})
- );
- if (cfg.profiles && cfg.profiles.lucene) {
- $('#user-search-form').show();
- $('#user-search-form').submit(function (ev) {
- ev.preventDefault();
- var q = $('#user-search-q', this).val();
- searchProfiles(q);
- return false;
- });$
- $('#user-search-q').keyup($.debounce(500, function () {
- var q = $(this).val();
+// set to true when updating the page inline but changing the url
+// for back button / bookmarking support
+var ignore_next_hashchange = false;
+
+
+module.exports = function (q) {
+ if (ignore_next_hashchange) {
+ // already showing home page, it will have updated inline
+ // without redrawing. do nothing on hash change.
+ ignore_next_hashchange = false;
+ }
+ else {
+ $('#content').html(
+ templates['home.html']({})
+ );
+ if (cfg.profiles && cfg.profiles.lucene) {
+ $('#user-search-form').show();
+ $('#user-search-form').submit(function (ev) {
+ ev.preventDefault();
+ var q = $('#user-search-q', this).val();
+ searchProfiles(q);
+ return false;
+ });$
+ $('#user-search-q').keyup($.debounce(500, function () {
+ var q = $(this).val();
+ if (q !== prev_search) {
+ searchProfiles(q);
+ }
+ prev_search = q;
+ }));
+ $('#user-search-q').focus();
+ }
+ if (q) {
+ var q = decodeURIComponent(q);
+ $('#user-search-q').val(q);
searchProfiles(q);
- }));
- $('#user-search-q').focus();
+ }
+ else {
+ getProfiles();
+ }
}
- getProfiles();
};
function getProfiles(start_key, descending) {
+ if (window.location.hash !== '#/') {
+ ignore_next_hashchange = true;
+ window.location = '#/';
+ }
$('#user-search-form .control-group').removeClass('error');
$('#user-search-form .help-inline').text('');
$('#user-search-submit').button('loading');
@@ -60,6 +86,11 @@ function searchProfiles(q, skip) {
if (!q) {
return getProfiles();
}
+ var newurl = '#/search/' + encodeURIComponent(q);
+ if (window.location.hash !== newurl) {
+ ignore_next_hashchange = true;
+ window.location = newurl;
+ }
$('#user-search-form .control-group').removeClass('error');
$('#user-search-form .help-inline').text('');
$('#user-search-submit').button('loading');

0 comments on commit bc9036a

Please sign in to comment.
Something went wrong with that request. Please try again.