Commit f595ae6b authored by Randall Leeds's avatar Randall Leeds

Replace documentHelpers with baseURI + URL API

Rather than have a service, ``documentHelpers``, that exposes a value,
``baseURI``, and a function, ``absoluteURI``, decorate ``$document``
so that it's guaranteed to have a ``baseURI`` property and use the
previously included ``URL`` constructor as a standard way to get parts
of a URL.
parent f153ff0d
...@@ -63,10 +63,16 @@ configureTemplates = ['$sceDelegateProvider', ($sceDelegateProvider) -> ...@@ -63,10 +63,16 @@ configureTemplates = ['$sceDelegateProvider', ($sceDelegateProvider) ->
] ]
configure = ['$injector', ($injector) -> configure = ['$injector', '$provide', ($injector, $provide) ->
$injector.invoke(configureLocation) $injector.invoke(configureLocation)
$injector.invoke(configureRoutes) $injector.invoke(configureRoutes)
$injector.invoke(configureTemplates) $injector.invoke(configureTemplates)
$provide.decorator '$document', ($delegate) ->
baseURI = $delegate.prop('baseURI')
baseURI ?= $delegate.find('base').prop('href') # fallback
baseURI ?= $delegate.prop('URL') # fallback
$delegate.prop('baseURI', baseURI)
] ]
angular.module('h', imports, configure) angular.module('h', imports, configure)
...@@ -10,10 +10,10 @@ ...@@ -10,10 +10,10 @@
### ###
class Auth class Auth
this.$inject = ['$http', '$location', '$rootScope', this.$inject = ['$document', '$http', '$location', '$rootScope',
'annotator', 'documentHelpers', 'identity'] 'annotator', 'identity']
constructor: ( $http, $location, $rootScope, constructor: ( $document, $http, $location, $rootScope,
annotator, documentHelpers, identity) -> annotator, identity) ->
{plugins} = annotator {plugins} = annotator
_checkingToken = false _checkingToken = false
@user = undefined @user = undefined
...@@ -25,10 +25,11 @@ class Auth ...@@ -25,10 +25,11 @@ class Auth
onlogin = (assertion) => onlogin = (assertion) =>
_checkingToken = true _checkingToken = true
base = $document.prop('baseURI')
tokenUrl = new URL("/api/token?assertion=#{assertion}", base).href
# Configure the Auth plugin with the issued assertion as refresh token. # Configure the Auth plugin with the issued assertion as refresh token.
annotator.addPlugin 'Auth', annotator.addPlugin 'Auth', tokenUrl: tokenUrl
tokenUrl: documentHelpers.absoluteURI(
"/api/token?assertion=#{assertion}")
# Set the user from the token. # Set the user from the token.
plugins.Auth.withToken (token) => plugins.Auth.withToken (token) =>
......
class AppController class AppController
this.$inject = [ this.$inject = [
'$location', '$route', '$scope', '$window', '$document', '$location', '$route', '$scope', '$window',
'annotator', 'auth', 'documentHelpers', 'drafts', 'identity', 'annotator', 'auth', 'drafts', 'identity',
'permissions', 'streamer', 'streamfilter' 'permissions', 'streamer', 'streamfilter'
] ]
constructor: ( constructor: (
$location, $route, $scope, $window, $document, $location, $route, $scope, $window,
annotator, auth, documentHelpers, drafts, identity, annotator, auth, drafts, identity,
permissions, streamer, streamfilter, permissions, streamer, streamfilter,
) -> ) ->
...@@ -14,7 +14,10 @@ class AppController ...@@ -14,7 +14,10 @@ class AppController
$scope.auth = auth $scope.auth = auth
isFirstRun = $location.search().hasOwnProperty('firstrun') isFirstRun = $location.search().hasOwnProperty('firstrun')
streamerUrl = documentHelpers.baseURI.replace(/^http/, 'ws') + 'ws'
streamerUrl = new URL('/ws', $document.prop('baseURI'))
streamerUrl.protocol = streamerUrl.protocol.replace('http', 'ws')
streamerUrl = streamerUrl.href
applyUpdates = (action, data) -> applyUpdates = (action, data) ->
# Update the application with new data from the websocket. # Update the application with new data from the websocket.
......
### global -extractURIComponent, -validate ### ### global -validate ###
# Use an anchor tag to extract specific components within a uri.
extractURIComponent = (uri, component) ->
unless extractURIComponent.a
extractURIComponent.a = document.createElement('a')
extractURIComponent.a.href = uri
extractURIComponent.a[component]
# Validate an annotation. # Validate an annotation.
# Annotations must be attributed to a user or marked as deleted. # Annotations must be attributed to a user or marked as deleted.
...@@ -37,12 +29,10 @@ validate = (value) -> ...@@ -37,12 +29,10 @@ validate = (value) ->
# {@link annotator annotator service} for persistence. # {@link annotator annotator service} for persistence.
### ###
AnnotationController = [ AnnotationController = [
'$scope', '$timeout', '$document', '$scope', '$timeout',
'annotator', 'auth', 'drafts', 'flash', 'documentHelpers', 'permissions', 'annotator', 'auth', 'drafts', 'flash', 'permissions', 'timeHelpers'
'timeHelpers' ($document, $scope, $timeout,
($scope, $timeout, annotator, auth, drafts, flash, permissions, timeHelpers
annotator, auth, drafts, flash, documentHelpers, permissions,
timeHelpers
) -> ) ->
@annotation = {} @annotation = {}
@action = 'view' @action = 'view'
...@@ -202,7 +192,7 @@ AnnotationController = [ ...@@ -202,7 +192,7 @@ AnnotationController = [
angular.extend @annotation, angular.copy model angular.extend @annotation, angular.copy model
# Set the URI # Set the URI
@annotationURI = documentHelpers.absoluteURI("/a/#{@annotation.id}") @annotationURI = new URL("/a/#{@annotation.id}", this.baseURI).href
# Extract the document metadata. # Extract the document metadata.
if model.document if model.document
...@@ -213,7 +203,7 @@ AnnotationController = [ ...@@ -213,7 +203,7 @@ AnnotationController = [
uri = link.href uri = link.href
break break
domain = extractURIComponent(uri, 'hostname') domain = new URL(uri).hostname
documentTitle = if Array.isArray(model.document.title) documentTitle = if Array.isArray(model.document.title)
model.document.title[0] model.document.title[0]
else else
...@@ -255,7 +245,7 @@ AnnotationController = [ ...@@ -255,7 +245,7 @@ AnnotationController = [
, nextUpdate, false , nextUpdate, false
# Export the baseURI for the share link # Export the baseURI for the share link
this.baseURI = documentHelpers.baseURI this.baseURI = $document.prop('baseURI')
# Discard the draft if the scope goes away. # Discard the draft if the scope goes away.
$scope.$on '$destroy', -> $scope.$on '$destroy', ->
......
createDocumentHelpers = [
'$document'
($document) ->
baseURI: do ->
baseURI = $document.prop('baseURI')
# XXX: IE workaround for the lack of document.baseURI property
unless baseURI
baseURI = $document.find('base').prop('href') or $document.prop('URL')
# Strip an empty hash and end in exactly one slash
baseURI.replace(/#$/, '').replace(/\/+$/, '/')
absoluteURI: (path) ->
"#{@baseURI}#{path.replace(/^\//, '')}"
]
angular.module('h.helpers')
.factory('documentHelpers', createDocumentHelpers)
...@@ -40,8 +40,8 @@ class SessionProvider ...@@ -40,8 +40,8 @@ class SessionProvider
# }); # });
### ###
$get: [ $get: [
'$http', '$q', '$resource', 'documentHelpers', 'flash', 'xsrf', '$document', '$http', '$q', '$resource', 'flash', 'xsrf',
($http, $q, $resource, documentHelpers, flash, xsrf) -> ($document, $http, $q, $resource, flash, xsrf) ->
actions = {} actions = {}
provider = this provider = this
...@@ -72,7 +72,8 @@ class SessionProvider ...@@ -72,7 +72,8 @@ class SessionProvider
actions[name].transformRequest = prepare actions[name].transformRequest = prepare
actions[name].transformResponse = process actions[name].transformResponse = process
endpoint = documentHelpers.absoluteURI('/app') base = $document.prop('baseURI')
endpoint = new URL('/app', base).href
$resource(endpoint, {}, actions) $resource(endpoint, {}, actions)
] ]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment