Commit 1bbc3909 authored by Thomas Lechauve's avatar Thomas Lechauve

Create new project: a routing javascript library

Further it will be integrate in vifib web application
parent 47c02662
/*! jQuery Slapos - v0.1.0 - 2012-05-11
* Copyright (c) 2012 Nexedi; Licensed */
(function ($) {
"use strict";
var methods = {
init: function (options) {
var settings = $.extend({
'host': '',
'access_token': '',
'clientID': ''
}, options);
return this.each(function () {
var setting;
methods.store = Modernizr.localstorage ? methods.lStore : methods.cStore;
for (setting in settings) {
if (settings.hasOwnProperty(setting)) {
$(this).slapos('store', setting, settings[setting]);
}
}
});
},
/* Getters & Setters shortcuts */
access_token: function (value) {
return $(this).slapos('store', 'access_token', value);
},
host: function (value) {
return $(this).slapos('store', 'host', value);
},
clientID: function (value) {
return $(this).slapos('store', 'clientID', value);
},
/* Local storage method */
lStore: function (name, value) {
if (Modernizr.localstorage) {
return value === undefined ? window.localStorage[name] : window.localStorage[name] = value;
}
return false;
},
/* Cookie storage method */
cStore: function (name, value) {
if (value !== undefined) {
document.cookie = name + "=" + value + ";domain=" + window.location.hostname + ";path=" + window.location.pathname;
} else {
var i, x, y, cookies = document.cookie.split(';');
for (i = 0; i < cookies.length; i += 1) {
x = cookies[i].substr(0, cookies[i].indexOf('='));
y = cookies[i].substr(cookies[i].indexOf('=') + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x === name) {
return unescape(y);
}
}
}
},
statusDefault: function () {
return {
0: function () { console.error("status error code: 0"); },
404: function () { console.error("status error code: Not Found !"); }
};
},
request: function (type, url, callback, statusCode, data) {
data = data || '';
statusCode = statusCode || this.statusDefault;
return this.each(function () {
$.ajax({
url: $(this).slapos('host') + url,
type: type,
contentType: 'application/octet-stream',
data: JSON.stringify(data),
dataType: 'json',
context: $(this),
beforeSend: function (xhr) {
if ($(this).slapos("access_token")) {
xhr.setRequestHeader("Authorization", $(this).slapos("store", "token_type") + " " + $(this).slapos("access_token"));
xhr.setRequestHeader("Accept", "application/json");
}
},
statusCode: statusCode,
success: callback
});
});
},
newInstance: function (data, callback, statusEvent) {
return $(this).slapos('request', 'POST', '/request', callback, statusEvent, data);
},
deleteInstance: function (id, callback, statusEvent) {
return $(this).slapos('request', 'DELETE', '/instance/' + id, callback, statusEvent);
},
getInstance: function (id, callback, statusEvent) {
return $(this).slapos('request', 'GET', '/instance/' + id, callback, statusEvent);
},
getInstanceCert: function (id, callback, statusEvent) {
return $(this).slapos('request', 'GET', '/instance/' + id + '/certificate', callback, statusEvent);
},
bangInstance: function (id, log, callback, statusEvent) {
return $(this).slapos('request', 'POST', '/instance/' + id + '/bang', callback, statusEvent, log);
},
editInstance: function (id, data, callback, statusEvent) {
return $(this).slapos('request', 'PUT', '/instance/' + id, callback, statusEvent, data);
},
newComputer: function (data, callback, statusEvent) {
return $(this).slapos('request', 'POST', '/computer', callback, statusEvent, data);
},
getComputer: function (id, callback, statusEvent) {
return $(this).slapos('request', 'GET', '/computer/' + id, callback, statusEvent);
},
editComputer: function (id, data, callback, statusEvent) {
return $(this).slapos('request', 'PUT', '/computer/' + id, callback, statusEvent, data);
},
newSoftware: function (computerId, data, callback, statusEvent) {
return $(this).slapos('request', 'POST', '/computer/' + computerId + '/supply', callback, statusEvent, data);
},
bangComputer: function (id, log, callback, statusEvent) {
return $(this).slapos('request', 'POST', '/computer/' + id + '/bang', callback, statusEvent, log);
},
computerReport: function (id, data, callback, statusEvent) {
return $(this).slapos('request', 'POST', '/computer/' + id + '/report', callback, statusEvent, data);
}
};
$.fn.slapos = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.slapos');
}
};
}(jQuery));
/*! jQuery Slapos - v0.1.0 - 2012-05-11
* Copyright (c) 2012 Nexedi; Licensed */
(function(a){"use strict";var b={init:function(c){var d=a.extend({host:"",access_token:"",clientID:""},c);return this.each(function(){var c;b.store=Modernizr.localstorage?b.lStore:b.cStore;for(c in d)d.hasOwnProperty(c)&&a(this).slapos("store",c,d[c])})},access_token:function(b){return a(this).slapos("store","access_token",b)},host:function(b){return a(this).slapos("store","host",b)},clientID:function(b){return a(this).slapos("store","clientID",b)},lStore:function(a,b){return Modernizr.localstorage?b===undefined?window.localStorage[a]:window.localStorage[a]=b:!1},cStore:function(a,b){if(b!==undefined)document.cookie=a+"="+b+";domain="+window.location.hostname+";path="+window.location.pathname;else{var c,d,e,f=document.cookie.split(";");for(c=0;c<f.length;c+=1){d=f[c].substr(0,f[c].indexOf("=")),e=f[c].substr(f[c].indexOf("=")+1),d=d.replace(/^\s+|\s+$/g,"");if(d===a)return unescape(e)}}},statusDefault:function(){return{0:function(){console.error("status error code: 0")},404:function(){console.error("status error code: Not Found !")}}},request:function(b,c,d,e,f){return f=f||"",e=e||this.statusDefault,this.each(function(){a.ajax({url:a(this).slapos("host")+c,type:b,contentType:"application/octet-stream",data:JSON.stringify(f),dataType:"json",context:a(this),beforeSend:function(b){a(this).slapos("access_token")&&(b.setRequestHeader("Authorization",a(this).slapos("store","token_type")+" "+a(this).slapos("access_token")),b.setRequestHeader("Accept","application/json"))},statusCode:e,success:d})})},newInstance:function(b,c,d){return a(this).slapos("request","POST","/request",c,d,b)},deleteInstance:function(b,c,d){return a(this).slapos("request","DELETE","/instance/"+b,c,d)},getInstance:function(b,c,d){return a(this).slapos("request","GET","/instance/"+b,c,d)},getInstanceCert:function(b,c,d){return a(this).slapos("request","GET","/instance/"+b+"/certificate",c,d)},bangInstance:function(b,c,d,e){return a(this).slapos("request","POST","/instance/"+b+"/bang",d,e,c)},editInstance:function(b,c,d,e){return a(this).slapos("request","PUT","/instance/"+b,d,e,c)},newComputer:function(b,c,d){return a(this).slapos("request","POST","/computer",c,d,b)},getComputer:function(b,c,d){return a(this).slapos("request","GET","/computer/"+b,c,d)},editComputer:function(b,c,d,e){return a(this).slapos("request","PUT","/computer/"+b,d,e,c)},newSoftware:function(b,c,d,e){return a(this).slapos("request","POST","/computer/"+b+"/supply",d,e,c)},bangComputer:function(b,c,d,e){return a(this).slapos("request","POST","/computer/"+b+"/bang",d,e,c)},computerReport:function(b,c,d,e){return a(this).slapos("request","POST","/computer/"+b+"/report",d,e,c)}};a.fn.slapos=function(c){if(b[c])return b[c].apply(this,Array.prototype.slice.call(arguments,1));if(typeof c=="object"||!c)return b.init.apply(this,arguments);a.error("Method "+c+" does not exist on jQuery.slapos")}})(jQuery);
\ No newline at end of file
/*global module:false*/
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: '<json:package.json>',
meta: {
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' +
'* Copyright (c) <%= grunt.template.today("yyyy") %> Nexedi;' +
' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
},
concat: {
dist: {
src: ['<banner:meta.banner>', '<file_strip_banner:src/<%= pkg.name %>.js>'],
dest: 'dist/<%= pkg.name %>.js'
}
},
min: {
dist: {
src: ['<banner:meta.banner>', '<config:concat.dist.dest>'],
dest: 'dist/<%= pkg.name %>.min.js'
}
},
qunit: {
files: ['test/**/*.html']
},
lint: {
files: ['grunt.js', 'src/**/*.js', 'test/**/*.js']
},
watch: {
files: '<config:lint.files>',
tasks: 'lint qunit'
},
jshint: {
options: {
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
browser: true
},
globals: {
jQuery: true,
Modernizr: true,
console: true,
unescape: true,
// Needed to avoid "not defined error" with sinonJs
sinon: true,
module: true,
test: true,
ok: true,
expect: true,
stop: true,
start: true,
equal: true
}
},
uglify: {}
});
// Default task.
grunt.registerTask('default', 'lint qunit concat min');
};
This diff is collapsed.
This diff is collapsed.
/**
* QUnit v1.4.0 - A JavaScript Unit Testing Framework
*
* http://docs.jquery.com/QUnit
*
* Copyright (c) 2012 John Resig, Jörn Zaefferer
* Dual licensed under the MIT (MIT-LICENSE.txt)
* or GPL (GPL-LICENSE.txt) licenses.
*/
/** Font Family and Sizes */
#qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult {
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
}
#qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; }
#qunit-tests { font-size: smaller; }
/** Resets */
#qunit-tests, #qunit-tests ol, #qunit-header, #qunit-banner, #qunit-userAgent, #qunit-testresult {
margin: 0;
padding: 0;
}
/** Header */
#qunit-header {
padding: 0.5em 0 0.5em 1em;
color: #8699a4;
background-color: #0d3349;
font-size: 1.5em;
line-height: 1em;
font-weight: normal;
border-radius: 15px 15px 0 0;
-moz-border-radius: 15px 15px 0 0;
-webkit-border-top-right-radius: 15px;
-webkit-border-top-left-radius: 15px;
}
#qunit-header a {
text-decoration: none;
color: #c2ccd1;
}
#qunit-header a:hover,
#qunit-header a:focus {
color: #fff;
}
#qunit-header label {
display: inline-block;
}
#qunit-banner {
height: 5px;
}
#qunit-testrunner-toolbar {
padding: 0.5em 0 0.5em 2em;
color: #5E740B;
background-color: #eee;
}
#qunit-userAgent {
padding: 0.5em 0 0.5em 2.5em;
background-color: #2b81af;
color: #fff;
text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px;
}
/** Tests: Pass/Fail */
#qunit-tests {
list-style-position: inside;
}
#qunit-tests li {
padding: 0.4em 0.5em 0.4em 2.5em;
border-bottom: 1px solid #fff;
list-style-position: inside;
}
#qunit-tests.hidepass li.pass, #qunit-tests.hidepass li.running {
display: none;
}
#qunit-tests li strong {
cursor: pointer;
}
#qunit-tests li a {
padding: 0.5em;
color: #c2ccd1;
text-decoration: none;
}
#qunit-tests li a:hover,
#qunit-tests li a:focus {
color: #000;
}
#qunit-tests ol {
margin-top: 0.5em;
padding: 0.5em;
background-color: #fff;
border-radius: 15px;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
box-shadow: inset 0px 2px 13px #999;
-moz-box-shadow: inset 0px 2px 13px #999;
-webkit-box-shadow: inset 0px 2px 13px #999;
}
#qunit-tests table {
border-collapse: collapse;
margin-top: .2em;
}
#qunit-tests th {
text-align: right;
vertical-align: top;
padding: 0 .5em 0 0;
}
#qunit-tests td {
vertical-align: top;
}
#qunit-tests pre {
margin: 0;
white-space: pre-wrap;
word-wrap: break-word;
}
#qunit-tests del {
background-color: #e0f2be;
color: #374e0c;
text-decoration: none;
}
#qunit-tests ins {
background-color: #ffcaca;
color: #500;
text-decoration: none;
}
/*** Test Counts */
#qunit-tests b.counts { color: black; }
#qunit-tests b.passed { color: #5E740B; }
#qunit-tests b.failed { color: #710909; }
#qunit-tests li li {
margin: 0.5em;
padding: 0.4em 0.5em 0.4em 0.5em;
background-color: #fff;
border-bottom: none;
list-style-position: inside;
}
/*** Passing Styles */
#qunit-tests li li.pass {
color: #5E740B;
background-color: #fff;
border-left: 26px solid #C6E746;
}
#qunit-tests .pass { color: #528CE0; background-color: #D2E0E6; }
#qunit-tests .pass .test-name { color: #366097; }
#qunit-tests .pass .test-actual,
#qunit-tests .pass .test-expected { color: #999999; }
#qunit-banner.qunit-pass { background-color: #C6E746; }
/*** Failing Styles */
#qunit-tests li li.fail {
color: #710909;
background-color: #fff;
border-left: 26px solid #EE5757;
white-space: pre;
}
#qunit-tests > li:last-child {
border-radius: 0 0 15px 15px;
-moz-border-radius: 0 0 15px 15px;
-webkit-border-bottom-right-radius: 15px;
-webkit-border-bottom-left-radius: 15px;
}
#qunit-tests .fail { color: #000000; background-color: #EE5757; }
#qunit-tests .fail .test-name,
#qunit-tests .fail .module-name { color: #000000; }
#qunit-tests .fail .test-actual { color: #EE5757; }
#qunit-tests .fail .test-expected { color: green; }
#qunit-banner.qunit-fail { background-color: #EE5757; }
/** Result */
#qunit-testresult {
padding: 0.5em 0.5em 0.5em 2.5em;
color: #2b81af;
background-color: #D2E0E6;
border-bottom: 1px solid white;
}
/** Fixture */
#qunit-fixture {
position: absolute;
top: -10000px;
left: -10000px;
width: 1000px;
height: 1000px;
}
This diff is collapsed.
This diff is collapsed.
/**
* sinon-qunit 1.0.0, 2010/12/09
*
* @author Christian Johansen (christian@cjohansen.no)
*
* (The BSD License)
*
* Copyright (c) 2010-2011, Christian Johansen, christian@cjohansen.no
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Christian Johansen nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*global sinon, QUnit, test*/
sinon.assert.fail = function (msg) {
QUnit.ok(false, msg);
};
sinon.assert.pass = function (assertion) {
QUnit.ok(true, assertion);
};
sinon.config = {
injectIntoThis: true,
injectInto: null,
properties: ["spy", "stub", "mock", "clock", "sandbox"],
useFakeTimers: true,
useFakeServer: false
};
(function (global) {
var qTest = QUnit.test;
QUnit.test = global.test = function (testName, expected, callback, async) {
if (arguments.length === 2) {
callback = expected;
expected = null;
}
return qTest(testName, expected, sinon.test(callback), async);
};
}(this));
This diff is collapsed.
{
"name": "jquery.slapos",
"title": "jQuery Slapos",
"description": "API to use slapos stuff in a web application",
"version": "0.1.0",
"homepage": "",
"author": {
"name": "Thomas Lechauve",
"email": "thomas.lechauve@gmail.com"
},
"repository": {
"type": "git",
"url": "http://git.erp5.org/repos/slapos.core.git"
},
"bugs": {
"url": ""
},
"licenses": [
],
"dependencies": {
"jquery": "~1.7",
"sinon": "1.3.2",
"modernizr": "2.5.3"
},
"keywords": []
}
(function ($) {
'use strict';
$.extend({
router: {
routes: {
list: [],
add: function (route, level, callback, context) {
if (typeof this.list[level] === 'undefined') {
this.list[level] = [];
}
var r = {
'route': route,
'level': level,
'callback': function (params) {
if (callback !== undefined) {
if (context !== undefined) {
callback(params);
} else {
callback.call(context, params);
}
}
}
},
i = this.list[level].length;
this.list[level][i] = r;
},
clean: function (level) {
this.list = this.list.slice(0, level);
},
cleanAll: function () {
this.list = this.list.slice(0, 0);
}
},
hashHandler: function () {
var newHash = window.location.href.split('#')[1];
console.log('hash change to : ' + newHash);
},
}
});
$(window).bind('hashchange', $.router.hashchangeHandler);
}(jQuery));
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Slapos Test Suite</title>
<!-- Load local jQuery, removing access to $ (use jQuery, not $). -->
<script src="../libs/jquery/jquery-1.7.2.js"></script>
<!-- Load local QUnit (grunt requires v1.0.0 or newer). -->
<link rel="stylesheet" href="../libs/qunit/qunit.css" media="screen">
<script src="../libs/qunit/qunit.js"></script>
<!-- Load local lib and tests. -->
<script src="../src/jquery.urljs.js"></script>
<script src="jquery.urljs_test.js"></script>
</head>
<body>
<h1 id="qunit-header">jQuery Slapos Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
<span>lame test markup</span>
<span>normal test markup</span>
<span>awesome test markup</span>
</div>
</body>
</html>
$(function () {
module("routes list tests", {
setup: function () {
var url1 = "/url1",
url11 = "/url1/1",
url12 = "/url1/2",
url111 = "/url1/1/1",
url2 = "/url2";
$.router.routes.add(url1, 0);
$.router.routes.add(url2, 0);
$.router.routes.add(url11, 1);
$.router.routes.add(url12, 1);
$.router.routes.add(url111, 2);
},
teardown: function () {
$.router.routes.clean(0);
}
});
test('add test', function () {
console.log($.router.routes.list)
equal($.router.routes.list.length, 3, "should contains 3 levels");
equal($.router.routes.list[0].length, 2, "should contains 2 routes level 0");
equal($.router.routes.list[1].length, 2, "should contains 2 routes level 1");
equal($.router.routes.list[2].length, 1, "should contains 1 routes level 2");
});
test('clean test', function () {
$.router.routes.clean(2);
equal($.router.routes.list.length, 2, "should remove all routes level 2");
$.router.routes.clean(1);
equal($.router.routes.list.length, 1, "should remove all routes level 1");
});
test('clean all test', function () {
$.router.routes.cleanAll();
equal($.router.routes.list.length, 0, "should remove all routes");
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment