Refactor repo tools to use components

This commit is contained in:
Piotr Sarnacki 2015-08-05 14:53:03 +02:00
parent 759e56d7ba
commit daeb5fa056
11 changed files with 93 additions and 152 deletions

View File

@ -0,0 +1,47 @@
`import Ember from 'ember'`
`import config from 'travis/config/environment'`
RepoShowToolsComponent = Ember.Component.extend
popup: Ember.inject.service()
click: (event) ->
if $(event.target).is('a') && $(event.target).parents('.dropdown-menu').length
@closeMenu()
closeMenu: ->
$('.menu').removeClass('display')
actions:
menu: ->
@get('popup').close()
$('#tools .menu').toggleClass('display')
return false
hasPermission: (->
if permissions = @get('currentUser.permissions')
permissions.contains parseInt(@get('repo.id'))
).property('currentUser.permissions.length', 'repo.id')
hasPushPermission: (->
if permissions = @get('currentUser.pushPermissions')
permissions.contains parseInt(@get('repo.id'))
).property('currentUser.pushPermissions.length', 'repo.id')
hasAdminPermission: (->
if permissions = @get('currentUser.adminPermissions')
permissions.contains parseInt(@get('repo.id'))
).property('currentUser.adminPermissions.length', 'repo.id')
displaySettingsLink: (->
@get('hasPushPermission')
).property('hasPushPermission')
displayCachesLink: (->
@get('hasPushPermission') && config.endpoints.caches
).property('hasPushPermission')
displayStatusImages: (->
@get('hasPermission')
).property('hasPermission')
`export default RepoShowToolsComponent`

View File

@ -3,6 +3,8 @@
`import { colorForState } from 'travis/utils/helpers'`
ReposListItemComponent = Ember.Component.extend Polling,
routing: Ember.inject.service('-routing')
tagName: 'li'
pollModels: 'repo'
@ -24,6 +26,6 @@ ReposListItemComponent = Ember.Component.extend Polling,
click: ->
@scrollTop()
@get('controller').transitionToRoute('/' + @get('repo.slug'))
@get('routing').transitionTo('repo', @get('repo.slug').split('/'))
`export default ReposListItemComponent`

15
app/services/popup.coffee Normal file
View File

@ -0,0 +1,15 @@
`import Ember from 'ember'`
PopupService = Ember.Service.extend
open: (name) ->
@closeAll()
name = event?.target?.name || name
$("##{name}").toggleClass('display')
close: ->
if view = Ember.View.currentPopupView
view.destroy()
Ember.View.currentPopupView = null
$('.popup').removeClass('display')
`export default PopupService`

View File

