Replaced Underscore with Lodash

Lodash has more useful functions and is more regularly updated.
This commit is contained in:
Hyzual 2015-06-14 17:20:27 +02:00
parent 0ba6b651b1
commit 5d35c2bd1a
11 changed files with 108 additions and 48 deletions

View file

@ -1,8 +1,20 @@
'use strict'; 'use strict';
/* Declare app level module */ /* Declare app level module */
angular.module('JamStash', ['ngCookies', 'ngRoute', 'ngSanitize', 'ui.keypress', 'angular-underscore/utils', angular.module('JamStash', [
'jamstash.subsonic.controller', 'jamstash.archive.controller', 'jamstash.player.controller', 'jamstash.queue.controller', 'jamstash.settings.controller', 'jamstash.persistence', 'jamstash.loading']) 'ngCookies',
'ngRoute',
'ngSanitize',
'ngLodash',
'ui.keypress',
'jamstash.subsonic.controller',
'jamstash.archive.controller',
'jamstash.player.controller',
'jamstash.queue.controller',
'jamstash.settings.controller',
'jamstash.persistence',
'jamstash.loading'
])
.config(['$routeProvider',function ($routeProvider) { .config(['$routeProvider',function ($routeProvider) {
$routeProvider $routeProvider

View file

@ -1,6 +1,6 @@
angular.module('JamStash') angular.module('JamStash')
.controller('AppController', ['$scope', '$rootScope', '$document', '$window', '$location', '$cookieStore', '$http', 'utils', 'globals', 'model', 'notifications', 'player', 'persistence', 'Page', 'subsonic', 'Loading', .controller('AppController', ['$scope', '$rootScope', '$document', '$window', '$location', '$cookieStore', '$http', 'lodash', 'utils', 'globals', 'model', 'notifications', 'player', 'persistence', 'Page', 'subsonic', 'Loading',
function ($scope, $rootScope, $document, $window, $location, $cookieStore, $http, utils, globals, model, notifications, player, persistence, Page, subsonic, Loading) { function ($scope, $rootScope, $document, $window, $location, $cookieStore, $http, _, utils, globals, model, notifications, player, persistence, Page, subsonic, Loading) {
'use strict'; 'use strict';
$rootScope.settings = globals.settings; $rootScope.settings = globals.settings;
@ -47,10 +47,10 @@ angular.module('JamStash')
} }
var settings = persistence.getSettings(); var settings = persistence.getSettings();
if (settings !== undefined) { if (settings !== undefined) {
var updSettings = _(settings).omit('Url'); var updSettings = _.omit(settings, 'Url');
// We can't just assign settings to globals.settings because it's on the scope // We can't just assign settings to globals.settings because it's on the scope
// TODO: Hyz: remove $rootScope.settings and replace with individual settings // TODO: Hyz: remove $rootScope.settings and replace with individual settings
_(updSettings).each(function(val, key) { _.each(updSettings, function(val, key) {
globals.settings[key] = val; globals.settings[key] = val;
}); });
} }

View file

@ -48,7 +48,7 @@ describe("Main controller", function() {
"updateMessage" "updateMessage"
]); ]);
inject(function (_$controller_, $rootScope, _$q_, _$document_, _$window_, _$location_, _$cookieStore_, _utils_, globals, _model_, _Page_) { inject(function (_$controller_, $rootScope, _$q_, _$document_, _$window_, _$location_, _$cookieStore_, lodash, _utils_, globals, _model_, _Page_) {
scope = $rootScope.$new(); scope = $rootScope.$new();
utils = _utils_; utils = _utils_;
$q = _$q_; $q = _$q_;
@ -64,6 +64,7 @@ describe("Main controller", function() {
$window: _$window_, $window: _$window_,
$location: _$location_, $location: _$location_,
$cookieStore: _$cookieStore_, $cookieStore: _$cookieStore_,
lodash: lodash,
utils: utils, utils: utils,
globals: globals, globals: globals,
model: _model_, model: _model_,

View file

@ -5,7 +5,7 @@
* Provides load, save and delete operations for the current song and queue. * Provides load, save and delete operations for the current song and queue.
* Data storage provided by HTML5 localStorage. * Data storage provided by HTML5 localStorage.
*/ */
angular.module('jamstash.persistence', ['angular-locker', angular.module('jamstash.persistence', ['ngLodash', 'angular-locker',
'jamstash.settings.service', 'jamstash.player.service', 'jamstash.notifications', 'jamstash.utils']) 'jamstash.settings.service', 'jamstash.player.service', 'jamstash.notifications', 'jamstash.utils'])
.config(['lockerProvider', function (lockerProvider) { .config(['lockerProvider', function (lockerProvider) {
@ -14,8 +14,8 @@ angular.module('jamstash.persistence', ['angular-locker',
.setEventsEnabled(false); .setEventsEnabled(false);
}]) }])
.service('persistence', ['globals', 'player', 'notifications', 'locker', 'json', 'jamstashVersionChangesets', 'utils', .service('persistence', ['lodash', 'globals', 'player', 'notifications', 'locker', 'json', 'jamstashVersionChangesets', 'utils',
function (globals, player, notifications, locker, json, jamstashVersionChangesets, utils) { function (_, globals, player, notifications, locker, json, jamstashVersionChangesets, utils) {
/* Manage current track */ /* Manage current track */
this.loadTrackPosition = function () { this.loadTrackPosition = function () {
// Load Saved Song // Load Saved Song
@ -121,12 +121,12 @@ angular.module('jamstash.persistence', ['angular-locker',
this.upgradeVersion = function (currentVersion, finalVersion) { this.upgradeVersion = function (currentVersion, finalVersion) {
var settings = locker.get('Settings'); var settings = locker.get('Settings');
// Apply all upgrades older than the final version and newer than the current // Apply all upgrades older than the final version and newer than the current
var allUpgrades = _(jamstashVersionChangesets.versions).filter(function (toApply) { var allUpgrades = _.filter(jamstashVersionChangesets.versions, function (toApply) {
var olderOrEqualToFinal = utils.checkVersion(finalVersion, toApply.version); var olderOrEqualToFinal = utils.checkVersion(finalVersion, toApply.version);
var newerThanCurrent = utils.checkVersionNewer(toApply.version, currentVersion); var newerThanCurrent = utils.checkVersionNewer(toApply.version, currentVersion);
return olderOrEqualToFinal && newerThanCurrent; return olderOrEqualToFinal && newerThanCurrent;
}); });
_(allUpgrades).each(function (versionUpg) { _.forEach(allUpgrades, function (versionUpg) {
versionUpg.changeset(settings); versionUpg.changeset(settings);
}); });
this.saveSettings(settings); this.saveSettings(settings);

View file

@ -92,10 +92,9 @@
<script src="bower_components/notify.js/notify.js"></script> <script src="bower_components/notify.js/notify.js"></script>
<script src="bower_components/jquery.scrollTo/jquery.scrollTo.js"></script> <script src="bower_components/jquery.scrollTo/jquery.scrollTo.js"></script>
<script src="bower_components/jquery-dateFormat/dist/jquery-dateFormat.js"></script> <script src="bower_components/jquery-dateFormat/dist/jquery-dateFormat.js"></script>
<script src="bower_components/underscore/underscore.js"></script>
<script src="bower_components/angular-underscore/angular-underscore.js"></script>
<script src="bower_components/angular-locker/dist/angular-locker.min.js"></script> <script src="bower_components/angular-locker/dist/angular-locker.min.js"></script>
<script src="bower_components/angular-ui-utils/keypress.js"></script> <script src="bower_components/angular-ui-utils/keypress.js"></script>
<script src="bower_components/ng-lodash/build/ng-lodash.js"></script>
<!-- endbower --> <!-- endbower -->
<!-- endbuild --> <!-- endbuild -->
<!-- our scripts --> <!-- our scripts -->

View file

@ -3,9 +3,9 @@
* *
* Manages the player and playing queue. Use it to play a song, go to next track or add songs to the queue. * Manages the player and playing queue. Use it to play a song, go to next track or add songs to the queue.
*/ */
angular.module('jamstash.player.service', ['angular-underscore/utils']) angular.module('jamstash.player.service', ['ngLodash'])
.factory('player', [function () { .factory('player', ['lodash', function (_) {
'use strict'; 'use strict';
var playerVolume = 1.0; var playerVolume = 1.0;
@ -105,8 +105,8 @@ angular.module('jamstash.player.service', ['angular-underscore/utils'])
}, },
shuffleQueue: function () { shuffleQueue: function () {
var shuffled = _(player.queue).without(player._playingSong); var shuffled = _.without(player.queue, player._playingSong);
shuffled = _(shuffled).shuffle(); shuffled = _.shuffle(shuffled);
if(player._playingSong !== undefined) { if(player._playingSong !== undefined) {
shuffled.unshift(player._playingSong); shuffled.unshift(player._playingSong);
player._playingIndex = 0; player._playingIndex = 0;
@ -132,7 +132,7 @@ angular.module('jamstash.player.service', ['angular-underscore/utils'])
}, },
removeSongs: function (songs) { removeSongs: function (songs) {
player.queue = _(player.queue).difference(songs); player.queue = _.difference(player.queue, songs);
return player; return player;
}, },

View file

@ -4,11 +4,30 @@
* Provides access through $http to the Subsonic server's API. * Provides access through $http to the Subsonic server's API.
* Also offers more fine-grained functionality that is not part of Subsonic's API. * Also offers more fine-grained functionality that is not part of Subsonic's API.
*/ */
angular.module('jamstash.subsonic.service', ['angular-underscore/utils', angular.module('jamstash.subsonic.service', [
'jamstash.settings.service', 'jamstash.utils', 'jamstash.model']) 'ngLodash',
'jamstash.settings.service',
'jamstash.utils',
'jamstash.model'
])
.factory('subsonic', ['$rootScope', '$http', '$q', 'globals', 'utils', 'map', .factory('subsonic', [
function ($rootScope, $http, $q, globals, utils, map) { '$rootScope',
'$http',
'$q',
'lodash',
'globals',
'utils',
'map',
function (
$rootScope,
$http,
$q,
_,
globals,
utils,
map
) {
'use strict'; 'use strict';
//TODO: Hyz: Remove when refactored //TODO: Hyz: Remove when refactored
@ -152,7 +171,7 @@ angular.module('jamstash.subsonic.service', ['angular-underscore/utils',
var formattedResponse = {}; var formattedResponse = {};
formattedResponse.shortcut = [].concat(subsonicResponse.indexes.shortcut); formattedResponse.shortcut = [].concat(subsonicResponse.indexes.shortcut);
formattedResponse.index = [].concat(subsonicResponse.indexes.index); formattedResponse.index = [].concat(subsonicResponse.indexes.index);
_(formattedResponse.index).map(function (index) { _.map(formattedResponse.index, function (index) {
var formattedIndex = index; var formattedIndex = index;
formattedIndex.artist = [].concat(index.artist); formattedIndex.artist = [].concat(index.artist);
return formattedIndex; return formattedIndex;
@ -277,7 +296,7 @@ angular.module('jamstash.subsonic.service', ['angular-underscore/utils',
// Make sure this is an array using concat because Madsonic will return an object when there's only one element // Make sure this is an array using concat because Madsonic will return an object when there's only one element
var children = [].concat(subsonicResponse.directory.child); var children = [].concat(subsonicResponse.directory.child);
if (children.length > 0) { if (children.length > 0) {
var allChildren = _(children).partition(function (item) { var allChildren = _.partition(children, function (item) {
return item.isDir; return item.isDir;
}); });
return { return {
@ -315,7 +334,7 @@ angular.module('jamstash.subsonic.service', ['angular-underscore/utils',
// since all of this is asynchronous, we need to wait for all the requests to finish by using $q.all() // since all of this is asynchronous, we need to wait for all the requests to finish by using $q.all()
var allRequestsFinished = $q.all(promises).then(function (data) { var allRequestsFinished = $q.all(promises).then(function (data) {
// and since $q.all() wraps everything in another array, we use flatten() to end up with only one array of songs // and since $q.all() wraps everything in another array, we use flatten() to end up with only one array of songs
return _(data).flatten(); return _.flatten(data);
}); });
deferred.resolve(allRequestsFinished); deferred.resolve(allRequestsFinished);
} }
@ -419,7 +438,7 @@ angular.module('jamstash.subsonic.service', ['angular-underscore/utils',
var songArray = [].concat(starred.song); var songArray = [].concat(starred.song);
if (songArray.length > 0) { if (songArray.length > 0) {
// Return random subarray of songs // Return random subarray of songs
var songs = [].concat(_(songArray).sample(globals.settings.AutoPlaylistSize)); var songs = [].concat(_.sample(songArray, globals.settings.AutoPlaylistSize));
return map.mapSongs(songs); return map.mapSongs(songs);
} }
} }
@ -437,7 +456,7 @@ angular.module('jamstash.subsonic.service', ['angular-underscore/utils',
// Make sure this is an array using concat because Madsonic will return an object when there's only one element // Make sure this is an array using concat because Madsonic will return an object when there's only one element
var playlistArray = [].concat(subsonicResponse.playlists.playlist); var playlistArray = [].concat(subsonicResponse.playlists.playlist);
if (playlistArray.length > 0) { if (playlistArray.length > 0) {
var allPlaylists = _(playlistArray).partition(function (item) { var allPlaylists = _.partition(playlistArray, function (item) {
return item.owner === globals.settings.Username; return item.owner === globals.settings.Username;
}); });
return {playlists: allPlaylists[0], playlistsPublic: allPlaylists[1]}; return {playlists: allPlaylists[0], playlistsPublic: allPlaylists[1]};
@ -572,7 +591,7 @@ angular.module('jamstash.subsonic.service', ['angular-underscore/utils',
if (channel !== null && channel.id === id) { if (channel !== null && channel.id === id) {
// Make sure this is an array using concat because Madsonic will return an object when there's only one element // Make sure this is an array using concat because Madsonic will return an object when there's only one element
var episodesArray = [].concat(channel.episode); var episodesArray = [].concat(channel.episode);
episodes = _(episodesArray).filter(function (episode) { episodes = _.filter(episodesArray, function (episode) {
return episode.status === "completed"; return episode.status === "completed";
}); });
if(episodes.length > 0) { if(episodes.length > 0) {

View file

@ -4,10 +4,40 @@
* Access and use the Subsonic Server. The Controller is in charge of relaying the Service's messages to the user through the * Access and use the Subsonic Server. The Controller is in charge of relaying the Service's messages to the user through the
* notifications. * notifications.
*/ */
angular.module('jamstash.subsonic.controller', ['angular-underscore/utils', 'jamstash.subsonic.service', 'jamstash.player.service', 'jamstash.persistence']) angular.module('jamstash.subsonic.controller', [
'ngLodash',
'jamstash.subsonic.service',
'jamstash.player.service',
'jamstash.persistence'
])
.controller('SubsonicController', ['$scope', '$rootScope', '$routeParams', '$window', 'utils', 'globals', 'map', 'subsonic', 'notifications', 'player', 'persistence', .controller('SubsonicController', [
function ($scope, $rootScope, $routeParams, $window, utils, globals, map, subsonic, notifications, player, persistence) { '$scope',
'$rootScope',
'$routeParams',
'$window',
'lodash',
'utils',
'globals',
'map',
'subsonic',
'notifications',
'player',
'persistence',
function (
$scope,
$rootScope,
$routeParams,
$window,
_,
utils,
globals,
map,
subsonic,
notifications,
player,
persistence
) {
'use strict'; 'use strict';
$scope.settings = globals.settings; $scope.settings = globals.settings;

View file

@ -2,17 +2,18 @@ describe("Subsonic controller", function() {
'use strict'; 'use strict';
var scope, $rootScope, $controller, $window, var scope, $rootScope, $controller, $window,
subsonic, notifications, player, utils, persistence, controllerParams, deferred; _, subsonic, notifications, player, utils, persistence, controllerParams, deferred;
beforeEach(function() { beforeEach(function() {
jasmine.addCustomEqualityTester(angular.equals); jasmine.addCustomEqualityTester(angular.equals);
module('jamstash.subsonic.controller'); module('jamstash.subsonic.controller');
inject(function (_$controller_, _$rootScope_, globals, map, $q) { inject(function (_$controller_, _$rootScope_, globals, map, $q, lodash) {
$rootScope = _$rootScope_; $rootScope = _$rootScope_;
scope = $rootScope.$new(); scope = $rootScope.$new();
deferred = $q.defer(); deferred = $q.defer();
_ = lodash;
$window = jasmine.createSpyObj("$window", [ $window = jasmine.createSpyObj("$window", [
"prompt", "prompt",
@ -43,11 +44,11 @@ describe("Subsonic controller", function() {
"newPlaylist", "newPlaylist",
"recursiveGetSongs", "recursiveGetSongs",
"savePlaylist", "savePlaylist",
"search", "search"
]); ]);
// We make them return different promises and use our deferred variable only when testing // We make them return different promises and use our deferred variable only when testing
// a particular function, so that they stay isolated // a particular function, so that they stay isolated
_.chain(subsonic).pluck('and').invoke("returnValue", $q.defer().promise); _.forIn(subsonic, _.method('and.returnValue', $q.defer().promise));
subsonic.showIndex = false; subsonic.showIndex = false;
// Mock the player service // Mock the player service
@ -58,7 +59,7 @@ describe("Subsonic controller", function() {
"play", "play",
"playFirstSong" "playFirstSong"
]); ]);
_.chain(player).pluck('and').invoke("returnValue", player); _.forIn(player, _.method('and.returnValue', player));
player.queue = []; player.queue = [];
$controller = _$controller_; $controller = _$controller_;

View file

@ -26,11 +26,11 @@
}, },
"main": "app/index.html", "main": "app/index.html",
"dependencies": { "dependencies": {
"angular": "~1.3.15", "angular": "~1.3.16",
"angular-route": "~1.3.15", "angular-route": "~1.3.16",
"angular-sanitize": "~1.3.15", "angular-sanitize": "~1.3.16",
"angular-cookies": "~1.3.15", "angular-cookies": "~1.3.16",
"angular-resource": "~1.3.15", "angular-resource": "~1.3.16",
"jquery": "~2.0.0", "jquery": "~2.0.0",
"jquery-ui": "~1.10.0", "jquery-ui": "~1.10.0",
"jplayer": "~2.9.0", "jplayer": "~2.9.0",
@ -38,11 +38,10 @@
"notify.js": "<=1.2.2", "notify.js": "<=1.2.2",
"jquery.scrollTo": "~1.4.5", "jquery.scrollTo": "~1.4.5",
"jquery-dateFormat": "~1.0.2", "jquery-dateFormat": "~1.0.2",
"underscore": "~1.8.3",
"angular-underscore": "~0.5.0",
"angular-locker": "~1.2.0", "angular-locker": "~1.2.0",
"angular-ui-utils": "bower-keypress", "angular-ui-utils": "bower-keypress",
"open-iconic": "~1.1.1" "open-iconic": "~1.1.1",
"ng-lodash": "~0.2.3"
}, },
"overrides": { "overrides": {
"fancybox": { "fancybox": {
@ -53,7 +52,7 @@
} }
}, },
"devDependencies": { "devDependencies": {
"angular-mocks": "~1.3.15", "angular-mocks": "~1.3.16",
"jasmine-promise-matchers": "~1.1.1", "jasmine-promise-matchers": "~1.1.1",
"jasmine-fixture": "~1.2.2" "jasmine-fixture": "~1.2.2"
}, },

View file

@ -31,10 +31,9 @@ module.exports = function (config) {
'bower_components/notify.js/notify.js', 'bower_components/notify.js/notify.js',
'bower_components/jquery.scrollTo/jquery.scrollTo.js', 'bower_components/jquery.scrollTo/jquery.scrollTo.js',
'bower_components/jquery-dateFormat/dist/jquery-dateFormat.js', 'bower_components/jquery-dateFormat/dist/jquery-dateFormat.js',
'bower_components/underscore/underscore.js',
'bower_components/angular-underscore/angular-underscore.js',
'bower_components/angular-locker/dist/angular-locker.min.js', 'bower_components/angular-locker/dist/angular-locker.min.js',
'bower_components/angular-ui-utils/keypress.js', 'bower_components/angular-ui-utils/keypress.js',
'bower_components/ng-lodash/build/ng-lodash.js',
'bower_components/angular-mocks/angular-mocks.js', 'bower_components/angular-mocks/angular-mocks.js',
'bower_components/jasmine-promise-matchers/dist/jasmine-promise-matchers.js', 'bower_components/jasmine-promise-matchers/dist/jasmine-promise-matchers.js',
'bower_components/jasmine-fixture/dist/jasmine-fixture.js', 'bower_components/jasmine-fixture/dist/jasmine-fixture.js',