Skip to content

Instantly share code, notes, and snippets.

@stovak
Last active August 29, 2015 14:04
Show Gist options
  • Save stovak/e75f946cde61c46e0f7f to your computer and use it in GitHub Desktop.
Save stovak/e75f946cde61c46e0f7f to your computer and use it in GitHub Desktop.
'use strict';
/*global $:false */
/*global FastClick:false */
/*jshint unused:vars */
/*jshint quotmark:false */
angular.module('license')
.controller('NameCheck', ['$scope', '$window', 'License', '$http', '$modal',
function ($scope, $window, License, $http, $modal) {
$scope.showStep = function($step, $http) {
$('.step').removeClass('active');
$('#'+$step).addClass('active');
$('#signup-current-step-num').html($('.step.active').attr('id').replace('-', 'fa-pencil'));
};
$scope.toggleSpinner = {
on: function() {
$("#check-name-textfield-addon span").removeClass('fa-pencil').addClass(['fa-spinner', 'fa-spin']);
},
off: function() {
$("#check-name-textfield-addon span").removeClass(['fa-spinner', 'fa-spin']).addClass('fa-pencil');
}
};
$scope.userMessage = function(message) {
if ($('form:visible #response-panel').length === 0) {
$($(document.createElement("div")).attr({
'class': 'panel',
'id': 'response-panel'
})).appendTo("form:visible");
}
$('form:visible #response-panel').html(message);
return this;
};
$scope.License = License;
angular.element(document).ready(function(){
$http.bridge();
console.log('Angular dom Ready');
FastClick.attach(document.body);
// initialize connection with JS Bridge
});
$scope.suggestions = function(suggestions) {
console.log('Suggestions...');
if ($scope.toggleSpinner !== undefined) {
$scope.toggleSpinner.on();
}
if (suggestions === undefined) {
suggestions = $('form:visible input[type="text"]').val();
}
console.log(suggestions);
$scope.license.getSuggestions(suggestions).then(function(response){
console.log("getSuggestions=>Then");
console.log(response.suggestions);
var message = '';
for (var i in response.suggestions) {
message += '<button type="button" ng-click="changeDesiredUsername(\''+response.suggestions[i]+'\')" class="button button-sm">'+response.suggestions[i]+'</button>';
}
console.log("Suggestion Buttons:");
console.log(message);
$scope.userMessage(message);
}, function(config) {
console.log("Suggestions Reject");
console.log(config);
});
};
$scope.platform = navigator.platform;
$scope.createLicenseForExistingUsername = function() {
console.log("==>createLicenseForExistingUsername");
console.log(this);
this.license = new this.License({'existingUsername': this.existingUsername});
this.showStep('step-2');
this.checkUsername();
};
$scope.createNewEmptyLicense = function() {
this.license = new this.License();
this.showStep('step-2');
};
$scope.checkUsername = function() {
$scope.license.usernameExists().then(function(data){
console.log("UsernameExists=>Then");
console.log(arguments);
if (data.exists) {
$scope.userMessage('username exists: fetching suggestions...');
$scope.suggestions();
} else {
$scope.userMessage('username does not exist. Reserving...');
$scope.myModal = $modal({title: 'Create your account using Facebook or an Email Address?', content: '', show: true});
}
}, function(){
console.log("UserNameExistsReject");
console.log(arguments);
});
console.log("==>checkUsernameResults:");
console.log($scope.license);
};
$scope.changeDesiredUsername = function(newName) {
console.log(arguments);
$scope.license.existingUsername = newName;
$scope.checkUsername();
};
$scope.existingUsername = 'stovak';
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment