Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into right-align-what-ar…
Browse files Browse the repository at this point in the history
…e-bookshelves
  • Loading branch information
maxstarkenburg committed Feb 18, 2014
2 parents a2e6ff5 + c766cc8 commit edc7d66
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "scripts/main.js",
"dependencies": {
"bookish": "oerpub/bookish#master",
"octokit": "0.9.12"
"octokit": "0.9.16"
},
"ignore": [
"**/.*",
Expand Down
3 changes: 3 additions & 0 deletions scripts/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ require.config
bootstrapTooltip: "#{BOWER}/bootstrap/js/bootstrap-tooltip"
bootstrapTransition: "#{BOWER}/bootstrap/js/bootstrap-transition"
bootstrapTypeahead: "#{BOWER}/bootstrap/js/bootstrap-typeahead"
bootstrapTags: "#{BOWER}/bootstrapTags/dist/bootstrap-tagsinput.min"
bootstrapTagsCss: "#{BOWER}/bootstrapTags/dist/bootstrap-tagsinput"

# ## Handlebars Dependencies
Handlebars: "#{BOWER}/require-handlebars-plugin/Handlebars"
Expand Down Expand Up @@ -127,6 +129,7 @@ require.config
bootstrapTooltip: ['jquery']
bootstrapTransition: ['jquery']
bootstrapTypeahead: ['jquery']
bootstrapTags: ['jquery', 'css!bootstrapTagsCss']

# Select2
select2:
Expand Down
30 changes: 28 additions & 2 deletions scripts/gh-book/auth-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ <h3 id="sign-in-modal-label">Sign in to Book Editor</h3>
<p>To be able to save your changes, you must provide us with your
<a href="http://github.com/">github</a> username and password.</p>

<p>If you refresh this page you may need to re-enter this information.</p>
<p id="login-info-wrapper">If you refresh this page you may need to re-enter this information.</p>
<div id="login-advanced-wrapper">
<div class="form-inline">
<input id="github-id" class="input-small" type="text" required="true" placeholder="Username" value="{{id}}"/>
Expand Down Expand Up @@ -80,7 +80,7 @@ <h3 id="sign-in-modal-label">Sign in to Book Editor</h3>
<div id="fork-book-modal" data-backdrop="false" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="fork-book-modal-label" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="fork-book-modal-label">Copy this book!</h3>
<h3 id="fork-book-modal-label">Copy this shelf!</h3>
</div>
<h4>&#160; Places I have permission to make changes:</h4>
<div class="modal-body">
Expand All @@ -93,6 +93,32 @@ <h4>&#160; Places I have permission to make changes:</h4>
</div>
</div>

<div id="fork-progress-modal" data-backdrop="false" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="fork-progress-modal-label" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="fork-progress-modal-label">Copying in progress</h3>
</div>
<div class="modal-body">
[Progress indicator here]
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
</div>
</div>

<div id="fork-redirect-modal" data-backdrop="false" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="fork-redirect-modal-label" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="fork-redirect-modal-label">Existing bookshelf</h3>
</div>
<div class="modal-body">
You already have a copy of this bookshelf.
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<button class="btn btn-primary">Take me there</button>
</div>
</div>


<!-- Edit Repository Modal -->
Expand Down
130 changes: 119 additions & 11 deletions scripts/gh-book/auth.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
define [
'jquery'
'underscore'
'marionette'
'cs!collections/content'
'cs!session'
Expand All @@ -10,7 +11,7 @@ define [
'cs!configs/github'
'bootstrapModal'
'bootstrapCollapse'
], ($, Marionette, allContent, session, remoteUpdater, authTemplate, difflib, diffview, config) ->
], ($, _, Marionette, allContent, session, remoteUpdater, authTemplate, difflib, diffview, config) ->

return class GithubAuthView extends Marionette.ItemView
template: authTemplate
Expand All @@ -25,6 +26,8 @@ define [
'click #show-diffs': 'showDiffsModal'
'submit #edit-repo-form': 'editRepo'
'click [data-select-repo]': 'selectRepo'
'click #fork-book-modal .organisation-block': 'selectOrg'
'click #fork-redirect-modal .btn-primary': 'selectFork'
'click [data-create-repo]': 'createRepo'
'shown #edit-repo-modal': 'createRepoModal'

Expand Down Expand Up @@ -77,18 +80,39 @@ define [
@isDirty = true
@render()

signInModal: () ->
signInModal: (options) ->
$modal = @$el.find('#sign-in-modal')

# The hidden event on #login-advanced should not propagate
$modal.find('#login-advanced').on 'hidden', (e) => e.stopPropagation()

# We'll return a promise, and resolve it upon login or close.
promise = $.Deferred()
$modal.data('login-promise', promise)

# attach a close listener
$modal.on 'hidden', () => @trigger 'close'
$modal.on 'hidden', () =>
if promise.state() == 'pending'
promise.reject()
@trigger 'close'

# Hide parts of the modal, if requested, for a simpler UI.
if options
if options.anonymous != undefined and options.anonymous == false
$modal.find('#login-anonymous').hide()
else
$modal.find('#login-anonymous').show()

if options.info != undefined and options.info == false
$modal.find('#login-info-wrapper').hide()
else
$modal.find('#login-info-wrapper').show()

# Show the modal
$modal.modal {show:true}

return promise

# Show a diff of all unsaved models
showDiffsModal: () ->
$modal = @$el.find('#diffs-modal')
Expand Down Expand Up @@ -134,17 +158,97 @@ define [
$modal.modal {show:true}


forkContent: () ->
organisationModal: (info, orgs) ->
$modal = @$el.find('#fork-book-modal')
$body = $modal.find('.modal-body').empty()

if not (@model.get('password') or @model.get('token'))
alert 'Please Sign In before trying to fork a book'
return
# Own account
$block = $('<div class="organisation-block"></div>')
$avatar = $('<img alt="avatar">').attr('src', info.avatar_url)
$name = $('<span>').html(info.login)
$block.append($avatar).append($name).data('org-name', info.login)
$body.append($block)
for org in orgs
$block = $('<div class="organisation-block"></div>')
$avatar = $('<img alt="avatar">').attr('src', org.avatar_url)
$name = $('<span>').html(org.login)
$block.append($avatar).append($name).data('org-name', org.login)
$body.append($block)

@model.getClient().getLogin().done (login) =>
@model.getRepo()?.fork().done () =>
@model.set 'repoUser', login
$modal.modal {show:true}


selectOrg: (e) ->
$block = $(e.target).addBack().closest('.organisation-block')
org = $block.data('org-name') or null
@$el.find('#fork-book-modal').modal('hide')
@__forkContent(org)

selectFork: (e) ->
e.preventDefault()
login = @$el.find('#fork-redirect-modal').modal('hide').data('login')
@_selectRepo(login, @model.get('repoName'))

forkContent: () ->
if not (@model.get('password') or @model.get('token'))
@signInModal
anonymous: false
info: false
.done () => @_forkContent()
return
@_forkContent()

_forkContent: () ->
# If user has more than one organisation, ask which one.
@model.getClient().getUser().getInfo().done (userinfo) =>
@model.getClient().getUser().getOrgs().done (orgs) =>
if orgs.length > 1
@organisationModal(userinfo, orgs)
else
@__forkContent(userinfo.login)

__forkContent: (login) ->
# If repo exists, go to it or cancel. Else fork.
@model.getClient().getRepo(login, @model.get('repoName')).getInfo().done () =>
@$el.find('#fork-redirect-modal').data('login', login).modal
show: true
.fail () =>
@___forkContent(login)

___forkContent: (org) ->
$modal = @$el.find('#fork-progress-modal')
$body = $modal.find('.modal-body')
$body.html('Creating a Fork...')
$modal.modal {show: true}

# If the chosen organisation is myself, leave it out.
@model.getRepo()?.fork(org != @model.get('id') and org or null).done () =>
$body.html('Waiting for Fork to become available...')

# Change upstream repo
wait = 2000
@model.set 'repoUser', org

# Poll until repo becomes available
pollRepo = () =>
@model.getRepo()?.getInfo().done (info) =>
require ['backbone', 'cs!controllers/routing'], (bb, controller) =>
# Filter out the view bit, then set the url to reflect the fork
v = RegExp('repo/[^/]*/[^/]*(/branch/[^/]*)?/(.*)').exec(
bb.history.getHash())[2]
controller.trigger 'navigate', v
.fail () =>
if wait < 30
setTimeout(pollRepo, wait)
wait = wait * 2 # exponential backoff
else
alert('Fork failed')
.always () =>
# Leave it open for another two seconds, because sometimes
# the fork happens so quickly that people are unnerved by
# the flashing modal they couldn't read.
window.setTimeout((() -> $modal.modal('hide')), 2000)
pollRepo()


signIn: (e) ->
# Prevent form submission
Expand All @@ -156,6 +260,9 @@ define [
token: @$el.find('#github-token').val()
password: @$el.find('#github-password').val()

# signInModal persists the promise on the modal
promise = @$el.find('#sign-in-modal').data('login-promise')

if not (attrs.password or attrs.token)
alert 'We are terribly sorry but github recently changed so you must login to use their API.\nPlease refresh and provide a password or an OAuth token.'
else
Expand All @@ -166,6 +273,7 @@ define [
# The 1st time the editor loads up it waits for the modal to close
# but `render` will hide the modal without triggering 'close'
@trigger 'close'
promise.resolve()
.fail (err) =>
alert 'Login failed. Did you use the correct credentials?'

Expand Down
13 changes: 13 additions & 0 deletions scripts/gh-book/gh-book.less
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,16 @@ nav#workspace-sidebar-toc{
font-size: 120%;
}
}

#fork-book-modal {
.organisation-block {
cursor: pointer;
img {
height: 45px;
width: 45px;
}
span {
margin-left: 0.5em;
}
}
}
Loading

0 comments on commit edc7d66

Please sign in to comment.