diff --git a/app/common/main-controller.js b/app/common/main-controller.js index a09b0b4..f6127d9 100644 --- a/app/common/main-controller.js +++ b/app/common/main-controller.js @@ -1,6 +1,6 @@ angular.module('JamStash') -.controller('AppController', ['$scope', '$rootScope', '$document', '$window', '$location', '$cookieStore', '$http', 'utils', 'globals', 'model', 'notifications', 'player', 'persistence', - function($scope, $rootScope, $document, $window, $location, $cookieStore, $http, utils, globals, model, notifications, player, persistence) { +.controller('AppController', ['$scope', '$rootScope', '$document', '$window', '$location', '$cookieStore', '$http', 'utils', 'globals', 'model', 'notifications', 'player', 'persistence', 'Page', + function($scope, $rootScope, $document, $window, $location, $cookieStore, $http, utils, globals, model, notifications, player, persistence, Page) { 'use strict'; $rootScope.settings = globals.settings; @@ -13,6 +13,7 @@ angular.module('JamStash') $rootScope.SelectedMusicFolder = ""; $rootScope.unity = null; + $scope.Page = Page; $rootScope.loggedIn = function () { if (globals.settings.Server !== '' && globals.settings.Username !== '' && globals.settings.Password !== '') { return true; diff --git a/app/common/page-service.js b/app/common/page-service.js new file mode 100644 index 0000000..40f630b --- /dev/null +++ b/app/common/page-service.js @@ -0,0 +1,64 @@ +/** +* jamstash.page Module +* +* Set the page's title from anywhere, the angular way +*/ +angular.module('jamstash.page', ['jamstash.settings', 'jamstash.utils']) + +.factory('Page', ['$interval', 'globals', 'utils', function($interval, globals, utils){ + 'use strict'; + + var title = 'Jamstash'; + var timer; + return { + title: function() { return title; }, + setTitle: function(newTitle) { + title = newTitle; + return this; + }, + setTitleSong: function(song) { + if (song.artist !== undefined && song.name !== undefined) { + title = utils.toHTML.un(song.artist) + " - " + utils.toHTML.un(song.name); + } else { + title = 'Jamstash'; + } + if (globals.settings.ScrollTitle) { + this.scrollTitle(); + } + + return this; + }, + scrollTitle: function() { + var shift = { + "left": function (a) { + a.push(a.shift()); + }, + "right": function (a) { + a.unshift(a.pop()); + } + }; + var opts = { + text: title, + dir: "left", + speed: 1200 + }; + + var t = (opts.text).split(""); + if (!t) { + return; + } + t.push(" "); + if (timer !== undefined) { + timer.cancel(); + } + timer = $interval(function () { + var f = shift[opts.dir]; + if (f) { + f(t); + title = t.join(""); + } + }, opts.speed); + return this; + } + }; +}]); diff --git a/app/common/page-service_test.js b/app/common/page-service_test.js new file mode 100644 index 0000000..b1994d2 --- /dev/null +++ b/app/common/page-service_test.js @@ -0,0 +1,54 @@ +describe("Page service", function() { + 'use strict'; + + var mockGlobals, Page, utils, $interval; + beforeEach(function() { + + mockGlobals = { + settings: { + ScrollTitle: false + } + }; + + module('jamstash.page', function ($provide) { + $provide.value('globals', mockGlobals); + }); + + inject(function (_Page_, _utils_, _$interval_) { + Page = _Page_; + utils = _utils_; + $interval = _$interval_; + }); + spyOn(utils.toHTML, "un").and.callFake(function (arg) { return arg; }); + }); + + describe("Given a song,", function() { + var song; + beforeEach(function() { + song = { + artist: 'Merlyn Nurse', + name: 'Exsiccator tumble' + }; + }); + + it("it displays its artist and its name as the page's title", function() { + Page.setTitleSong(song); + expect(Page.title()).toBe('Merlyn Nurse - Exsiccator tumble'); + }); + + it("if the global setting 'ScrollTitle' is true, it scrolls the page title", function() { + spyOn(Page, "scrollTitle"); + mockGlobals.settings.ScrollTitle = true; + + Page.setTitleSong(song); + + expect(Page.scrollTitle).toHaveBeenCalled(); + }); + }); + + it("Given a title, it can scroll it", function() { + Page.setTitle('unbeloved omnificent supergravitate').scrollTitle(); + $interval.flush(1201); + expect(Page.title()).toBe('nbeloved omnificent supergravitate u'); + }); +}); diff --git a/app/common/utils-service.js b/app/common/utils-service.js index 376de3b..03c0fa8 100644 --- a/app/common/utils-service.js +++ b/app/common/utils-service.js @@ -251,41 +251,6 @@ angular.module('jamstash.utils', ['jamstash.settings']) var u = strurl.substring(0, strurl.indexOf('?')); return u; }; - this.setTitle = function (text) { - if (text !== "") { - document.title = text; - } - }; - var timer = 0; - this.scrollTitle = function (text) { - var shift = { - "left": function (a) { - a.push(a.shift()); - }, - "right": function (a) { - a.unshift(a.pop()); - } - }; - var opts = { - text: text, - dir: "left", - speed: 1200 - }; - - var t = (opts.text || document.title).split(""); - if (!t) { - return; - } - t.push(" "); - clearInterval(timer); - timer = setInterval(function () { - var f = shift[opts.dir]; - if (f) { - f(t); - document.title = t.join(""); - } - }, opts.speed); - }; this.parseVersionString = function (str) { if (typeof (str) !== 'string') { return false; } var x = str.split('.'); diff --git a/app/index.html b/app/index.html index c8d4091..c4322b9 100755 --- a/app/index.html +++ b/app/index.html @@ -1,12 +1,12 @@ - + - Jamstash + Jamstash @@ -20,7 +20,7 @@ - +