From e109e116f537be732703fbed7580fecc2d984e6c Mon Sep 17 00:00:00 2001 From: Alexandre Tuleu Date: Thu, 11 Feb 2016 17:04:15 +0100 Subject: [PATCH] Reorganizes the project with clean MVC structure --- .bowerrc | 2 +- gulpfile.js | 16 ++++ package.json | 2 + router.go | 13 ++- webapp/directives.js | 20 ++++ webapp/html/index.html | 56 +++++++++++ webapp/index.html | 96 ------------------- webapp/js/controllers.js | 17 ++++ webapp/{html/.gitkeep => js/route.js} | 0 webapp/js/satbd.satellite.bar.js | 129 ++------------------------ webapp/js/views/authors/authors.html | 1 + webapp/js/views/authors/authors.js | 9 ++ webapp/js/views/recents/recents.html | 1 + webapp/js/views/recents/recents.js | 20 ++++ webapp/js/views/search/search.html | 2 + webapp/js/views/search/search.js | 12 +++ webapp/js/views/series/series.html | 1 + webapp/js/views/series/series.js | 9 ++ 18 files changed, 187 insertions(+), 219 deletions(-) create mode 100644 webapp/directives.js create mode 100644 webapp/html/index.html delete mode 100644 webapp/index.html create mode 100644 webapp/js/controllers.js rename webapp/{html/.gitkeep => js/route.js} (100%) create mode 100644 webapp/js/views/authors/authors.html create mode 100644 webapp/js/views/authors/authors.js create mode 100644 webapp/js/views/recents/recents.html create mode 100644 webapp/js/views/recents/recents.js create mode 100644 webapp/js/views/search/search.html create mode 100644 webapp/js/views/search/search.js create mode 100644 webapp/js/views/series/series.html create mode 100644 webapp/js/views/series/series.js diff --git a/.bowerrc b/.bowerrc index d058d74..60af0e0 100644 --- a/.bowerrc +++ b/.bowerrc @@ -1,3 +1,3 @@ { - "directory": "static/bower_components" + "directory": "webapp/bower_components" } \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index c875899..209476a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -18,3 +18,19 @@ gulp.task('test', function() { gulp.task('autotest', function() { return gulp.watch(['webapp/js/**/*.js','test/specs/*.js'], ['test']); }); + + +gulp.task('build', function() { + return gulp.src('./webapp/html/index.html') + .pipe(plugins.inject( + gulp.src(['./webapp/js/**/*.js']).pipe(plugins.angularFilesort()), + { + ignorePath: '/webapp' + } + )) + .pipe(gulp.dest('./webapp')); +}); + +gulp.task('autobuild', function() { + return gulp.watch(['webapp/js/**/*.js','webapp/html/index.html'], ['build']); +}); diff --git a/package.json b/package.json index 9ebb399..1f531b5 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,8 @@ "devDependencies": { "bower": "^1.7.7", "gulp": "^3.9.1", + "gulp-angular-filesort": "^1.1.1", + "gulp-inject": "^3.0.0", "gulp-jasmine": "^2.2.1", "gulp-karma": "0.0.5", "gulp-load-plugins": "^1.2.0", diff --git a/router.go b/router.go index fc0bc12..4889142 100644 --- a/router.go +++ b/router.go @@ -119,11 +119,14 @@ func (a *appData) buildRouter() http.Handler { return } - io.Copy(w, rc) + _, err = io.Copy(w, rc) + if err != nil { + panic(fmt.Sprintf("internal error: %s", err)) + } }))) - dirs := []string{"css", "js", "img", "fonts", "html", "bower_components"} + dirs := []string{"css", "js", "img", "bower_components"} for _, d := range dirs { router.ServeFiles(path.Join("/", d, "/*filepath"), http.Dir(filepath.Join("webapp", d))) } @@ -137,7 +140,11 @@ func (a *appData) buildRouter() http.Handler { } defer closeOrPanic(f, filepath.Join("webapp", "index.html")) - io.Copy(w, f) + _, err = io.Copy(w, f) + if err != nil { + panic(fmt.Sprintf("internal error: %s", err)) + } + }))) return router diff --git a/webapp/directives.js b/webapp/directives.js new file mode 100644 index 0000000..2177b92 --- /dev/null +++ b/webapp/directives.js @@ -0,0 +1,20 @@ +var directives = angular.module('satbd.satellite.bar.directives') + +directives.directive('album', function() { + return { + scope: { + id: '@album-id' + } + templateUrl: 'js/directives/album.html', + restrict: 'E', + }; +}); + +directives.controller('albumCtrl', function($scope,$html) { + $scope.init = function() { + $http.get('/api/albums/'+ $scope.id) + .success(function(data) { + $scope.album = data; + }); + }; +}); diff --git a/webapp/html/index.html b/webapp/html/index.html new file mode 100644 index 0000000..ebc5979 --- /dev/null +++ b/webapp/html/index.html @@ -0,0 +1,56 @@ + + + + + + + + + + satbd: explorez la betheque de sat + + + + + +
+
+ + + + + + + + + diff --git a/webapp/index.html b/webapp/index.html deleted file mode 100644 index 521aa99..0000000 --- a/webapp/index.html +++ /dev/null @@ -1,96 +0,0 @@ - - - - - - - - - - satbd: explorez la betheque de sat - - - - - -
- - - -
- - - -
-
- -
-
- - - - - - - - - diff --git a/webapp/js/controllers.js b/webapp/js/controllers.js new file mode 100644 index 0000000..0bdb536 --- /dev/null +++ b/webapp/js/controllers.js @@ -0,0 +1,17 @@ +'use strict'; + +var controllers = angular.module('satbd.satellite.bar.controllers',[ + 'ui.bootstrap', + 'ngAnimate' +]); + +controllers.controller('NavBarCtrl',function($scope,$location) { + $scope.isCollapsed = true; + $scope.isActive = function(loc) { + return loc === $location.path(); + }; + + $scope.search = function(searchTerms) { + $location.url('/search?q='+ encodeURIComponent(searchTerms)); + } +}); diff --git a/webapp/html/.gitkeep b/webapp/js/route.js similarity index 100% rename from webapp/html/.gitkeep rename to webapp/js/route.js diff --git a/webapp/js/satbd.satellite.bar.js b/webapp/js/satbd.satellite.bar.js index bd65101..ffe85dd 100644 --- a/webapp/js/satbd.satellite.bar.js +++ b/webapp/js/satbd.satellite.bar.js @@ -1,121 +1,12 @@ -angular.module('satbd.satellite.bar', ['ui.bootstrap','ngAnimate','ngSanitize']); - -angular.module('satbd.satellite.bar').controller('GlobalCtrl', function($scope,$log) { - $scope.location = ''; - $scope.isActive = function(location) { - return $scope.location === location - }; - - $scope.recents = function() { - $scope.location='recents'; - $scope.$broadcast('displayRecents') - }; - - $scope.collections = function() { - $scope.location='collections'; - $scope.$broadcast('displayCollections') - }; - - $scope.authors = function() { - $scope.location='authors'; - $scope.$broadcast('displayAuthors') - }; - - $scope.$on('onSearchQuery', function(event,query) { - if (query.length > 0) { - $scope.location='search'; - $scope.$broadcast('displaySearch', query) - } - }); - - $scope.$on('recentsReady', function(event) { - $scope.recents(); - }); +'use strict'; -}); - -angular.module('satbd.satellite.bar').controller('NavbarCollapseCtrl', function ($scope, $uibModal) { - $scope.isCollapsed = true; - $scope.openModal = function (size) { - var modalInstance = $uibModal.open({ - templateUrl: 'help.html', - controller: 'HelpInstanceCtrl', - size: size, - keyboard: true, - }); - }; - - $scope.searchQuery = ''; - $scope.search = function() { - $scope.$emit('onSearchQuery',$scope.searchQuery); - }; - -}); - -angular.module('satbd.satellite.bar').controller('HelpInstanceCtrl', function($scope, $uibModalInstance) { - $scope.ok = function () { - $uibModalInstance.close(''); - }; -}); - -angular.module('satbd.satellite.bar').controller('RecentCtrl', function($scope, $http,$log) { - $scope.albumIDs = [ ]; - $scope.clear = function(event) { $scope.albumIDs = [] }; - $scope.$on('displayAuthors', $scope.clear); - $scope.$on('displayCollections', $scope.clear); - $scope.$on('displaySearch', $scope.clear); - $scope.$on('displayRecents', function(event) { - $scope.clear(); - $log.info('fetching recent albums'); - $http.get('/api/recents').success(function(data){ - for (var i = 0; i < 10; i++) { - $scope.albumIDs.push(data[i]); - } - }); - }); - - $scope.$emit('recentsReady') -}); - - -angular.module('satbd.satellite.bar').controller('RecentAlbumCtrl', function($scope, $http) { - $scope.init_by_id = function(id) { - $http.get('api/albums/'+ id).success(function (data) { - $scope.album = data; - }); - }; -}); - - -angular.module('satbd.satellite.bar').controller('SearchCtrl', function($scope, $http, $log) { - $scope.showResults = false - $scope.searchResults = null; - $scope.searchResultsJSON = ''; - - $scope.clear = function () { - $scope.showResults = false; - $scope.searchResults = null; - $scope.searchResultsJSON = ''; - }; - $scope.$on('displayAuthors', $scope.clear); - $scope.$on('displayCollections', $scope.clear); - $scope.$on('displayRecents', $scope.clear); - $scope.$on('displaySearch', function(event, query) { - $log.info("Submitting query " + query); - $http.post('/api/search', { - "size": 20, - "explain": false, - "highlight":{}, - "fields": ["série", "collection","titre","éditeur","ref","description","Note"], - "query": { - "query": query, - } - }).success(function(data) { - $scope.searchResults = data; - $scope.showResults = true; - }).error(function(data, code) { - $log.error("got " + code + " data: " + data); - $scope.showResults = true; - }); - }); +angular.module('satbd.satellite.bar',[ + 'ngRoute', + 'satbd.satellite.bar.controllers', + 'satbd.satellite.bar.views.recents', + 'satbd.satellite.bar.views.series', + 'satbd.satellite.bar.views.authors', + 'satbd.satellite.bar.views.search' +]).config(function($routeProvider) { + $routeProvider.otherwise({redirectTo: '/recents'}); }); diff --git a/webapp/js/views/authors/authors.html b/webapp/js/views/authors/authors.html new file mode 100644 index 0000000..ab09c78 --- /dev/null +++ b/webapp/js/views/authors/authors.html @@ -0,0 +1 @@ +

Par autheurs

diff --git a/webapp/js/views/authors/authors.js b/webapp/js/views/authors/authors.js new file mode 100644 index 0000000..ddcc7bf --- /dev/null +++ b/webapp/js/views/authors/authors.js @@ -0,0 +1,9 @@ +'use strict'; + +angular.module('satbd.satellite.bar.views.authors',[ + 'ngRoute' +]).config(function($routeProvider) { + $routeProvider.when('/authors', { + templateUrl: 'js/views/authors/authors.html', + }); +}) diff --git a/webapp/js/views/recents/recents.html b/webapp/js/views/recents/recents.html new file mode 100644 index 0000000..733ee80 --- /dev/null +++ b/webapp/js/views/recents/recents.html @@ -0,0 +1 @@ +

Albums Récents

diff --git a/webapp/js/views/recents/recents.js b/webapp/js/views/recents/recents.js new file mode 100644 index 0000000..027d83c --- /dev/null +++ b/webapp/js/views/recents/recents.js @@ -0,0 +1,20 @@ +'use strict'; + +angular.module('satbd.satellite.bar.views.recents',[ + 'ngRoute' +]).config(function($routeProvider) { + $routeProvider.when('/recents', { + templateUrl: 'js/views/recents/recents.html', + controller: 'RecentsCtrl' + }); +}).controller('RecentsCtrl', function( $scope, $http, $log) { + $scope.albumIDs = []; + $http.get('/api/recents') + .success(function(data) { + for (var i=0; i < 10; i++) { + $scope.albumIDs.push(data[i]); + } + }).error(function(err){ + $log.error(err); + }); +}); diff --git a/webapp/js/views/search/search.html b/webapp/js/views/search/search.html new file mode 100644 index 0000000..6ed0fc3 --- /dev/null +++ b/webapp/js/views/search/search.html @@ -0,0 +1,2 @@ +

Votre recherche de '{{ terms }}' n'a pas abouti

+

C'est toujours pas implémenté, couillon

diff --git a/webapp/js/views/search/search.js b/webapp/js/views/search/search.js new file mode 100644 index 0000000..85da9fc --- /dev/null +++ b/webapp/js/views/search/search.js @@ -0,0 +1,12 @@ +'use strict'; + +angular.module('satbd.satellite.bar.views.search',[ + 'ngRoute' +]).config(function($routeProvider) { + $routeProvider.when('/search', { + templateUrl: 'js/views/search/search.html', + controller: 'SearchCtrl' + }); +}).controller('SearchCtrl', function($scope,$routeParams) { + $scope.terms=$routeParams.q; +}); diff --git a/webapp/js/views/series/series.html b/webapp/js/views/series/series.html new file mode 100644 index 0000000..d239c56 --- /dev/null +++ b/webapp/js/views/series/series.html @@ -0,0 +1 @@ +

Par Séries

diff --git a/webapp/js/views/series/series.js b/webapp/js/views/series/series.js new file mode 100644 index 0000000..b98bee4 --- /dev/null +++ b/webapp/js/views/series/series.js @@ -0,0 +1,9 @@ +'use strict'; + +angular.module('satbd.satellite.bar.views.series',[ + 'ngRoute' +]).config(function($routeProvider) { + $routeProvider.when('/series', { + templateUrl: 'js/views/series/series.html' + }); +});