(function() { msnbc.namespace('msnbc.misc'); msnbc.misc.URLCommand = Class.extend({ init: function(m, c, args){ this.module = m; this.command = c || 'generic'; this.args = args || []; }, toURLString: function(){ var s = "sp-" + this.module; s+= (this.command=='generic')?'':'-'+this.command; if (this.args.length) { s += '/'; for (var i=0; i < this.args.length; i++) { var arg = this.args[i]; for (var n in arg) { if (n=='id' && this.args.length==1) { s+= arg[n]; continue; } if (i>0) s+='-'; s += n + '-' + arg[n]; }; }; } return s; }, toShareString: function(){ // share slash link when on msnbc domain, // this is a workaround for when working off local files var sep = (window.location.host.match(/msnbc/))?'/':'#'; var url = msnbc.document.pristineURL || window.location.toString(); url = url.replace(/#.*$/, ''); url = url.replace(/\/$/, ''); return url + sep + this.toURLString(); } }); msnbc.misc.URLManager = msnbc.core.EventDispatcher.extend({ init: function(){ this._super(); this.commands = []; this.currCommands = []; this.patt = /sp(-\w{1,2})+(\/(\d+|(?!sp)[\w\d]+(-[\w\d]+)+))?/g; this.getCommands(window.location.toString()); }, getCommands: function(url){ var self = this; // split on '/' to get commands with arguments // split arguments on '-' url = url.replace(/#.*$/, ''); // then parse arguments, matched to command. ignore invalid var mt = url.match(this.patt); msnbc.log("command match = "+ mt); if (!mt) return (msnbc.log("[URLManager] no commands matched")); jQuery.each(mt, function(i) { var m; var c; var args = []; var str = this; var ca = str.split('/'); var cmd = ca[0].split('-'); m = cmd[1]; c = cmd[2]; if (ca.length>1) { var parts = ca[1].split("-"); if (parts.length==1) { args.push( {id:parts[0]} ); } else if (parts.length%2==0) { for (var j=0; j < parts.length; j+=2) { var o = {}; o[parts[j]] = parts[j+1]; args.push( o ); } } else { msnbc.warn("[URLManager] invalid pattern: "+str); } } self.commands.push(new msnbc.misc.URLCommand(m, c, args)); }); msnbc.log(this.commands); msnbc.document.pristineURL = url.replace(/[\/#]+?sp-.*$/g, ""); msnbc.log('pristine url: '+msnbc.document.pristineURL); }, updateLocation: function(command){ // need to check for duplicates // get rid of the existing commands matching the new command's module ///this.currCommands = jQuery.grep(this.currCommands, function(n,i){ /// return n.module != command.module; ///}); /// OR this.currCommands = []; /// this.currCommands.unshift(command); window.location.hash = '#'+this.commandsToString(this.currCommands); }, commandsToString: function(arrayOfCommands){ arrayOfCommands = arrayOfCommands || this.commands; var s =''; var commands= arrayOfCommands.length; for (var i=0; i < commands; i++) { s += arrayOfCommands[i].toURLString(); if (i