Created
March 18, 2014 20:00
-
-
Save kirs/9628225 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
var projectForm = $(".form-repo") | |
var repoOwnerField = projectForm.find("select[name=owner]") | |
var repoNameField = projectForm.find("select[name=name]") | |
if(projectForm.length == 0) { | |
return | |
} | |
repoNameField.selectize({ | |
valueField: 'name', | |
labelField: 'name', | |
searchField: ['name'], | |
create: true | |
}) | |
repoOwnerField.selectize({ | |
valueField: 'name', | |
labelField: 'name', | |
searchField: ['name'], | |
create: true, | |
preload: true, | |
load: function(query, callback) { | |
var control = repoOwnerField[0].selectize | |
control.disable() | |
repoNameField[0].selectize.disable() | |
$.ajax({ | |
url: '/new/github.com/available_orgs', | |
type: 'GET', | |
error: function() { | |
callback(); | |
}, | |
success: function(orgs) { | |
orgs = $.map(orgs, function(o) { | |
return { name: o } | |
}) | |
control.enable() | |
callback(orgs) | |
} | |
}) | |
} | |
}) | |
repoOwnerField.on('change', function() { | |
control = repoNameField[0].selectize | |
control.disable() | |
control.clearOptions() | |
orgname = repoOwnerField.val() | |
if(orgname == "") return | |
$.get('/new/github.com/available_repos', | |
{ org: orgname }, | |
function(repos) { | |
control.enable() | |
$.each(repos, function(i, r) { | |
control.addOption({ | |
name: r.name | |
}); | |
}) | |
if(repos.length > 0) { | |
control.open() | |
} | |
} | |
) | |
}) | |
projectForm.on('submit', function() { | |
$("#successAlert").hide(); | |
$("#failureAlert").hide(); | |
$('#submitButton').button('loading'); | |
$.ajax({ | |
type: 'POST', | |
url: projectForm.attr("target"), | |
data: projectForm.serialize(), | |
success: function(response, status) { | |
var name = repoNameField.val() | |
var owner = repoOwnerField.val() | |
var domain = $("input[name=domain]").val() | |
window.location.pathname = "/" + domain + "/"+owner+"/"+name | |
}, | |
error: function() { | |
$("#failureAlert").text("Unable to setup the Repository"); | |
$("#failureAlert").show().removeClass("hide"); | |
$('#submitButton').button('reset'); | |
} | |
}); | |
return false; | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AddGithubRepo = { | |
formSelector: ".form-repo", | |
ownerFieldSelector: "select[name=owner]", | |
nameFieldSelector: "select[name=name]", | |
orgsUrl: '/new/github.com/available_orgs', | |
reposUrl: '/new/github.com/available_repos', | |
selectize: function() { | |
var that = this | |
this.nameField.selectize({ | |
valueField: 'name', | |
labelField: 'name', | |
searchField: ['name'], | |
create: true | |
}) | |
this.ownerField.selectize({ | |
valueField: 'name', | |
labelField: 'name', | |
searchField: ['name'], | |
create: true, | |
preload: true, | |
load: function(query, callback) { | |
var control = that.ownerField[0].selectize | |
control.disable() | |
that.nameField[0].selectize.disable() | |
$.ajax({ | |
url: that.orgsUrl, | |
type: 'GET', | |
error: function() { | |
callback(); | |
}, | |
success: function(orgs) { | |
orgs = $.map(orgs, function(o) { | |
return { name: o } | |
}) | |
control.enable() | |
callback(orgs) | |
} | |
}) | |
} | |
}) | |
this.bindSwitcher() | |
}, | |
bindSwitcher: function() { | |
var that = this | |
this.ownerField.on('change', function() { | |
control = that.nameField[0].selectize | |
control.disable() | |
control.clearOptions() | |
orgname = that.ownerField.val() | |
if(orgname == "") return | |
$.get(that.reposUrl, | |
{ org: orgname }, | |
function(repos) { | |
control.enable() | |
$.each(repos, function(i, r) { | |
control.addOption({ | |
name: r.name | |
}); | |
}) | |
if(repos.length > 0) { | |
control.open() | |
} | |
} | |
) | |
}) | |
}, | |
validation: function() { | |
var that = this | |
this.form.on('submit', function() { | |
$("#successAlert").hide(); | |
$("#failureAlert").hide(); | |
$('#submitButton').button('loading'); | |
$.ajax({ | |
type: 'POST', | |
url: that.form.attr("target"), | |
data: that.form.serialize(), | |
success: function(response, status) { | |
var name = that.nameField.val() | |
var owner = that.ownerField.val() | |
var domain = $("input[name=domain]").val() | |
window.location.pathname = "/" + domain + "/"+owner+"/"+name | |
}, | |
error: function() { | |
$("#failureAlert").text("Unable to setup the Repository"); | |
$("#failureAlert").show().removeClass("hide"); | |
$('#submitButton').button('reset'); | |
} | |
}); | |
return false; | |
}) | |
}, | |
start: function() { | |
this.form = $(this.formSelector) | |
if(this.form.length == 0) { | |
return | |
} | |
this.nameField = this.form.find(that.nameFieldSelector) | |
this.ownerField = this.form.find(that.ownerFieldSelector) | |
this.validation() | |
this.selectize() | |
} | |
} | |
$(function() { | |
AddGithubRepo.start() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment