fixed issue with redis models
This commit is contained in:
@@ -252,10 +252,10 @@ app.user = (function(app){
|
||||
app.util = (function(app){
|
||||
|
||||
function getUrlParameter(name){
|
||||
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
|
||||
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
||||
var results = regex.exec(location.search);
|
||||
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
|
||||
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
|
||||
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
||||
var results = regex.exec(location.search);
|
||||
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
|
||||
};
|
||||
|
||||
function actionMessage(message, $target, type, callback){
|
||||
@@ -277,28 +277,38 @@ app.util = (function(app){
|
||||
})
|
||||
}else{
|
||||
if(type) $target.addClass('bg-' + type);
|
||||
message = '<span class="align-middle">' + message + '</span><button class="action-close btn btn-sm btn-outline-dark float-right"><i class="fa-solid fa-xmark"></i></button>'
|
||||
message = '<span class="align-middle">' + message + '</span><button class="action-close btn btn-sm btn-outline-dark float-end"><i class="fa-solid fa-xmark"></i></button>'
|
||||
$target.html(message).slideDown('fast');
|
||||
}
|
||||
setTimeout(callback,10)
|
||||
}
|
||||
|
||||
$.fn.serializeObject = function(){
|
||||
var
|
||||
arr = $(this).serializeArray(),
|
||||
obj = {};
|
||||
|
||||
for(var i = 0; i < arr.length; i++){
|
||||
if(obj[arr[i].name] === undefined) {
|
||||
obj[arr[i].name] = arr[i].value;
|
||||
} else {
|
||||
if(!(obj[arr[i].name] instanceof Array)) {
|
||||
obj[arr[i].name] = [obj[arr[i].name]];
|
||||
}
|
||||
obj[arr[i].name].push(arr[i].value);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
var
|
||||
arr = $(this).serializeArray(),
|
||||
obj = {};
|
||||
|
||||
for(var i = 0; i < arr.length; i++){
|
||||
if(obj[arr[i].name] === undefined) {
|
||||
if(!arr[i].value) continue;
|
||||
obj[arr[i].name] = arr[i].value;
|
||||
let type = $(this).parent().find(`[name="${arr[i].name}"]`).attr('type');
|
||||
if(['number', 'range'].includes(type)){
|
||||
obj[arr[i].name] = Number(arr[i].value);
|
||||
}
|
||||
|
||||
if(['radio'].includes(type) && ['true', 'false'].includes(arr[i].value)){
|
||||
obj[arr[i].name] = arr[i].value == 'true' ? true : false;
|
||||
}
|
||||
} else {
|
||||
if(!(obj[arr[i].name] instanceof Array)) {
|
||||
obj[arr[i].name] = [obj[arr[i].name]];
|
||||
}
|
||||
obj[arr[i].name].push(arr[i].value);
|
||||
}
|
||||
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -342,12 +352,12 @@ function formAJAX(btn, del){
|
||||
event.preventDefault(); // avoid to execute the actual submit of the form.
|
||||
var $form = $(btn).closest('[action]'); // gets the 'form' parent
|
||||
var formData = $form.find('[name]').serializeObject(); // builds query formDataing
|
||||
var method = $form.attr('method') || 'post';
|
||||
var method = ($form.attr('method') || 'post').toLowerCase();
|
||||
|
||||
// if( !$form.validate()){
|
||||
// app.util.actionMessage('Please fix the form errors.', $form, 'danger')
|
||||
// return false;
|
||||
// }
|
||||
if($form.validate && !$form.validate()){
|
||||
app.util.actionMessage('Please fix the form errors.', $form, 'danger')
|
||||
return false;
|
||||
}
|
||||
|
||||
app.util.actionMessage(
|
||||
'<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>',
|
||||
@@ -357,9 +367,21 @@ function formAJAX(btn, del){
|
||||
|
||||
app.api[method]($form.attr('action'), formData, function(error, data){
|
||||
app.util.actionMessage(data.message, $form, error ? 'danger' : 'success'); //re-populate table
|
||||
$form.validateClear();
|
||||
if(!error){
|
||||
$form.trigger("reset");
|
||||
eval($form.attr('evalAJAX')); //gets JS to run after completion
|
||||
}else{
|
||||
console.log('formAJAX res error', error, data)
|
||||
if(data && data.name === 'ObjectValidateError'){
|
||||
app.util.actionMessage('Please fix the form errors', $form, 'danger'); //re-populate table
|
||||
}
|
||||
if(data && data.keys){
|
||||
console.log('form key errors', data.keys)
|
||||
for(let keyError of data.keys){
|
||||
$form.find(`[name=${keyError.key}]`).validateMessage(keyError.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ MIT license
|
||||
$.scope = {};
|
||||
}
|
||||
|
||||
var make = function( element ){
|
||||
var make = function(element){
|
||||
var result = [];
|
||||
|
||||
result.splice = function(inputValue, ...args){
|
||||
@@ -20,9 +20,9 @@ MIT license
|
||||
|
||||
var index;
|
||||
//if a string is submitted as the index, try to match it to index number
|
||||
if( typeof arguments[0] === 'string' ){
|
||||
if(typeof arguments[0] === 'string'){
|
||||
index = this.indexOf( arguments[0] );//set where to start
|
||||
if ( index === -1 ) {
|
||||
if (index === -1) {
|
||||
return [];
|
||||
}
|
||||
}else{
|
||||
@@ -109,6 +109,7 @@ MIT license
|
||||
//set and return new array
|
||||
return Array.prototype.splice.apply(this, toProto);
|
||||
};
|
||||
|
||||
result.push = function(){
|
||||
//add one or more objects to the array
|
||||
|
||||
@@ -123,19 +124,25 @@ MIT license
|
||||
//return new array length
|
||||
return this.length;
|
||||
};
|
||||
|
||||
result.pop = function(){
|
||||
//remove and return array element
|
||||
|
||||
return this.splice( -1, 1 )[0];
|
||||
};
|
||||
result.reverse = function() {
|
||||
var temp = this.splice( 0 );
|
||||
Array.prototype.reverse.apply( temp );
|
||||
|
||||
for( var i = 0; i < temp.length; i++ ){
|
||||
this.push( temp[i] );
|
||||
result.reverse = function() {
|
||||
let hold = [];
|
||||
for(let item of this){
|
||||
hold.push(item.__jq_$el.html())
|
||||
}
|
||||
|
||||
for(let idx in hold.reverse()){
|
||||
this[idx].__jq_$el.html(hold[idx]);
|
||||
}
|
||||
|
||||
Array.prototype.reverse.apply( this );
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
@@ -149,6 +156,10 @@ MIT license
|
||||
return this.splice( 0, 1 )[0];
|
||||
};
|
||||
|
||||
result.unshift = function(data){
|
||||
return this.splice(0,0, data)
|
||||
}
|
||||
|
||||
result.loop = function(){
|
||||
var temp = this[0];
|
||||
this.splice( 0,1 );
|
||||
@@ -156,13 +167,17 @@ MIT license
|
||||
|
||||
return temp;
|
||||
};
|
||||
|
||||
result.loopUp = function(){
|
||||
var temp = this[this.length-1];
|
||||
this.splice( -1, 1 );
|
||||
this.splice( 0, 0, temp );
|
||||
return temp;
|
||||
};
|
||||
|
||||
result.indexOf = function( key, value ){
|
||||
if(typeof key === 'number') return key;
|
||||
|
||||
if( typeof value !== 'string' ){
|
||||
value = arguments[0];
|
||||
key = this.__index;
|
||||
@@ -175,7 +190,8 @@ MIT license
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
result.update = function( key, value, update ){
|
||||
|
||||
result.update = function(key, value, data){
|
||||
//set variables using sting for index
|
||||
|
||||
// If update is called with no index/key, assume its the 0
|
||||
@@ -186,24 +202,31 @@ MIT license
|
||||
return this.splice(0, 1, key);
|
||||
}
|
||||
|
||||
if( typeof value !== 'string' ){
|
||||
update = arguments[1];
|
||||
value = arguments[0];
|
||||
key = this.__index;
|
||||
if(typeof value !== 'string'){
|
||||
data = arguments[1];
|
||||
if(typeof key !== 'number'){
|
||||
value = arguments[0];
|
||||
key = this.__index;
|
||||
}
|
||||
}
|
||||
|
||||
var index = this.indexOf( key, value );
|
||||
|
||||
if(index === -1) {
|
||||
return [];
|
||||
}
|
||||
var object = $.extend( true, {}, this[index], update );
|
||||
|
||||
var $render = $(Mustache.render(this.__jqTemplate, object));
|
||||
$render.attr('jq-repeat-index', index);
|
||||
this[index].__jq_$el.replaceWith($render);
|
||||
this[index] = $.extend( true, this[index], data );
|
||||
|
||||
var $render = $(Mustache.render(this.__jqTemplate, this[index]));
|
||||
$render.attr('jq-repeat-index', index);
|
||||
|
||||
this.__update(this[index].__jq_$el, $render, this[index], this);
|
||||
this[index].__jq_$el = $render;
|
||||
this.__update(this[index].__jq_$el, this[index], this);
|
||||
};
|
||||
|
||||
result.getByKey = function(key, value){
|
||||
return this[this.indexOf(key, value)];
|
||||
}
|
||||
|
||||
result.__put = function($el, item, list){
|
||||
$el.show();
|
||||
@@ -213,8 +236,8 @@ MIT license
|
||||
$el.remove();
|
||||
};
|
||||
|
||||
result.__update = function($el, item, list){
|
||||
console.log('here', $el)
|
||||
result.__update = function($el, $render, item, list){
|
||||
$el.replaceWith($render);
|
||||
$el.show();
|
||||
};
|
||||
|
||||
|
||||
+53
-70
@@ -1,96 +1,79 @@
|
||||
( function( $ ) {
|
||||
var settings = {
|
||||
rule: {
|
||||
eq: function( value, options ) {
|
||||
var compare = $( '[name=' + options + ']' ).val();
|
||||
eq: function(value, options){
|
||||
var compare = $('[name=' + options + ']').val();
|
||||
|
||||
if ( value != compare ) {
|
||||
return "Miss-match";
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
form: {
|
||||
alertCount: false, //pop-up with error count
|
||||
alertCountMessage: " errors!"
|
||||
},
|
||||
|
||||
processValidation: function ( error_message, $input ) {
|
||||
if ( typeof error_message == 'undefined' || error_message == true ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$( '<b>' ).html( ' - ' + error_message ).appendTo( $input.siblings( 'label' ) );
|
||||
$input.parent().addClass("has-error");
|
||||
failedCount++;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
var failedCount = 0;
|
||||
$.fn.validate = function(event) {
|
||||
// let thisSettings = $.extend(true, settings, settingsObj);
|
||||
let hasErrors = false;
|
||||
|
||||
function processRule( thisSettings, $input ) {
|
||||
var attr = $input.attr( 'validate' ).split( ':' ), //array of params
|
||||
requirement = attr[1],
|
||||
value = $input.val(), //link to input value
|
||||
rule = attr[0];
|
||||
if(this.is('[validate]')) return this.validateField(event);
|
||||
|
||||
$input.siblings( 'label' ).children( 'b' ).remove(); //removes old error
|
||||
$input.parent().removeClass( "has-error" ); //removes has-error class
|
||||
if(!this.attr('isValid')){
|
||||
console.log('adding reset event')
|
||||
this.on('reset', function(){
|
||||
$(this).attr('isValid', false);
|
||||
$(this).validateClear();
|
||||
})
|
||||
}
|
||||
|
||||
this.find('[validate]').each(function(){
|
||||
if(!$(this).validateField()) hasErrors = true;
|
||||
});
|
||||
|
||||
this.attr('isValid', !hasErrors);
|
||||
|
||||
if(hasErrors && event) event.preventDefault();
|
||||
|
||||
return !hasErrors;
|
||||
};
|
||||
|
||||
$.fn.validateClear = function(){
|
||||
$(this).find('input').each(function(){
|
||||
$(this).removeClass('is-invalid');
|
||||
$(this).removeClass('is-valid');
|
||||
})
|
||||
}
|
||||
|
||||
$.fn.validateField = function(){
|
||||
var attr = this.attr('validate').split(':'); //array of params
|
||||
var rule = attr[0];
|
||||
var options = attr[1];
|
||||
var value = this.val(); //link to input value
|
||||
var message;
|
||||
|
||||
//checks if field is required, and length
|
||||
if (isNaN(requirement) === false && requirement && value.length < requirement) {
|
||||
return thisSettings.processValidation( 'Must be ' + requirement + ' characters', $input );
|
||||
if(!isNaN(options) && value.length < options){
|
||||
message = `Must be ${options} characters`;
|
||||
}
|
||||
|
||||
//checks if empty to stop processing
|
||||
if ( isNaN( requirement ) === false && value.length === 0 ) {
|
||||
return;
|
||||
if(!isNaN(options) && value.length === 0) {
|
||||
}else if(rule in settings.rule){
|
||||
let message = settings.rule[rule].apply(this, [value, options]);
|
||||
}
|
||||
|
||||
if ( rule in thisSettings.rule ) {
|
||||
return thisSettings.processValidation( thisSettings.rule[rule].apply( this, [value, requirement] ), $input );
|
||||
}
|
||||
this.validateMessage(message)
|
||||
return !message;
|
||||
}
|
||||
|
||||
$.fn.validate = function( settingsObj, event ) {
|
||||
event = event || window.event;
|
||||
|
||||
failedCount = 0;
|
||||
var thisForm = false,
|
||||
thisSettings = $.extend( true, settings, settingsObj );
|
||||
|
||||
if ( this.is( '[validate]' ) ) {
|
||||
processRule( thisSettings, this );
|
||||
} else {
|
||||
thisForm = true;
|
||||
this.find( '[validate]' ).each( function () {
|
||||
if(!processRule( thisSettings, $( this ) )){
|
||||
// failedCount++;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.attr('isValid', !failedCount);
|
||||
if ( failedCount === 0 ) { //no errors
|
||||
return true;
|
||||
} else { //errors
|
||||
if ( thisForm ){
|
||||
if(thisSettings.form.alertCount){
|
||||
alert( failedCount + thisSettings.form.alertCountMessage );
|
||||
}
|
||||
/* if(event) event.returnValue = false;
|
||||
if(event) event.preventDefault();
|
||||
return false;
|
||||
|
||||
if(event.preventDefault) if(event)*/
|
||||
//event.returnValue = false;
|
||||
event.preventDefault();
|
||||
event.defaultPrevented;
|
||||
|
||||
}
|
||||
return false;
|
||||
$.fn.validateMessage = function(message){
|
||||
if(message && message !== true){
|
||||
this.closest('.form-group').find('b.invalid-feedback').html(message);
|
||||
this.addClass('is-invalid');
|
||||
}else{
|
||||
this.removeClass('is-invalid');
|
||||
this.addClass('is-valid');
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
jQuery.extend({
|
||||
|
||||
Reference in New Issue
Block a user