@ -0,0 +1,18 @@
<div class="dropdown dropdown--classic" id="tools">
<a href="#" {{action "menu"}} class="menu-popup-button dropdown-button btn" type=""><span class="icon icon-cog-light"></span> Settings <span class="icon icon-arrow-down"></span></a>
<ul class="dropdown-menu menu">
{{#if displaySettingsLink}}
<li>
{{#link-to "settings" repo}}Settings{{/link-to}}
</li>
{{/if}}
<li>
{{#link-to "requests" repo}}Requests{{/link-to}}
</li>
{{#if displayCachesLink}}
<li>
{{#link-to "caches" repo}}Caches{{/link-to}}
</li>
{{/if}}
</ul>
</div>

View File

@ -18,7 +18,7 @@
</header>
<div class="repo-menus">
<div class="repo-menu-header">
{{view 'repo-show-tools'}}
{{repo-show-tools repo=repo build=build job=job tab=tab currentUser=auth.currentUser}}
{{repo-show-tabs repo=repo tab=tab}}
</div>

View File

@ -1,47 +1 @@
<div class="dropdown dropdown--classic" id="tools">
<a href="#" {{action "menu" target=view}} class="menu-popup-button dropdown-button btn" type=""><span class="icon icon-cog-light"></span> Settings <span class="icon icon-arrow-down"></span></a>
<ul class="dropdown-menu menu">
{{#if view.displaySettingsLink}}
<li>
{{#link-to "settings" view.repo}}Settings{{/link-to}}
</li>
{{/if}}
<li>
{{#link-to "requests" view.repo}}Requests{{/link-to}}
</li>
{{#if view.displayCachesLink}}
<li>
{{#link-to "caches" view.repo}}Caches{{/link-to}}
</li>
{{/if}}
</ul>
</div>
<div id="regenerate-key" class="popup">
<a href="#" class="close" {{action "popupClose" target=view}}></a>
<p>
Do you really want to regenerate ssl keys for this repository? Please note that
any data, which is encrypted, such as notification services credentials or secure
environment variables will not be accessible during the builds until you re-encrypt
it with the new key.
</p>
<p>
<a class="sync_now button" {{action "regenerateKey" target=view}}>Yes, do it!</a>
<span class="or">or</span>
<a href="#" class="cancel" {{action "popupClose" target=view}}>Cancel</a>
</p>
</div>
<div id="regeneration-success" class="popup">
<a href="#" class="close" {{action "popupClose" target=view}}></a>
<p>
Key for this repository has been regenerated. If you used previous key
for encryption, you will need encrypt your data again with the new key.
</p>
<p>
You can read more about encryption keys <a href="http://about.travis-ci.org/docs/user/encryption-keys/">in Travis documentation</a>
</p>
</div>

View File

@ -1,6 +1,8 @@
`import BasicView from 'travis/views/basic'`
View = BasicView.extend
popup: Ember.inject.service()
classNames: ['application']
click: (event) ->
# TODO: this solves the case of closing menus and popups,
@ -8,7 +10,7 @@ View = BasicView.extend
# yet, but this does not seem optimal
targetAndParents = $(event.target).parents().andSelf()
if ! ( targetAndParents.hasClass('open-popup') || targetAndParents.hasClass('popup') )
@popupCloseAll()
@get('popup').close()
# TODO: I needed to add second check to this conditional, because for some reason
# event.stopPropagation() in menu() function in RepoShowToolsView does

View File

@ -1,21 +1,5 @@
`import Ember from 'ember'`
View = Ember.View.extend
actions:
popup: (name) -> @popup(name)
popupClose: -> @popupClose()
popup: (name) ->
@popupCloseAll()
name = event?.target?.name || name
$("##{name}").toggleClass('display')
popupClose: ->
$('.popup').removeClass('display')
popupCloseAll: ->
if view = Ember.View.currentPopupView
view.destroy()
Ember.View.currentPopupView = null
$('.popup').removeClass('display')
View = Ember.View.extend()
`export default View`

View File

@ -42,6 +42,8 @@ Object.defineProperty Log.Limit.prototype, 'limited',
@count >= @max_lines
View = BasicView.extend
popup: Ember.inject.service()
templateName: 'jobs/pre'
currentUserBinding: 'controller.auth.currentUser'
@ -144,7 +146,7 @@ View = BasicView.extend
removeLogPopup: ->
if @get('canRemoveLog')
@popup('remove-log-popup')
@get('popup').open('remove-log-popup')
return false
noop: -> # TODO required?

View File

@ -1,84 +0,0 @@
`import Ember from 'ember'`
`import BasicView from 'travis/views/basic'`
`import config from 'travis/config/environment'`
View = BasicView.extend
templateName: 'repos/show/tools'
repoBinding: 'controller.repo'
buildBinding: 'controller.build'
jobBinding: 'controller.job'
tabBinding: 'controller.tab'
currentUserBinding: 'controller.auth.currentUser'
slugBinding: 'controller.repo.slug'
didInsertElement: ->
self = this
$('.menu a').on 'click', ->
self.closeMenu()
willRemoveElement: ->
$('.menu a').off 'click'
closeMenu: ->
$('.menu').removeClass('display')
actions:
menu: ->
@popupCloseAll()
$('#tools .menu').toggleClass('display')
return false
regenerateKeyPopup: ->
if @get('canRegenerateKey')
@set('active', true)
@closeMenu()
@popup('regenerate-key-popup')
return false
regenerateKey: ->
@popupCloseAll()
(@get('repo.content') || @get('repo')).regenerateKey
success: =>
@popup('regeneration-success')
error: ->
Travis.lookup('controller:flash').loadFlashes([{ error: 'Travis encountered an error while trying to regenerate the key, please try again.'}])
canRegenerateKey: (->
@get('hasAdminPermission')
).property('hasAdminPermission')
hasPermission: (->
if permissions = @get('currentUser.permissions')
permissions.contains parseInt(@get('repo.id'))
).property('currentUser.permissions.length', 'repo.id')
hasPushPermission: (->
if permissions = @get('currentUser.pushPermissions')
permissions.contains parseInt(@get('repo.id'))
).property('currentUser.pushPermissions.length', 'repo.id')
hasAdminPermission: (->
if permissions = @get('currentUser.adminPermissions')
permissions.contains parseInt(@get('repo.id'))
).property('currentUser.adminPermissions.length', 'repo.id')
displayRegenerateKey: (->
@get('canRegenerateKey')
).property('canRegenerateKey')
displaySettingsLink: (->
@get('hasPushPermission')
).property('hasPushPermission')
displayCachesLink: (->
@get('hasPushPermission') && config.endpoints.caches
).property('hasPushPermission')
displayStatusImages: (->
@get('hasPermission')
).property('hasPermission')
`export default View`

View File

@ -5,6 +5,7 @@
`import Polling from 'travis/mixins/polling'`
View = BasicView.extend Polling,
popup: Ember.inject.service()
reposBinding: 'controllers.repos'
repoBinding: 'controller.repo'
buildBinding: 'controller.build'
@ -25,7 +26,7 @@ View = BasicView.extend Polling,
actions:
statusImages: () ->
@popupCloseAll()
@get('popup').close()
view = StatusImagesView.create(toolsView: this, container: @container)
BasicView.currentPopupView = view
view.appendTo($('body'))