Implemented support of multiple commands trough hash

This commit is contained in:
Borys Levytskyi
2015-04-15 20:41:21 +03:00
parent 3a071c8ef6
commit 8e30d2f899
4 changed files with 95 additions and 7 deletions

View File

@@ -197,15 +197,13 @@
app.bootstrap(document.getElementById('rootView'));
var cmd = app.get('cmd');
var hashArgs = app.get('hashArgs');
cmd.execute('help');
if(window.location.hash.length > 1) {
cmd.execute(app.get('hash').decodeHash(window.location.hash));
if(hashArgs.commands.length > 0) {
hashArgs.commands.forEach(cmd.execute.bind(cmd));
}
else {
cmd.execute('help');
cmd.execute('1|2');
cmd.execute('1<<0x2a');
cmd.execute('2 4 8 16 32');

View File

@@ -13,8 +13,48 @@
},
decodeHash: function(hashValue) {
return decodeURI(hashValue).replace(/^\#/, '').replace(/,/g,' ');
},
getArgs: function (hashValue) {
core.should.beString(hashValue, 'hashValue');
var decodedHash = this.decodeHash(hashValue),
args = {
commands: []
};
splitHashList(decodedHash).forEach(function(value) {
if(/^\-[a-zA-Z]+$/.test(value)) {
args[value.substr(1)] = true;
return;
}
args.commands.push(value);
});
return Object.freeze(args);
}
};
function splitHashList(str) {
var values = [];
if(str.indexOf('||')) {
str.split('||').forEach(function (v) {
if (v.length > 0) {
values.push(v);
}
});
} else {
values.push(str);
}
return values;
}
});
app.set('hashArgs', function() {
return app.get('hash').getArgs(window.location.hash);
})
})(window.app, window.core);