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) ->
]
configure = ['$injector', ($injector) ->
configure = ['$injector', '$provide', ($injector, $provide) ->
$injector.invoke(configureLocation)
$injector.invoke(configureRoutes)
$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)
......@@ -10,10 +10,10 @@
###
class Auth
this.$inject = ['$http', '$location', '$rootScope',
'annotator', 'documentHelpers', 'identity']
constructor: ( $http, $location, $rootScope,
annotator, documentHelpers, identity) ->
this.$inject = ['$document', '$http', '$location', '$rootScope',
'annotator', 'identity']
constructor: ( $document, $http, $location, $rootScope,
annotator, identity) ->
{plugins} = annotator
_checkingToken = false
@user = undefined
......@@ -25,10 +25,11 @@ class Auth
onlogin = (assertion) =>
_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.
annotator.addPlugin 'Auth',
tokenUrl: documentHelpers.absoluteURI(
"/api/token?assertion=#{assertion}")
annotator.addPlugin 'Auth', tokenUrl: tokenUrl
# Set the user from the token.
plugins.Auth.withToken (token) =>
......
class AppController
this.$inject = [
'$location', '$route', '$scope', '$window',
'annotator', 'auth', 'documentHelpers', 'drafts', 'identity',
'$document', '$location', '$route', '$scope', '$window',
'annotator', 'auth', 'drafts', 'identity',
'permissions', 'streamer', 'streamfilter'
]
constructor: (
$location, $route, $scope, $window,
annotator, auth, documentHelpers, drafts, identity,
$document, $location, $route, $scope, $window,
annotator, auth, drafts, identity,
permissions, streamer, streamfilter,
) ->
......@@ -14,7 +14,10 @@ class AppController
$scope.auth = auth
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) ->
# Update the application with new data from the websocket.
......
### global -extractURIComponent, -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]
### global -validate ###
# Validate an annotation.
# Annotations must be attributed to a user or marked as deleted.
......@@ -37,12 +29,10 @@ validate = (value) ->
# {@link annotator annotator service} for persistence.
###
AnnotationController = [
'$scope', '$timeout',
'annotator', 'auth', 'drafts', 'flash', 'documentHelpers', 'permissions',
'timeHelpers'
($scope, $timeout,
annotator, auth, drafts, flash, documentHelpers, permissions,
timeHelpers
'$document', '$scope', '$timeout',
'annotator', 'auth', 'drafts', 'flash', 'permissions', 'timeHelpers'
($document, $scope, $timeout,
annotator, auth, drafts, flash, permissions, timeHelpers
) ->
@annotation = {}
@action = 'view'
......@@ -202,7 +192,7 @@ AnnotationController = [
angular.extend @annotation, angular.copy model
# Set the URI
@annotationURI = documentHelpers.absoluteURI("/a/#{@annotation.id}")
@annotationURI = new URL("/a/#{@annotation.id}", this.baseURI).href
# Extract the document metadata.
if model.document
......@@ -213,7 +203,7 @@ AnnotationController = [
uri = link.href
break
domain = extractURIComponent(uri, 'hostname')
domain = new URL(uri).hostname
documentTitle = if Array.isArray(model.document.title)
model.document.title[0]
else
......@@ -255,7 +245,7 @@ AnnotationController = [
, nextUpdate, false
# Export the baseURI for the share link
this.baseURI = documentHelpers.baseURI
this.baseURI = $document.prop('baseURI')
# Discard the draft if the scope goes away.
$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
# });
###
$get: [
'$http', '$q', '$resource', 'documentHelpers', 'flash', 'xsrf',
($http, $q, $resource, documentHelpers, flash, xsrf) ->
'$document', '$http', '$q', '$resource', 'flash', 'xsrf',
($document, $http, $q, $resource, flash, xsrf) ->
actions = {}
provider = this
......@@ -72,7 +72,8 @@ class SessionProvider
actions[name].transformRequest = prepare
actions[name].transformResponse = process
endpoint = documentHelpers.absoluteURI('/app')
base = $document.prop('baseURI')
endpoint = new URL('/app', base).href
$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