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);

View File

@@ -9,7 +9,7 @@ describe('html templates', function () {
});
it('should support each', function () {
var t = '{foreach n in m.lst}{each c in m.lst2}{n}{c}{/}{/}';
var t = '{each n in m.lst}{each c in m.lst2}{n}{c}{/}{/}';
var compiled = html.compileTemplate(t);
var result = compiled({lst:[1,2,3], lst2:['a','b']});
console.log(result);

View File

@@ -0,0 +1,50 @@
describe('hash arguments parser', function() {
var hash = app.get('hash');
it('should parse empty', function() {
var args = hash.getArgs('');
expect(args).not.toBe(null);
expect(args).toBeDefined();
expect(args.commands).toEqual([]);
});
it('should parse single command', function() {
var args = hash.getArgs('#cmd');
expect(args).not.toBe(null);
expect(args).toBeDefined();
expect(args.commands).toEqual(['cmd']);
});
it('should parse multiple commands', function() {
var args = hash.getArgs('#1|2||1^2||~2');
expect(args).not.toBe(null);
expect(args).toBeDefined();
expect(args.commands).toEqual(['1|2', '1^2', '~2']);
});
it('should parse multiple commands url encoded', function() {
var args = hash.getArgs('#' + encodeURI('1|2||1^2||~2'));
expect(args).not.toBe(null);
expect(args).toBeDefined();
expect(args.commands).toEqual(['1|2', '1^2', '~2']);
});
it('should parse multiple commands and switcher encoded', function() {
var args = hash.getArgs('#' + encodeURI('1|2||1^2||~2||-notrack||-debug'));
expect(args).not.toBe(null);
expect(args).toBeDefined();
expect(args.commands).toEqual(['1|2', '1^2', '~2']);
expect(args.notrack).toBe(true);
expect(args.debug).toBe(true);
});
it('should parse only switchers encoded', function() {
var args = hash.getArgs('#' + encodeURI('-notrack||-debug'));
expect(args).not.toBe(null);
expect(args).toBeDefined();
expect(args.commands).toEqual([]);
expect(args.notrack).toBe(true);
expect(args.debug).toBe(true);
});
});