110 lines
2.3 KiB
JavaScript
110 lines
2.3 KiB
JavaScript
|
|
/**
|
||
|
|
* JS for adding a Contact to a company.
|
||
|
|
*
|
||
|
|
* 1. open a new model window on the add form.
|
||
|
|
*
|
||
|
|
* 2. When the submit button is clicked, check it for errors
|
||
|
|
*
|
||
|
|
* 3. If it passes the first validation, submit it to PHP controller via AJAX.
|
||
|
|
*
|
||
|
|
* 4a. If PHP reports sucessful save
|
||
|
|
* i. close the Modal Window
|
||
|
|
* ii. add the new contacts user details to the contact table.
|
||
|
|
*
|
||
|
|
* 4b. If PHP reports validation error. Display error. Set error class to the fields that failed.
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
|
||
|
|
$(function() {
|
||
|
|
|
||
|
|
|
||
|
|
//Style the Button
|
||
|
|
$( "#addUser" ).button({
|
||
|
|
text: true,
|
||
|
|
icons: {
|
||
|
|
primary: "ui-icon-plus"
|
||
|
|
}
|
||
|
|
|
||
|
|
})
|
||
|
|
|
||
|
|
//The diaglog box for Adding a contact.
|
||
|
|
$("#addUserDiv").dialog({
|
||
|
|
autoOpen: false,
|
||
|
|
width: 450,
|
||
|
|
modal: true,
|
||
|
|
title: 'Add a Contact'
|
||
|
|
});
|
||
|
|
|
||
|
|
$("#addUser").click(function() {
|
||
|
|
$("#addUserDiv").dialog('open');
|
||
|
|
return false;
|
||
|
|
});
|
||
|
|
|
||
|
|
|
||
|
|
var _loadingDiv = $("#ajaxLoading");
|
||
|
|
|
||
|
|
$('#UserAddForm, #UserEditForm').submit(function(){
|
||
|
|
_loadingDiv.show();
|
||
|
|
$.post('/users/add_user',
|
||
|
|
$(this).serializeArray(),
|
||
|
|
afterValidate,
|
||
|
|
"json"
|
||
|
|
);
|
||
|
|
return false;
|
||
|
|
});
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Post-submit callback
|
||
|
|
function afterValidate(data, status) {
|
||
|
|
$(".message").remove();
|
||
|
|
$(".error-message").remove();
|
||
|
|
|
||
|
|
if (data.errors) {
|
||
|
|
onError(data.errors);
|
||
|
|
} else if (data.success) {
|
||
|
|
onSuccess(data.success);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function onSuccess(data) {
|
||
|
|
flashMessage(data.message);
|
||
|
|
_loadingDiv.hide();
|
||
|
|
window.setTimeout(function() {
|
||
|
|
// window.location.href = '/posts';
|
||
|
|
window.location.reload(true);
|
||
|
|
}, 2000);
|
||
|
|
};
|
||
|
|
|
||
|
|
function onError(data) {
|
||
|
|
flashMessage(data.message);
|
||
|
|
$.each(data.data, function(model, errors) {
|
||
|
|
for (fieldName in this) {
|
||
|
|
var element = $("#" + camelize(model + '_' + fieldName));
|
||
|
|
var _insert = $(document.createElement('div')).insertAfter(element);
|
||
|
|
_insert.addClass('error-message').text(this[fieldName])
|
||
|
|
}
|
||
|
|
_loadingDiv.hide();
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
function flashMessage(message) {
|
||
|
|
var _insert = $(document.createElement('div')).css('display', 'none');
|
||
|
|
_insert.attr('id', 'flashMessage').addClass('message').text(message);
|
||
|
|
_insert.insertAfter($("#UserAddForm")).fadeIn();
|
||
|
|
}
|
||
|
|
|
||
|
|
function camelize(string) {
|
||
|
|
var a = string.split('_'), i;
|
||
|
|
s = [];
|
||
|
|
for (i=0; i<a.length; i++){
|
||
|
|
s.push(a[i].charAt(0).toUpperCase() + a[i].substring(1));
|
||
|
|
}
|
||
|
|
s = s.join('');
|
||
|
|
return s;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
});
|
||
|
|
|