Pony.fm/resources/assets/scripts/app/app.coffee

417 lines
13 KiB
CoffeeScript
Raw Normal View History

# Pony.fm - A community for pony fan music.
# Copyright (C) 2015 Peter Deltchev
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Some notes on what's going on here:
#
# - Webpack resolves all of these require statements.
#
# - jQuery is loaded before Angular so it replaces jqLite.
#
# - "script!" is used with dependencies that expect to interact with the global state.
#
# - The "ponyfm" module in this file must be initialized before the controllers
# and other Angular modules are brought in; they expect the "ponyfm" module to exist.
require 'script!../base/jquery-2.0.2'
angular = require 'angular'
require 'angular-ui-router'
require 'angular-material'
2016-08-08 18:11:20 +02:00
require 'ng-sortable'
require '../base/angular-ui-sortable'
require '../base/angularytics'
require '../base/jquery.cookie'
require '../base/jquery.timeago'
2016-08-08 18:11:20 +02:00
require 'script!../base/modernizr'
require 'script!../base/marked'
require 'script!../base/moment'
require '../base/soundmanager2-nodebug'
2016-07-17 02:22:59 +02:00
require 'color-thief';
2016-05-21 22:34:12 +02:00
require 'angular-strap'
2016-05-21 23:15:37 +02:00
# Just ignore this, blame webpack
require '../../../../node_modules/angular-strap/dist/angular-strap.tpl'
2016-05-04 18:07:14 +02:00
require 'chart.js';
require 'angular-chart.js';
require '../shared/pfm-angular-marked'
require '../shared/pfm-angular-sanitize'
require '../shared/init.coffee'
2016-08-08 18:11:20 +02:00
ponyfm = angular.module 'ponyfm', ['ngMaterial', 'mgcrea.ngStrap', 'as.sortable', 'ui.router', 'ui.sortable', 'angularytics', 'ngSanitize', 'hc.marked', 'chart.js']
2015-08-31 17:42:21 +02:00
window.pfm.preloaders = {}
# Inspired by: https://stackoverflow.com/a/30652110/3225811
requireDirectory = (r) ->
r.keys().forEach(r)
requireDirectory(require.context('./controllers/', false, /\.coffee$/));
requireDirectory(require.context('./directives/', false, /\.coffee$/));
requireDirectory(require.context('./filters/', false, /\.coffee$/));
requireDirectory(require.context('./services/', false, /\.coffee$/));
2015-08-31 17:42:21 +02:00
if window.pfm.environment == 'production'
ponyfm.run [
2015-10-25 03:35:37 +01:00
'Angularytics',
(analytics) ->
analytics.init()
]
2015-08-31 17:42:21 +02:00
ponyfm.run [
'$rootScope', 'meta',
($rootScope, meta) ->
$rootScope.$on '$stateChangeStart', (event, toState, toParams, fromState, fromParams) ->
meta.reset()
]
ponyfm.config [
'$locationProvider', '$stateProvider', 'AngularyticsProvider', '$httpProvider', '$sceDelegateProvider', 'markedProvider', '$mdDateLocaleProvider', '$mdThemingProvider'
(location, state, analytics, $httpProvider, $sceDelegateProvider, markedProvider, $mdDateLocaleProvider, $mdThemingProvider) ->
2015-10-25 03:35:37 +01:00
if window.pfm.environment == 'local'
$httpProvider.interceptors.push [
->
request: (config) ->
return config if !(/^\/?templates\//.test config.url)
config.url += '?' + Math.ceil(Math.random() * 1000000)
return config
]
2015-10-25 03:35:37 +01:00
# This fixes resource loading on IE
$sceDelegateProvider.resourceUrlWhitelist [
'self',
'/templates/directives/*',
# Used for the "Tweet" button on content item pages
'https://platform.twitter.com/widgets/tweet_button.html**'
2015-10-25 03:35:37 +01:00
]
if window.pfm.environment == 'production'
analytics.setEventHandlers ['Google']
markedProvider.setOptions
gfm: true
2015-12-27 18:04:17 +01:00
tables: false
sanitize: true
smartLists: true
smartypants: true
2015-12-27 19:15:22 +01:00
breaks: true
2015-12-27 18:04:17 +01:00
markedProvider.setRenderer
link: (href, title, text) ->
'<a href="' + href + '" target="_blank" rel="nofollow">' + href + '</a>'
2015-12-27 18:04:17 +01:00
heading: (text, level) ->
text
image: (url) ->
url
codespan: (code) ->
code
code: (code, language) ->
code
del: (text) ->
text
$mdDateLocaleProvider.formatDate = (date) ->
return moment(date).format('DD/MM/YYYY')
$mdDateLocaleProvider.parseDate = (string) ->
return moment(date).format('DD/MM/YYYY')
pfmPurple = $mdThemingProvider.extendPalette('purple', {
'50': '#f5eff6',
'100': '#d6bfd9',
'200': '#c09cc4',
'300': '#a36faa',
'400': '#965d9d',
'500': '#84528a',
'600': '#724777',
'700': '#5f3b64',
'800': '#4d3050',
'900': '#3b243d',
'A100': '#c09cc4',
'A200': '#965d9d',
'A400': '#724777',
'A700': '#65326b',
'contrastDefaultColor': 'light',
'contrastDarkColors': '50 100 200 300 A100'
})
$mdThemingProvider.definePalette('pfmPurple', pfmPurple)
$mdThemingProvider.theme('default')
.primaryPalette('pfmPurple')
.accentPalette('pfmPurple')
.warnPalette('red')
2015-10-25 03:35:37 +01:00
# Errors
state.state 'errors-404',
url: '/errors/not-found'
templateUrl: '/templates/errors/404.html'
state.state 'errors-500',
url: '/errors/server'
templateUrl: '/templates/errors/500.html'
state.state 'errors-403',
url: '/errors/not-authorized'
templateUrl: '/templates/errors/403.html'
state.state 'errors-400',
url: '/errors/invalid'
templateUrl: '/templates/errors/400.html'
# Tracks
state.state 'content',
abstract: true
template: '<ui-view/>'
2015-10-25 03:35:37 +01:00
state.state 'content.tracks',
templateUrl: '/templates/tracks/index.html'
controller: 'tracks'
url: '/tracks'
abstract: true
state.state 'content.tracks.list',
url: '^/tracks?filter&page'
templateUrl: '/templates/tracks/list.html'
controller: 'tracks-list'
state.state 'content.track',
url: '/tracks/{id:[^\-]+}-{slug}'
templateUrl: '/templates/tracks/frame.html'
2015-10-25 03:35:37 +01:00
controller: 'track'
abstract: true
state.state 'content.track.show',
url: ''
templateUrl: '/templates/tracks/show.html'
controller: 'track-show'
state.state 'content.track.edit',
url: '/edit'
templateUrl: '/templates/tracks/edit.html'
controller: 'track-edit'
2016-05-03 00:25:54 +02:00
state.state 'content.track.stats',
url: '/stats'
templateUrl: '/templates/tracks/stats.html'
controller: 'track-stats'
2015-10-25 03:35:37 +01:00
# Albums
state.state 'content.albums',
url: '/albums'
templateUrl: '/templates/albums/index.html'
controller: 'albums'
abstract: true
state.state 'content.albums.list',
url: '?page'
templateUrl: '/templates/albums/list.html'
controller: 'albums-list'
state.state 'content.album',
url: '/albums/{id:[^\-]+}-{slug}'
templateUrl: '/templates/albums/show.html'
controller: 'album'
# Playlists
state.state 'content.playlists',
url: '/playlists'
templateUrl: '/templates/playlists/index.html'
controller: 'playlists'
abstract: true
state.state 'content.playlists.list',
url: '^/playlists?filter&page'
2015-10-25 03:35:37 +01:00
controller: 'playlists-list'
templateUrl: '/templates/playlists/list.html'
state.state 'content.playlist',
url: '/playlist/{id:[^\-]+}-{slug}'
templateUrl: '/templates/playlists/show.html'
controller: 'playlist'
# Artists
state.state 'content.artists',
url: '/artists'
templateUrl: '/templates/artists/index.html'
controller: 'artists'
abstract: true
state.state 'content.artists.list',
url: '?page'
templateUrl: '/templates/artists/list.html'
controller: 'artists-list'
# Pages
state.state 'pages',
templateUrl: '/templates/pages/_layout.html'
2015-10-25 03:35:37 +01:00
state.state 'pages.about',
2015-10-25 03:35:37 +01:00
url: '/about'
templateUrl: '/templates/pages/about.html'
state.state 'pages.faq',
url: '/faq'
templateUrl: '/templates/pages/faq.html'
state.state 'pages.mlpforums-advertising-program',
url: '/mlpforums-advertising-program'
templateUrl: '/templates/pages/mlpforums-advertising-program.html'
2015-10-25 03:35:37 +01:00
# Auth
state.state 'login',
url: '/login'
templateUrl: '/templates/auth/login.html'
controller: 'login'
state.state 'register',
url: '/register'
templateUrl: '/templates/auth/register.html'
# Admin
state.state 'admin',
abstract: true
url: '/admin'
templateUrl: '/templates/admin/_layout.html'
state.state 'admin.genres',
url: '/genres'
controller: 'admin-genres'
templateUrl: '/templates/admin/genres.html'
2016-06-06 01:12:04 +02:00
state.state 'admin.showsongs',
url: '/show-songs'
controller: 'admin-show-songs'
templateUrl: '/templates/admin/show-songs.html'
2016-05-11 18:41:49 +02:00
state.state 'admin.tracks',
url: '/tracks'
controller: 'admin-tracks'
templateUrl: '/templates/admin/tracks.html'
state.state 'admin.users',
url: '/users'
controller: 'admin-users'
templateUrl: '/templates/admin/users.html'
# Homepage
2015-10-25 03:35:37 +01:00
if window.pfm.auth.isLogged
state.state 'home',
url: '/'
templateUrl: '/templates/dashboard/index.html'
controller: 'dashboard'
state.state 'notifications',
url: '/notifications'
templateUrl: '/templates/notifications/index.html'
controller: 'notifications'
2015-10-25 03:35:37 +01:00
else
state.state 'home',
url: '/'
templateUrl: '/templates/home/index.html'
controller: 'home'
# Final catch-all for artists
2015-10-25 03:35:37 +01:00
state.state 'content.artist',
url: '^/{slug}'
templateUrl: '/templates/artists/_show_layout.html'
abstract: true
controller: 'artist'
state.state 'content.artist.profile',
2016-05-16 16:00:28 +02:00
url: '^/{slug}'
2015-10-25 03:35:37 +01:00
templateUrl: '/templates/artists/profile.html'
controller: 'artist-profile'
state.state 'content.artist.content',
url: '/content'
templateUrl: '/templates/artists/content.html'
controller: 'artist-content'
state.state 'content.artist.favourites',
url: '/favourites'
templateUrl: '/templates/artists/favourites.html'
controller: 'artist-favourites'
# Account
state.state 'content.artist.account',
url: '/account'
abstract: true
templateUrl: '/templates/account/_layout.html'
# Upload
state.state 'content.artist.account.uploader',
url: '/uploader'
templateUrl: '/templates/uploader/index.html'
controller: 'uploader'
state.state 'content.artist.account.settings',
url: ''
templateUrl: '/templates/account/settings.html'
controller: 'account-settings'
state.state 'content.artist.account.tracks',
url: '/tracks'
templateUrl: '/templates/account/tracks.html'
controller: 'account-tracks'
state.state 'content.artist.account.tracks.edit',
url: '/edit/:track_id'
templateUrl: '/templates/account/track.html'
controller: 'account-track'
state.state 'content.artist.account.albums',
url: '/albums'
templateUrl: '/templates/account/albums.html'
controller: 'account-albums'
state.state 'content.artist.account.albums.create',
url: '/create'
templateUrl: '/templates/account/album.html'
controller: 'account-albums-edit'
state.state 'content.artist.account.albums.edit',
url: '/edit/:album_id'
templateUrl: '/templates/account/album.html'
controller: 'account-albums-edit'
state.state 'content.artist.account.playlists',
url: '/playlists'
templateUrl: '/templates/account/playlists.html'
controller: 'account-playlists'
2015-10-25 03:35:37 +01:00
location.html5Mode(true);
]
module.exports = ponyfm