Move ajax and auth into services
This commit is contained in:
parent
d677307006
commit
5cf1cbba6b
|
@ -2,6 +2,8 @@
|
|||
`import config from 'travis/config/environment'`
|
||||
|
||||
Adapter = DS.ActiveModelAdapter.extend
|
||||
auth: Ember.inject.service()
|
||||
|
||||
host: config.apiEndpoint
|
||||
coalesceFindRequests: true
|
||||
|
||||
|
@ -12,7 +14,7 @@ Adapter = DS.ActiveModelAdapter.extend
|
|||
|
||||
hash.headers['accept'] = 'application/json; version=2'
|
||||
|
||||
if token = Travis.sessionStorage.getItem('travis.token')
|
||||
if token = @get('auth').token()
|
||||
hash.headers['Authorization'] ||= "token #{token}"
|
||||
|
||||
hash
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
|
||||
CachesItemComponent = Ember.Component.extend
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
tagName: 'li'
|
||||
classNames: ['cache-item']
|
||||
|
@ -20,7 +20,7 @@ CachesItemComponent = Ember.Component.extend
|
|||
deletingDone = => @set('isDeleting', false)
|
||||
|
||||
repo = @get('repo')
|
||||
Ajax.ajax("/repos/#{repo.get('id')}/caches", "DELETE", data: data).then(deletingDone, deletingDone).then =>
|
||||
@get('ajax').ajax("/repos/#{repo.get('id')}/caches", "DELETE", data: data).then(deletingDone, deletingDone).then =>
|
||||
@get('caches').removeObject(@get('cache'))
|
||||
|
||||
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import config from 'travis/config/environment'`
|
||||
|
||||
NoBuildsComponent = Ember.Component.extend
|
||||
|
||||
actions:
|
||||
actions:
|
||||
triggerBuild: () ->
|
||||
@set('isLoading', true)
|
||||
apiEndpoint = config.apiEndpoint
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import config from 'travis/config/environment'`
|
||||
|
||||
TravisStatusComponent = Ember.Component.extend
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
|
||||
Controller = Ember.Controller.extend
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
repoController: Ember.inject.controller('repo')
|
||||
repo: Ember.computed.alias('repoController.repo')
|
||||
|
||||
|
@ -21,7 +22,7 @@ Controller = Ember.Controller.extend
|
|||
deletingDone = => @set('isDeleting', false)
|
||||
|
||||
repo = @get('repo')
|
||||
Ajax.ajax("/repos/#{@get('repo.id')}/caches", "DELETE").then(deletingDone, deletingDone).then =>
|
||||
@get('ajax').ajax("/repos/#{@get('repo.id')}/caches", "DELETE").then(deletingDone, deletingDone).then =>
|
||||
@set('model', {})
|
||||
|
||||
`export default Controller`
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
|
||||
Controller = Ember.Controller.extend
|
||||
isLoading: false
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
|
||||
Controller = Ember.Controller.extend
|
||||
isLoading: false
|
||||
|
@ -13,14 +12,13 @@ Controller = Ember.Controller.extend
|
|||
item
|
||||
).sortBy('default_branch.last_build.finished_at').reverse()
|
||||
repos
|
||||
|
||||
).property('model')
|
||||
|
||||
# running: (->
|
||||
# data = @get('model')
|
||||
|
||||
# repos = data.repositories.filter (item, index) ->
|
||||
# if item.default_branch.last_build != null
|
||||
# repos = data.repositories.filter (item, index) ->
|
||||
# if item.default_branch.last_build != null
|
||||
# if item.default_branch.last_build.state == 'started'
|
||||
# item
|
||||
# repos
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
|
||||
Controller = Ember.Controller.extend
|
||||
isLoading: false
|
||||
|
|
|
@ -1,16 +1,11 @@
|
|||
`import Auth from 'travis/utils/auth'`
|
||||
`import TestAuth from 'travis/utils/test-auth'`
|
||||
|
||||
initialize = (container, app) ->
|
||||
app.register 'auth:main', if Ember.testing then TestAuth else Auth
|
||||
|
||||
app.inject('route', 'auth', 'auth:main')
|
||||
app.inject('controller', 'auth', 'auth:main')
|
||||
app.inject('application', 'auth', 'auth:main')
|
||||
app.inject('component', 'auth', 'auth:main')
|
||||
app.inject('service:flashes', 'auth', 'auth:main')
|
||||
|
||||
app.inject('auth', 'store', 'service:store')
|
||||
app.inject('route', 'auth', 'service:auth')
|
||||
app.inject('controller', 'auth', 'service:auth')
|
||||
app.inject('application', 'auth', 'service:auth')
|
||||
app.inject('component', 'auth', 'service:auth')
|
||||
app.inject('service:flashes', 'auth', 'service:auth')
|
||||
|
||||
AuthInitializer =
|
||||
name: 'auth'
|
||||
|
|
|
@ -2,12 +2,7 @@
|
|||
`import TravisPusher from 'travis/utils/pusher'`
|
||||
|
||||
initialize = (registry, application) ->
|
||||
if config.pusher.key
|
||||
application.pusher = new TravisPusher(config.pusher)
|
||||
|
||||
application.register 'pusher:main', application.pusher, { instantiate: false }
|
||||
|
||||
application.inject('route', 'pusher', 'pusher:main')
|
||||
null
|
||||
|
||||
PusherInitializer =
|
||||
name: 'pusher'
|
||||
|
|
|
@ -1,16 +1,11 @@
|
|||
`import Slider from 'travis/utils/slider'`
|
||||
`import Tailing from 'travis/utils/tailing'`
|
||||
`import ToTop from 'travis/utils/to-top'`
|
||||
`import config from 'travis/config/environment'`
|
||||
|
||||
initialize = (container, application) ->
|
||||
application.slider = new Slider(application.storage)
|
||||
application.tailing = new Tailing($(window), '#tail', '#log')
|
||||
application.toTop = new ToTop($(window), '.to-top', '#log-container')
|
||||
|
||||
application.register 'slider:main', application.slider, { instantiate: false }
|
||||
application.inject('controller', 'slider', 'slider:main')
|
||||
|
||||
Initializer =
|
||||
name: 'services'
|
||||
initialize: initialize
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
`import Ember from 'ember'`
|
||||
|
||||
Storage = Em.Object.extend
|
||||
init: ->
|
||||
@set('storage', {})
|
||||
key: (key) ->
|
||||
"__#{key.replace('.', '__')}"
|
||||
getItem: (k) ->
|
||||
return @get("storage.#{@key(k)}")
|
||||
setItem: (k,v) ->
|
||||
@set("storage.#{@key(k)}", v)
|
||||
removeItem: (k) ->
|
||||
@setItem(k, null)
|
||||
clear: ->
|
||||
@set('storage', {})
|
||||
|
||||
sessionStorage = (->
|
||||
storage = null
|
||||
try
|
||||
# firefox will not throw error on access for sessionStorage var,
|
||||
# you need to actually get something from session
|
||||
window.sessionStorage.getItem('foo')
|
||||
storage = window.sessionStorage
|
||||
catch err
|
||||
storage = Storage.create()
|
||||
|
||||
storage
|
||||
)()
|
||||
|
||||
storage = (->
|
||||
storage = null
|
||||
try
|
||||
storage = window.localStorage || throw('no storage')
|
||||
catch err
|
||||
storage = Storage.create()
|
||||
|
||||
storage
|
||||
)()
|
||||
|
||||
initialize = (container, application) ->
|
||||
application.register 'storage:main', storage, { instantiate: false }
|
||||
application.register 'sessionStorage:main', sessionStorage, { instantiate: false }
|
||||
|
||||
application.inject('auth', 'storage', 'storage:main')
|
||||
application.inject('auth', 'sessionStorage', 'sessionStorage:main')
|
||||
|
||||
# I still use Travis.storage in some places which are not that easy to
|
||||
# refactor
|
||||
application.storage = storage
|
||||
application.sessionStorage = sessionStorage
|
||||
|
||||
StorageInitializer =
|
||||
name: 'storage'
|
||||
before: 'services'
|
||||
initialize: initialize
|
||||
|
||||
`export {initialize}`
|
||||
`export default StorageInitializer`
|
|
@ -1,5 +1,17 @@
|
|||
`import config from 'travis/config/environment'`
|
||||
`import TravisPusher from 'travis/utils/pusher'`
|
||||
|
||||
initialize = (data) ->
|
||||
data.application.pusher.store = data.container.lookup('service:store')
|
||||
application = data.application
|
||||
|
||||
if config.pusher.key
|
||||
application.pusher = new TravisPusher(config.pusher)
|
||||
|
||||
application.register 'pusher:main', application.pusher, { instantiate: false }
|
||||
|
||||
application.inject('route', 'pusher', 'pusher:main')
|
||||
|
||||
application.pusher.store = data.container.lookup('service:store')
|
||||
|
||||
PusherInitializer =
|
||||
name: 'pusher'
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
`import { durationFrom, configKeys, compact } from 'travis/utils/helpers'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import configKeysMap from 'travis/utils/keys-map'`
|
||||
`import Ember from 'ember'`
|
||||
`import Model from 'travis/models/model'`
|
||||
`import DurationCalculations from 'travis/utils/duration-calculations'`
|
||||
|
||||
Build = Model.extend DurationCalculations,
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
state: DS.attr()
|
||||
number: DS.attr('number')
|
||||
branch: DS.attr('string')
|
||||
|
@ -90,11 +91,11 @@ Build = Model.extend DurationCalculations,
|
|||
canRestart: Ember.computed.alias('isFinished')
|
||||
|
||||
cancel: (->
|
||||
Ajax.post "/builds/#{@get('id')}/cancel"
|
||||
@get('ajax').post "/builds/#{@get('id')}/cancel"
|
||||
)
|
||||
|
||||
restart: ->
|
||||
Ajax.post "/builds/#{@get('id')}/restart"
|
||||
@get('ajax').post "/builds/#{@get('id')}/restart"
|
||||
|
||||
formattedFinishedAt: (->
|
||||
if finishedAt = @get('finishedAt')
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
`import { durationFrom, configKeys, compact } from 'travis/utils/helpers'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import configKeysMap from 'travis/utils/keys-map'`
|
||||
`import Ember from 'ember'`
|
||||
`import Model from 'travis/models/model'`
|
||||
|
@ -7,6 +6,7 @@
|
|||
`import DurationCalculations from 'travis/utils/duration-calculations'`
|
||||
|
||||
Job = Model.extend DurationCalculations,
|
||||
ajax: Ember.inject.service()
|
||||
logId: DS.attr()
|
||||
|
||||
queue: DS.attr()
|
||||
|
@ -30,7 +30,7 @@ Job = Model.extend DurationCalculations,
|
|||
|
||||
log: ( ->
|
||||
@set('isLogAccessed', true)
|
||||
Log.create(job: this)
|
||||
Log.create(job: this, ajax: @get('ajax'))
|
||||
).property()
|
||||
|
||||
startedAt: (->
|
||||
|
@ -94,11 +94,11 @@ Job = Model.extend DurationCalculations,
|
|||
canRestart: Ember.computed.alias('isFinished')
|
||||
|
||||
cancel: (->
|
||||
Ajax.post "/jobs/#{@get('id')}/cancel"
|
||||
@get('ajax').post "/jobs/#{@get('id')}/cancel"
|
||||
)
|
||||
|
||||
removeLog: ->
|
||||
Ajax.patch("/jobs/#{@get('id')}/log").then =>
|
||||
@get('ajax').patch("/jobs/#{@get('id')}/log").then =>
|
||||
@reloadLog()
|
||||
|
||||
reloadLog: ->
|
||||
|
@ -106,7 +106,7 @@ Job = Model.extend DurationCalculations,
|
|||
@get('log').fetch()
|
||||
|
||||
restart: ->
|
||||
Ajax.post "/jobs/#{@get('id')}/restart"
|
||||
@get('ajax').post "/jobs/#{@get('id')}/restart"
|
||||
|
||||
appendLog: (part) ->
|
||||
@get('log').append part
|
||||
|
|
|
@ -9,7 +9,7 @@ Request = Ember.Object.extend
|
|||
accept: 'application/json; chunked=true; version=2, text/plain; version=2'
|
||||
|
||||
run: ->
|
||||
Ajax.ajax "/jobs/#{@id}/log?cors_hax=true", 'GET',
|
||||
@get('ajax').ajax "/jobs/#{@id}/log?cors_hax=true", 'GET',
|
||||
dataType: 'text'
|
||||
headers: @HEADERS
|
||||
success: (body, status, xhr) => Ember.run(this, -> @handle(body, status, xhr))
|
||||
|
@ -50,7 +50,7 @@ Log = Ember.Object.extend
|
|||
data['part_numbers'] = partNumbers if partNumbers
|
||||
data['after'] = after if after
|
||||
|
||||
Ajax.ajax "/jobs/#{@get('job.id')}/log", 'GET',
|
||||
@get('ajax').ajax "/jobs/#{@get('job.id')}/log", 'GET',
|
||||
dataType: 'json'
|
||||
headers:
|
||||
accept: 'application/json; chunked=true; version=2'
|
||||
|
@ -81,7 +81,8 @@ Log = Ember.Object.extend
|
|||
@set('removed', true)
|
||||
@loadParts(json['log']['parts'])
|
||||
text: (text) => @loadText(text)
|
||||
Request.create(id: id, handlers: handlers, log: this).run() if id = @get('job.id')
|
||||
if id = @get('job.id')
|
||||
Request.create(id: id, handlers: handlers, log: this, ajax: @get('ajax')).run()
|
||||
|
||||
clear: ->
|
||||
@clearParts()
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
`import ExpandableRecordArray from 'travis/utils/expandable-record-array'`
|
||||
`import Model from 'travis/models/model'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
# TODO: Investigate for some weird reason if I use durationFrom here not durationFromHelper,
|
||||
# the function stops being visible inside computed properties.
|
||||
`import { durationFrom as durationFromHelper } from 'travis/utils/helpers'`
|
||||
`import Build from 'travis/models/build'`
|
||||
|
||||
Repo = Model.extend
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
slug: DS.attr()
|
||||
description: DS.attr()
|
||||
private: DS.attr('boolean')
|
||||
|
@ -135,14 +136,14 @@ Repo = Model.extend
|
|||
@notifyPropertyChange 'lastBuildDuration'
|
||||
|
||||
regenerateKey: (options) ->
|
||||
Ajax.ajax '/repos/' + @get('id') + '/key', 'post', options
|
||||
@get('ajax').ajax '/repos/' + @get('id') + '/key', 'post', options
|
||||
|
||||
fetchSettings: ->
|
||||
Ajax.ajax('/repos/' + @get('id') + '/settings', 'get', forceAuth: true).then (data) ->
|
||||
@get('ajax').ajax('/repos/' + @get('id') + '/settings', 'get', forceAuth: true).then (data) ->
|
||||
data['settings']
|
||||
|
||||
saveSettings: (settings) ->
|
||||
Ajax.ajax('/repos/' + @get('id') + '/settings', 'patch', data: { settings: settings })
|
||||
@get('ajax').ajax('/repos/' + @get('id') + '/settings', 'patch', data: { settings: settings })
|
||||
|
||||
Repo.reopenClass
|
||||
recent: ->
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Model from 'travis/models/model'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import config from 'travis/config/environment'`
|
||||
|
||||
User = Model.extend
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
# TODO: this totally not should be needed here
|
||||
sessionStorage: Ember.inject.service()
|
||||
|
||||
name: DS.attr()
|
||||
email: DS.attr()
|
||||
login: DS.attr()
|
||||
|
@ -27,7 +31,7 @@ User = Model.extend
|
|||
).property()
|
||||
|
||||
_rawPermissions: (->
|
||||
Ajax.get('/users/permissions')
|
||||
@get('ajax').get('/users/permissions')
|
||||
).property()
|
||||
|
||||
permissions: (->
|
||||
|
@ -72,12 +76,12 @@ User = Model.extend
|
|||
|
||||
sync: ->
|
||||
self = this
|
||||
Ajax.post('/users/sync', {}, ->
|
||||
@get('ajax').post('/users/sync', {}, ->
|
||||
self.setWithSession('isSyncing', true)
|
||||
)
|
||||
|
||||
poll: ->
|
||||
Ajax.get '/users', (data) =>
|
||||
@get('ajax').get '/users', (data) =>
|
||||
if data.user.is_syncing
|
||||
self = this
|
||||
setTimeout ->
|
||||
|
@ -92,8 +96,8 @@ User = Model.extend
|
|||
|
||||
setWithSession: (name, value) ->
|
||||
@set(name, value)
|
||||
user = JSON.parse(Travis.sessionStorage.getItem('travis.user'))
|
||||
user = JSON.parse(@get('sessionStorage').getItem('travis.user'))
|
||||
user[name.underscore()] = @get(name)
|
||||
Travis.sessionStorage.setItem('travis.user', JSON.stringify(user))
|
||||
@get('sessionStorage').setItem('travis.user', JSON.stringify(user))
|
||||
|
||||
`export default User`
|
||||
|
|
|
@ -13,7 +13,7 @@ Router = Ember.Router.extend
|
|||
#
|
||||
# we should probably think about a more general way to
|
||||
# do this, location should not know about auth status
|
||||
Location.create(auth: @container.lookup('auth:main'))
|
||||
Location.create(auth: @container.lookup('service:auth'))
|
||||
).property()
|
||||
|
||||
# TODO: this is needed, because in the original version
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
`import Ember from 'ember'`
|
||||
`import TravisRoute from 'travis/routes/basic'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
|
||||
Route = TravisRoute.extend
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
needsAuth: true
|
||||
setupController: (controller) ->
|
||||
@_super.apply this, arguments
|
||||
|
@ -10,7 +11,7 @@ Route = TravisRoute.extend
|
|||
|
||||
model: ->
|
||||
repo = @modelFor('repo')
|
||||
Ajax.get("/repos/#{repo.get('id')}/caches").then( (data) ->
|
||||
@get('ajax').get("/repos/#{repo.get('id')}/caches").then( (data) ->
|
||||
caches = {}
|
||||
|
||||
data["caches"].forEach (cacheData) ->
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
`import Ember from 'ember'`
|
||||
`import TravisRoute from 'travis/routes/basic'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import config from 'travis/config/environment'`
|
||||
|
||||
Route = TravisRoute.extend
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
`import Ember from 'ember'`
|
||||
`import TravisRoute from 'travis/routes/basic'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import config from 'travis/config/environment'`
|
||||
|
||||
Route = TravisRoute.extend
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
`import Ember from 'ember'`
|
||||
`import TravisRoute from 'travis/routes/basic'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import config from 'travis/config/environment'`
|
||||
|
||||
Route = TravisRoute.extend
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
`import TravisRoute from 'travis/routes/basic'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import config from 'travis/config/environment'`
|
||||
|
||||
Route = TravisRoute.extend
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
needsAuth: true
|
||||
setupController: (controller, model) ->
|
||||
@_super.apply(this, arguments)
|
||||
|
@ -27,7 +28,7 @@ Route = TravisRoute.extend
|
|||
|
||||
fetchSshKey: () ->
|
||||
repo = @modelFor('repo')
|
||||
Ajax.get "/repos/#{repo.get('id')}/key", (data) =>
|
||||
@get('ajax').get "/repos/#{repo.get('id')}/key", (data) =>
|
||||
Ember.Object.create(fingerprint: data.fingerprint)
|
||||
|
||||
fetchRepositoryActiveFlag: ->
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
`import TravisRoute from 'travis/routes/basic'`
|
||||
|
||||
Route = TravisRoute.extend
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
titleToken: 'Ssh Keys'
|
||||
|
||||
model: (params) ->
|
||||
|
@ -17,7 +18,7 @@ Route = TravisRoute.extend
|
|||
|
||||
afterModel: (model, transition) ->
|
||||
repo = @modelFor('repo')
|
||||
Ajax.get "/repos/#{repo.get('id')}/key", (data) =>
|
||||
@get('ajax').get "/repos/#{repo.get('id')}/key", (data) =>
|
||||
@defaultKey = Ember.Object.create(fingerprint: data.fingerprint)
|
||||
|
||||
setupController: (controller, model) ->
|
||||
|
|
|
@ -7,7 +7,9 @@ default_options =
|
|||
accepts:
|
||||
json: 'application/json; version=2'
|
||||
|
||||
ajax = Em.Object.create
|
||||
Ajax = Ember.Service.extend
|
||||
auth: Ember.inject.service()
|
||||
|
||||
publicEndpoints: [/\/repos\/?.*/, /\/builds\/?.*/, /\/jobs\/?.*/]
|
||||
privateEndpoints: [/\/repos\/\d+\/caches/]
|
||||
|
||||
|
@ -42,8 +44,8 @@ ajax = Em.Object.create
|
|||
endpoint = config.apiEndpoint || ''
|
||||
options = options || {}
|
||||
|
||||
token = Travis.sessionStorage.getItem('travis.token')
|
||||
if token && (ajax.needsAuth(method, url) || options.forceAuth)
|
||||
token = Ember.get(this, 'auth').token()
|
||||
if token && (@needsAuth(method, url) || options.forceAuth)
|
||||
options.headers ||= {}
|
||||
options.headers['Authorization'] ||= "token #{token}"
|
||||
|
||||
|
@ -66,9 +68,9 @@ ajax = Em.Object.create
|
|||
|
||||
error = options.error || (->)
|
||||
options.error = (data, status, xhr) =>
|
||||
console.log "[ERROR] API responded with an error (#{status}): #{JSON.stringify(data)}"
|
||||
Travis.lookup('controller:flash').pushObject(data.flash) if data?.flash
|
||||
delete data.flash if data?
|
||||
console.log "[ERROR] API responded with an error (#{status}): #{JSON.stringify(data)}"
|
||||
error.apply(this, arguments)
|
||||
|
||||
options = $.extend(options, default_options)
|
||||
|
@ -153,4 +155,4 @@ ajax = Em.Object.create
|
|||
|
||||
return promise
|
||||
|
||||
`export default ajax`
|
||||
`export default Ajax`
|
|
@ -1,7 +1,11 @@
|
|||
`import config from 'travis/config/environment'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
|
||||
Auth = Ember.Object.extend
|
||||
Auth = Ember.Service.extend
|
||||
store: Ember.inject.service(),
|
||||
storage: Ember.inject.service(),
|
||||
sessionStorage: Ember.inject.service(),
|
||||
ajax: Ember.inject.service()
|
||||
|
||||
state: "signed-out"
|
||||
receivingEnd: "#{location.protocol}//#{location.host}"
|
||||
|
||||
|
@ -9,20 +13,20 @@ Auth = Ember.Object.extend
|
|||
window.addEventListener('message', (e) => @receiveMessage(e))
|
||||
|
||||
token: ->
|
||||
Travis.sessionStorage.getItem('travis.token')
|
||||
@get('sessionStorage').getItem('travis.token')
|
||||
|
||||
endpoint: (->
|
||||
config.apiEndpoint
|
||||
).property(),
|
||||
|
||||
signOut: ->
|
||||
@storage.removeItem('travis.user')
|
||||
@storage.removeItem('travis.token')
|
||||
@sessionStorage.clear()
|
||||
@get('storage').removeItem('travis.user')
|
||||
@get('storage').removeItem('travis.token')
|
||||
@get('sessionStorage').clear()
|
||||
@set('state', 'signed-out')
|
||||
@set('user', undefined)
|
||||
if user = @get('currentUser')
|
||||
@store.unloadAll('user')
|
||||
@get('store').unloadAll('user')
|
||||
@set('currentUser', null)
|
||||
@sendToApp('afterSignOut')
|
||||
Travis.trigger('user:signed_out')
|
||||
|
@ -36,7 +40,7 @@ Auth = Ember.Object.extend
|
|||
$('<iframe id="auth-frame" />').hide().appendTo('body').attr('src', url)
|
||||
|
||||
autoSignIn: (data) ->
|
||||
data ||= @userDataFrom(@sessionStorage) || @userDataFrom(@storage)
|
||||
data ||= @userDataFrom(@get('sessionStorage')) || @userDataFrom(@get('storage'))
|
||||
@setData(data) if data
|
||||
|
||||
userDataFrom: (storage) ->
|
||||
|
@ -54,7 +58,7 @@ Auth = Ember.Object.extend
|
|||
|
||||
validateUser: (user) ->
|
||||
fieldsToValidate = ['id', 'login', 'token']
|
||||
isTravisBecome = sessionStorage.getItem('travis.become')
|
||||
isTravisBecome = @get('sessionStorage').getItem('travis.become')
|
||||
|
||||
unless isTravisBecome
|
||||
fieldsToValidate.push 'correct_scopes'
|
||||
|
@ -72,8 +76,8 @@ Auth = Ember.Object.extend
|
|||
false
|
||||
|
||||
setData: (data) ->
|
||||
@storeData(data, @sessionStorage)
|
||||
@storeData(data, @storage) unless @userDataFrom(@storage)
|
||||
@storeData(data, @get('sessionStorage'))
|
||||
@storeData(data, @get('storage')) unless @userDataFrom(@get('storage'))
|
||||
user = @loadUser(data.user)
|
||||
@set('currentUser', user)
|
||||
|
||||
|
@ -83,19 +87,19 @@ Auth = Ember.Object.extend
|
|||
|
||||
refreshUserData: (user) ->
|
||||
unless user
|
||||
if data = @userDataFrom(@sessionStorage) || @userDataFrom(@storage)
|
||||
if data = @userDataFrom(@get('sessionStorage')) || @userDataFrom(@get('storage'))
|
||||
user = data.user
|
||||
|
||||
if user
|
||||
Ajax.get("/users/#{user.id}").then (data) =>
|
||||
@get('ajax').get("/users/#{user.id}").then (data) =>
|
||||
if data.user.correct_scopes
|
||||
userRecord = @loadUser(data.user)
|
||||
userRecord.get('permissions')
|
||||
# if user is still signed in, update saved data
|
||||
if @get('signedIn')
|
||||
data.user.token = user.token
|
||||
@storeData(data, @sessionStorage)
|
||||
@storeData(data, @storage)
|
||||
@storeData(data, @get('sessionStorage'))
|
||||
@storeData(data, @get('storage'))
|
||||
Travis.trigger('user:refreshed', data.user)
|
||||
else
|
||||
return Ember.RSVP.Promise.reject()
|
||||
|
@ -119,13 +123,13 @@ Auth = Ember.Object.extend
|
|||
storage.setItem('travis.user', JSON.stringify(data.user))
|
||||
|
||||
loadUser: (user) ->
|
||||
@store.push(
|
||||
@get('store').push(
|
||||
data:
|
||||
type: 'user',
|
||||
id: user.id
|
||||
attributes: user
|
||||
)
|
||||
@store.recordForId('user', user.id)
|
||||
@get('store').recordForId('user', user.id)
|
||||
|
||||
receiveMessage: (event) ->
|
||||
if event.origin == @expectedOrigin()
|
18
app/services/session-storage.coffee
Normal file
18
app/services/session-storage.coffee
Normal file
|
@ -0,0 +1,18 @@
|
|||
`import Ember from 'ember'`
|
||||
`import StorageService from 'travis/services/storage'`
|
||||
`import Storage from 'travis/utils/hash-storage'`
|
||||
|
||||
SessionStorageService = StorageService.extend
|
||||
init: ->
|
||||
storage = null
|
||||
try
|
||||
# firefox will not throw error on access for sessionStorage var,
|
||||
# you need to actually get something from session
|
||||
window.sessionStorage.getItem('foo')
|
||||
storage = window.sessionStorage
|
||||
catch err
|
||||
storage = Storage.create()
|
||||
|
||||
@set('storage', storage)
|
||||
|
||||
`export default SessionStorageService`
|
22
app/services/storage.coffee
Normal file
22
app/services/storage.coffee
Normal file
|
@ -0,0 +1,22 @@
|
|||
`import Ember from 'ember'`
|
||||
`import Storage from 'travis/utils/hash-storage'`
|
||||
|
||||
StorageService = Ember.Service.extend
|
||||
init: ->
|
||||
storage = null
|
||||
try
|
||||
storage = window.localStorage || throw('no storage')
|
||||
catch err
|
||||
storage = Storage.create()
|
||||
|
||||
@set('storage', storage)
|
||||
getItem: (key) ->
|
||||
return @get("storage").getItem(key)
|
||||
setItem: (key, value) ->
|
||||
return @get("storage").setItem(key, value)
|
||||
removeItem: (key) ->
|
||||
return @get("storage").removeItem(key)
|
||||
clear: ->
|
||||
return @get("storage").clear()
|
||||
|
||||
`export default StorageService`
|
|
@ -2,6 +2,8 @@
|
|||
`import config from 'travis/config/environment'`
|
||||
|
||||
Store = DS.Store.extend
|
||||
auth: Ember.inject.service()
|
||||
|
||||
defaultAdapter: 'application'
|
||||
adapter: 'application'
|
||||
|
||||
|
@ -17,7 +19,7 @@ Store = DS.Store.extend
|
|||
|
||||
canHandleEvent: (event, data) ->
|
||||
[name, type] = event.split(':')
|
||||
auth = @container.lookup('auth:main')
|
||||
auth = @get('auth')
|
||||
if event != 'job:log' && auth.get('signedIn') &&
|
||||
!config.pro && !config.enterprise
|
||||
# if recent repos hasn't been opened yet, we can safely
|
||||
|
|
17
app/utils/hash-storage.coffee
Normal file
17
app/utils/hash-storage.coffee
Normal file
|
@ -0,0 +1,17 @@
|
|||
`import Ember from 'ember'`
|
||||
|
||||
Storage = Ember.Object.extend
|
||||
init: ->
|
||||
@set('storage', {})
|
||||
key: (key) ->
|
||||
"__#{key.replace('.', '__')}"
|
||||
getItem: (k) ->
|
||||
return @get("storage.#{@key(k)}")
|
||||
setItem: (k,v) ->
|
||||
@set("storage.#{@key(k)}", v)
|
||||
removeItem: (k) ->
|
||||
@setItem(k, null)
|
||||
clear: ->
|
||||
@set('storage', {})
|
||||
|
||||
`export default Storage`
|
|
@ -1,13 +1,13 @@
|
|||
`import ENV from 'travis/config/environment'`
|
||||
`import Ajax from 'travis/utils/ajax'`
|
||||
|
||||
TravisPusher = (config) ->
|
||||
@init(config)
|
||||
TravisPusher = (config, ajaxService) ->
|
||||
@init(config, ajaxService)
|
||||
this
|
||||
|
||||
TravisPusher.prototype.active_channels = []
|
||||
|
||||
TravisPusher.prototype.init = (config) ->
|
||||
TravisPusher.prototype.init = (config, ajaxService) ->
|
||||
this.ajaxService = ajaxService
|
||||
Pusher.warn = @warn.bind(this)
|
||||
Pusher.host = config.host if config.host
|
||||
@pusher = new Pusher(config.key, encrypted: config.encrypted, disableStats: true)
|
||||
|
@ -93,7 +93,7 @@ if ENV.pro
|
|||
|
||||
unless channels.fetching
|
||||
channels.fetching = true
|
||||
Ajax.post Pusher.channel_auth_endpoint, { socket_id: socketId, channels: names }, (data) ->
|
||||
TravisPusher.ajaxService.post Pusher.channel_auth_endpoint, { socket_id: socketId, channels: names }, (data) ->
|
||||
channels.fetching = false
|
||||
callback(data.channels) for callback in channels.callbacks
|
||||
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
`import Ember from 'ember'`
|
||||
|
||||
Slider = (storage) ->
|
||||
@minimize() if storage.getItem('travis.maximized') == 'true'
|
||||
this
|
||||
|
||||
Slider.prototype.persist = ->
|
||||
Travis.storage.setItem('travis.maximized', @isMinimized())
|
||||
|
||||
Slider.prototype.isMinimized = ->
|
||||
return $('body').hasClass('maximized');
|
||||
|
||||
Slider.prototype.minimize = ->
|
||||
$('body').addClass('maximized')
|
||||
|
||||
Slider.prototype.toggle = ->
|
||||
$('body').toggleClass('maximized')
|
||||
@persist()
|
||||
# TODO gotta force redraws here :/
|
||||
element = $('<span></span>')
|
||||
$('#top .profile').append(element)
|
||||
Ember.run.later (-> element.remove()), 10
|
||||
|
||||
`export default Slider`
|
Loading…
Reference in New Issue
Block a user