Skip to content

Commit

Permalink
chore(release): update dist
Browse files Browse the repository at this point in the history
  • Loading branch information
lubber-de committed Oct 9, 2022
1 parent 7b23222 commit 4a130c2
Show file tree
Hide file tree
Showing 176 changed files with 25,885 additions and 30,884 deletions.
125 changes: 92 additions & 33 deletions definitions/behaviors/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ $.api = $.fn.api = function(parameters) {

// context used for state
$context = (settings.stateContext)
? $(settings.stateContext)
? ([window,document].indexOf(settings.stateContext) < 0 ? $(document).find(settings.stateContext) : $(settings.stateContext))
: $module,

// request details
Expand All @@ -74,6 +74,7 @@ $.api = $.fn.api = function(parameters) {
url,
data,
requestStartTime,
originalData,

// standard module
element = this,
Expand All @@ -86,6 +87,7 @@ $.api = $.fn.api = function(parameters) {

initialize: function() {
if(!methodInvoked) {
originalData = settings.data;
module.bind.events();
}
module.instantiate();
Expand Down Expand Up @@ -132,7 +134,7 @@ $.api = $.fn.api = function(parameters) {
response = JSON.parse(response);
}
catch(e) {
// isnt json string
// isn't json string
}
}
return response;
Expand All @@ -148,8 +150,8 @@ $.api = $.fn.api = function(parameters) {
module.error(error.noStorage);
return;
}
response = sessionStorage.getItem(url);
module.debug('Using cached response', url, response);
response = sessionStorage.getItem(url + module.get.normalizedData());
module.debug('Using cached response', url, settings.data, response);
response = module.decode.json(response);
return response;
}
Expand All @@ -167,8 +169,8 @@ $.api = $.fn.api = function(parameters) {
if( $.isPlainObject(response) ) {
response = JSON.stringify(response);
}
sessionStorage.setItem(url, response);
module.verbose('Storing cached response for url', url, response);
sessionStorage.setItem(url + module.get.normalizedData(), response);
module.verbose('Storing cached response for url', url, settings.data, response);
}
},

Expand Down Expand Up @@ -197,7 +199,7 @@ $.api = $.fn.api = function(parameters) {

// Add form content
if(settings.serializeForm) {
settings.data = module.add.formData(settings.data);
settings.data = module.add.formData(originalData || settings.data);
}

// call beforesend and get any settings changes
Expand Down Expand Up @@ -334,10 +336,6 @@ $.api = $.fn.api = function(parameters) {
cancelled: function() {
return (module.cancelled || false);
},
succesful: function() {
module.verbose('This behavior will be deleted due to typo. Use "was successful" instead.');
return module.was.successful();
},
successful: function() {
return (module.request && module.request.state() == 'resolved');
},
Expand Down Expand Up @@ -365,8 +363,8 @@ $.api = $.fn.api = function(parameters) {
var
// allow legacy {$var} style
variable = (templatedString.indexOf('$') !== -1)
? templatedString.substr(2, templatedString.length - 3)
: templatedString.substr(1, templatedString.length - 2),
? templatedString.slice(2, -1)
: templatedString.slice(1, -1),
value = ($.isPlainObject(urlData) && urlData[variable] !== undefined)
? urlData[variable]
: ($module.data(variable) !== undefined)
Expand Down Expand Up @@ -397,8 +395,8 @@ $.api = $.fn.api = function(parameters) {
var
// allow legacy {/$var} style
variable = (templatedString.indexOf('$') !== -1)
? templatedString.substr(3, templatedString.length - 4)
: templatedString.substr(2, templatedString.length - 3),
? templatedString.slice(3, -1)
: templatedString.slice(2, -1),
value = ($.isPlainObject(urlData) && urlData[variable] !== undefined)
? urlData[variable]
: ($module.data(variable) !== undefined)
Expand Down Expand Up @@ -429,24 +427,76 @@ $.api = $.fn.api = function(parameters) {
},
formData: function(data) {
var
canSerialize = ($.fn.serializeObject !== undefined),
formData = (canSerialize)
? $form.serializeObject()
: $form.serialize(),
hasOtherData
formData = {},
hasOtherData,
useFormDataApi = settings.serializeForm === 'formdata'
;
data = data || settings.data;
data = data || originalData || settings.data;
hasOtherData = $.isPlainObject(data);

if (useFormDataApi) {
formData = new FormData($form[0]);
settings.processData = typeof settings.processData !== 'undefined' ? settings.processData : false;
settings.contentType = typeof settings.contentType !== 'undefined' ? settings.contentType : false;
} else {
var formArray = $form.serializeArray(),
pushes = {},
pushValues= {},
build = function(base, key, value) {
base[key] = value;
return base;
}
;
// add files
$.each($('input[type="file"]',$form), function(i, tag) {
$.each($(tag)[0].files, function(j, file) {
formArray.push({name:tag.name, value: file});
});
});
$.each(formArray, function(i, el) {
if (!settings.regExp.validate.test(el.name)) return;
var isCheckbox = $('[name="' + el.name + '"]', $form).attr('type') === 'checkbox',
floatValue = parseFloat(el.value),
value = (isCheckbox && el.value === 'on') || el.value === 'true' || (String(floatValue) === el.value ? floatValue : (el.value === 'false' ? false : el.value)),
nameKeys = el.name.match(settings.regExp.key) || [], k, pushKey= el.name.replace(/\[\]$/,'')
;
if(!(pushKey in pushes)) {
pushes[pushKey] = 0;
pushValues[pushKey] = value;
} else if (Array.isArray(pushValues[pushKey])) {
pushValues[pushKey].push(value);
} else {
pushValues[pushKey] = [pushValues[pushKey] , value];
}
value = pushValues[pushKey];

while ((k = nameKeys.pop()) !== undefined) {
// foo[]
if (k == '' && !Array.isArray(value)){
value = build([], pushes[pushKey]++, value);
}
// foo[n]
else if (settings.regExp.fixed.test(k)) {
value = build([], k, value);
}
// foo; foo[bar]
else if (settings.regExp.named.test(k)) {
value = build({}, k, value);
}
}
formData = $.extend(true, formData, value);
});
}

if(hasOtherData) {
if(canSerialize) {
module.debug('Extending existing data with form data', data, formData);
data = $.extend(true, {}, data, formData);
}
else {
module.error(error.missingSerialize);
module.debug('Cant extend data. Replacing data with form data', data, formData);
module.debug('Extending existing data with form data', data, formData);
if(useFormDataApi) {
$.each(Object.keys(data),function(i, el){
formData.append(el, data[el]);
});
data = formData;
} else {
data = $.extend(true, {}, data, formData);
}
}
else {
Expand Down Expand Up @@ -698,6 +748,9 @@ $.api = $.fn.api = function(parameters) {
},

get: {
normalizedData: function(){
return typeof settings.data === "string" ? settings.data : JSON.stringify(settings.data, Object.keys(settings.data).sort());
},
responseFromXHR: function(xhr) {
return $.isPlainObject(xhr)
? (module.is.expectingJSON())
Expand Down Expand Up @@ -962,7 +1015,7 @@ $.api = $.fn.api = function(parameters) {
response
;
passedArguments = passedArguments || queryArguments;
context = element || context;
context = context || element;
if(typeof query == 'string' && object !== undefined) {
query = query.split(/[\. ]/);
maxDepth = query.length - 1;
Expand Down Expand Up @@ -1083,6 +1136,8 @@ $.api.settings = {
defaultData : true,

// whether to serialize closest form
// use true to convert complex named keys like a[b][1][c][] into a nested object
// use 'formdata' for formdata web api
serializeForm : false,

// how long to wait before request should occur
Expand All @@ -1105,7 +1160,7 @@ $.api.settings = {
responseAsync : false,

// whether onResponse should work with response value without force converting into an object
rawResponse : false,
rawResponse : true,

// callbacks before request
beforeSend : function(settings) { return settings; },
Expand Down Expand Up @@ -1141,7 +1196,6 @@ $.api.settings = {
legacyParameters : 'You are using legacy API success callback names',
method : 'The method you called is not defined',
missingAction : 'API action used but no url was defined',
missingSerialize : 'jquery-serialize-object is required to add form data to an existing data object',
missingURL : 'No URL specified for api event',
noReturnedValue : 'The beforeSend callback must return a settings object, beforeSend ignored.',
noStorage : 'Caching responses locally requires session storage',
Expand All @@ -1152,8 +1206,13 @@ $.api.settings = {
},

regExp : {
required : /\{\$*[A-z0-9]+\}/g,
optional : /\{\/\$*[A-z0-9]+\}/g,
required : /\{\$*[a-z0-9]+\}/gi,
optional : /\{\/\$*[a-z0-9]+\}/gi,
validate: /^[a-z_][a-z0-9_-]*(?:\[[a-z0-9_-]*\])*$/i,
key: /[a-z0-9_-]+|(?=\[\])/gi,
push: /^$/,
fixed: /^\d+$/,
named: /^[a-z0-9_-]+$/i
},

className: {
Expand Down
Loading

0 comments on commit 4a130c2

Please sign in to comment.