Skip to content

Commit

Permalink
As per Reynald Borer's suggestion, Build Monitor now uses YAHOO.util.…
Browse files Browse the repository at this point in the history
…Cookie object instead of AngularJS cookie service and is able to set cookies that live longer than a browser session (about a year longer, to be precise).

This closes jenkinsci#10 - thanks @rborer !
  • Loading branch information
jan-molak committed Sep 28, 2013
1 parent 126fd0f commit 590792c
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ you'll need to apply your changes again after restart. I'm working on this one,
1. Support for [Claim Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Claim+plugin)
1. Support for [Gravatar](http://gravatar.com)
1. Display parameters of parametrized jobs
1. Persist layout configuration changes in a long-lived cookie.
1. ~~Persist layout configuration changes in a long-lived cookie.~~

## License: MIT

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<link rel="stylesheet" href="${resourcesURL}/themes/industrial.css"/>

<script src="${resourcesURL}/vendor/angular-${angularVersion}.min.js"></script>
<script src="${resourcesURL}/vendor/angular-cookies-${angularVersion}.min.js"></script>
<script src="${resourcesURL}/vendor/ui-bootstrap-custom-tpls-0.4.0.js"></script>
<script src="${resourcesURL}/vendor/angular-slider.js"></script>
</l:header>
Expand Down Expand Up @@ -82,10 +81,20 @@
<script>
'use strict';

angular.module('buildMonitor').config(function(proxyProvider) {
proxyProvider.configureProxiesUsing(window.bindings);
});
angular.module('buildMonitor.services').value('buildMonitorName', '${it.displayName}');
angular.

module('buildMonitor').

config(function(proxyProvider, cookieJarProvider, hashCodeProvider) {
var hashCodeOf = hashCodeProvider.hashCodeOf;

proxyProvider.configureProxiesUsing(window.bindings);

cookieJarProvider.describe({
label: 'buildMonitor.' + hashCodeOf('${it.displayName}'),
shelfLife: 365
});
});
</script>
</l:main-panel>
</l:layout>
Expand Down
10 changes: 5 additions & 5 deletions src/main/webapp/scripts/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
angular.
module('buildMonitor.controllers', [ 'buildMonitor.services', 'uiSlider']).

controller('JobViews', function($scope, $rootScope, $dialog, $timeout, proxy, storage) {
$scope.fontSize = storage.retrieve('fontSize', 1);
$scope.numberOfColumns = storage.retrieve('numberOfColumns', 2);
controller('JobViews', function($scope, $rootScope, $dialog, $timeout, proxy, cookieJar) {
$scope.fontSize = cookieJar.get('fontSize', 1);
$scope.numberOfColumns = cookieJar.get('numberOfColumns', 2);

$scope.$watch('fontSize', function(currentFontSize) {
storage.persist('fontSize', currentFontSize);
cookieJar.put('fontSize', currentFontSize);
});
$scope.$watch('numberOfColumns', function(currentNumberOfColumns) {
storage.persist('numberOfColumns', currentNumberOfColumns);
cookieJar.put('numberOfColumns', currentNumberOfColumns);
});


Expand Down
67 changes: 53 additions & 14 deletions src/main/webapp/scripts/services.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';

angular.
module('buildMonitor.services', ['ui.bootstrap.dialog', 'template/dialog/message.html', 'ngCookies']).
module('buildMonitor.services', ['ui.bootstrap.dialog', 'template/dialog/message.html']).

value('YahooCookie', YAHOO.util.Cookie).

service('notifyUser',function ($dialog, $window) {
this.about = function (problemStatus) {
Expand All @@ -21,26 +23,63 @@ angular.
}
}).

service('storage',function ($cookies, buildMonitorName, hashCodeOf) {
this.persist = function (name, value) {
$cookies[prefix(name)] = value;
provider('cookieJar',function () {
var defaultAttributes = {
label: '',
shelfLife: 0
},
attributes = {};

this.describe = function (cookieJarAttributes) {
attributes = cookieJarAttributes;
}

this.retrieve = function (name, defaultValue) {
var value = $cookies[prefix(name)];
this.$get = ['YahooCookie', function (YahooCookie) {
return new CookieJar(YahooCookie, angular.extend(defaultAttributes, attributes));
}];

return (typeof value !== 'undefined')
? value
: defaultValue;
}

function prefix(name) {
return 'buildMonitor.' + hashCodeOf(buildMonitorName) + '.' + name;
function CookieJar(YahooCookie, attributes) {

function expiryDetailsBasedOn(days) {
if (days <= 0) {
return {};
}

return {
expires: new Date(+new Date() + (days * 1000 * 3600 * 24))
}
}

function prefixed(name) {
return attributes.label
? attributes.label + '.' + name
: name;
}

return {
put: function (name, value) {
YahooCookie.set(prefixed(name), value, expiryDetailsBasedOn(attributes.shelfLife));
},
get: function (name, defaultValue) {
var value = YahooCookie.get(prefixed(name));

return (value !== null)
? value
: defaultValue;
}
}
}
}).

factory('hashCodeOf', function() {
return function(name) {
provider('hashCode', function () {
this.hashCodeOf = hashCodeOf;

this.$get = function() {
return hashCodeOf
}

function hashCodeOf(name) {
var name = name || '',
hash = 0,
char;
Expand Down
7 changes: 0 additions & 7 deletions src/main/webapp/vendor/angular-cookies-1.1.5.min.js

This file was deleted.

124 changes: 124 additions & 0 deletions src/test/javascript/unit/services/cookieJarSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
'use strict';

describe('buildMonitor', function () {
describe('buildMonitor.services', function () {
describe('cookieJar', function () {

var services,
YahooCookie = YAHOO.util.Cookie,
mockedCookie, // YAHOO.util.Cookie backend used by the cookieJar

NAME = 'numberOfColumns',
VALUE = 3,
DEFAULT_VALUE = 1;

beforeEach(function () {
services = angular.module('buildMonitor.services');

mockedCookie = sinon.mock(YahooCookie);

module('buildMonitor.services', function ($provide) {
$provide.value('YahooCookie', YahooCookie);
});
});

describe('default configuration', function() {

it('returns "undefined" if requested value couldn\'t be found in a cookie and no defaults are provided', inject(function (cookieJar) {
expect(cookieJar.get('aCookieThatDoesNotExist')).toBeUndefined();
}));

it('returns default value if the one requested couldn\'t be found in a cookie', inject(function (cookieJar) {
expect(cookieJar.get('aCookieThatDoesNotExist', DEFAULT_VALUE)).toBe(DEFAULT_VALUE);
}));

it('allows for a value to be persisted and then retrieved', inject(function (cookieJar) {
mockedCookie.expects("set").withArgs(NAME, VALUE);
mockedCookie.expects("get").withArgs(NAME).returns(VALUE);


cookieJar.put(NAME, VALUE);

expect(cookieJar.get(NAME)).toBe(VALUE);

mockedCookie.verify()
}));

it('should use cookies that expire at the end of the session', inject(function (cookieJar) {
var noExpiryDateSpecified = {};

mockedCookie.expects("set").withArgs(NAME, VALUE, noExpiryDateSpecified);


cookieJar.put(NAME, VALUE);

mockedCookie.verify();
}));
});

describe('custom label', function () {

var COOKIE_JAR_LABEL = 'chocolateChipCookies';

beforeEach(function () {
services.config(function(cookieJarProvider) {
cookieJarProvider.describe({
label: COOKIE_JAR_LABEL
});
});
});

it('should prefix each cookie with a label', inject(function (cookieJar) {
mockedCookie.expects("set").withArgs(prefixed(NAME), VALUE);
mockedCookie.expects("get").withArgs(prefixed(NAME));


cookieJar.put(NAME, VALUE);
cookieJar.get(NAME);

mockedCookie.verify();
}));

afterEach(function() {
mockedCookie.restore();
});

function prefixed(name) {
return COOKIE_JAR_LABEL + '.' + name;
};
});

describe('custom shelf life', function() {

var SHELF_LIFE_IN_DAYS = 7;

beforeEach(function () {

services.config(function(cookieJarProvider) {
cookieJarProvider.describe({
shelfLife: SHELF_LIFE_IN_DAYS
});
});
});

it('should use cookies that expire when specified', inject(function (cookieJar) {
mockedCookie.expects("set").withArgs(NAME, VALUE, {
expires: dateIn(SHELF_LIFE_IN_DAYS)}
);

cookieJar.put(NAME, VALUE);

mockedCookie.verify();
}));

afterEach(function() {
mockedCookie.restore();
});

function dateIn(days) {
return new Date(+new Date() + (days * 24 * 3600 * 1000));
};
});
});
});
});
18 changes: 9 additions & 9 deletions src/test/javascript/unit/services/hashCodeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

describe('buildMonitor', function () {
describe('buildMonitor.services', function () {
describe('hashCodeOf', function () {
describe('hashCode', function () {

beforeEach(module('buildMonitor.services'));

it('produces a hash code uniquely identifying a string of text', inject(function (hashCodeOf) {
expect(hashCodeOf('')).toEqual(0);
expect(hashCodeOf('name')).toEqual(3373707)
expect(hashCodeOf('Name')).toEqual(2420395);
it('produces a hash code uniquely identifying a string of text', inject(function (hashCode) {
expect(hashCode('')).toEqual(0);
expect(hashCode('name')).toEqual(3373707)
expect(hashCode('Name')).toEqual(2420395);
}));

it('should treat "undefined" the same way as empty string', inject(function (hashCodeOf) {
expect(hashCodeOf()).toEqual(hashCodeOf(''));
it('should treat "undefined" the same way as empty string', inject(function (hashCode) {
expect(hashCode()).toEqual(hashCode(''));
}));

it('should treat "null" the same way as empty string', inject(function (hashCodeOf) {
expect(hashCodeOf(null)).toEqual(hashCodeOf(''));
it('should treat "null" the same way as empty string', inject(function (hashCode) {
expect(hashCode(null)).toEqual(hashCode(''));
}));
});
});
Expand Down
42 changes: 0 additions & 42 deletions src/test/javascript/unit/services/storageSpec.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/test/resources/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = function(config) {
'src/main/webapp/vendor/ui-*.js',
'src/test/resources/vendor/angular-mocks.js',
'src/test/resources/vendor/sinon-1.7.3.js',
'src/test/resources/vendor/yahoo-2.9.0.min.js',
'src/test/resources/vendor/yahoo-cookie-2.9.0.min.js',
'src/main/webapp/scripts/**/*.js',
'src/test/javascript/**/*Spec.js'
],
Expand Down
Loading

0 comments on commit 590792c

Please sign in to comment.