Skip to content
This repository has been archived by the owner on Mar 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #23 from arkytn/id_option
Browse files Browse the repository at this point in the history
Id option
  • Loading branch information
BenGriffiths authored Mar 14, 2017
2 parents 906117a + 97ec8a3 commit 0be285c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Save As You Type

* ##### Update v.1.4.5 #####
Added "id" to settings so you can allow several forms to share a common form save.

* ##### Update v.1.4.4 #####
Various bug fixes have been implemented. Also, thanks to @npostman you can now specify a prefix to the cookie name to help you have greater control over your code.

Expand Down Expand Up @@ -45,6 +48,7 @@ Default's:
* recover: false
* autorecover: true
* checksaveexists: false - (Returns true or false)
* id: this.attr('id') (defaults to form id)

```js
$(function()
Expand Down Expand Up @@ -72,7 +76,26 @@ $(function()
*/
$('#form_id').sayt({'autosave': false, 'autorecover': false, 'days': 7});


/*
* Override form id so multiple forms can share one save.
* Useful for initialyzing fields in multiple forms or on different pages
* Or "wizard" style forms where an initial form's values are carried forward
* to the next form in the sequence.
*
* Example:
* The first line will remember the fields typed in the first blank form of class "form_class".
* The second and third line will remember the state of a specific form.
*
* When the second blank form is opened it is first initialized with "form_class"
* and then by its own specific id which being blank does nothing but take on the
* initial values.
* From then on each form remembers it's own values because the second line always
* overwrites the first line.
*/
$('.form_class').sayt({ 'id': 'common' }); //class specific cookie id = prefix + 'common'
$('#form_id_1').sayt(); //id specific cookie id = prefix + 'form_id_1'
$('#form_id_2').sayt(); //id specific cookie id = prefix + 'form_id_2'

/*
* Check to see if a form has a save
*/
Expand Down
13 changes: 7 additions & 6 deletions source/sayt.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
'recover' : false,
'autorecover' : true,
'checksaveexists': false,
'exclude' : []
'exclude' : [],
'id' : this.attr('id'),
}, options);

/*
Expand All @@ -82,7 +83,7 @@
/*
* Define the cookie name
*/
var cookie_id = settings.prefix + theform.attr('id');
var cookie_id = settings.prefix + settings.id;

/*
* Erase a cookie
Expand All @@ -93,9 +94,9 @@
if (typeof(Storage) !== "undefined") {
localStorage.removeItem(cookie_id);
}
else {
$.cookie(cookie_id, null);
}
// else {
// $.cookie(cookie_id, null);
// }

return true;
}
Expand Down Expand Up @@ -236,7 +237,7 @@
cookieString = cookieString + field.name + ':::--FIELDANDVARSPLITTER--:::' + field.value + ':::--FORMSPLITTERFORVARS--:::';
});

$.cookie(cookie_id, cookieString, { expires: settings['days'] });
// $.cookie(cookie_id, cookieString, { expires: settings['days'] });
if (typeof(Storage) !== "undefined") {
localStorage.setItem(cookie_id, cookieString);
}
Expand Down
2 changes: 1 addition & 1 deletion source/sayt.min.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
*
*******************************************************************************
*/
(function(e){e.fn.sayt=function(t){function l(t){var r="";jQuery.each(t,function(e,t){r=r+t.name+":::--FIELDANDVARSPLITTER--:::"+t.value+":::--FORMSPLITTERFORVARS--:::"});e.cookie(s,r,{expires:n["days"]});if(typeof Storage!=="undefined"){localStorage.setItem(s,r)}else{e.cookie(s,r,{expires:n["days"]})}}function c(e,t,n){var r=(e+"").indexOf(t,n||0);return r===-1?false:r}function h(t,n){var r=e.extend({},t);var s=r.find("[data-sayt-exclude]");s.remove();for(i in n){s=r.find(n[i]);s.remove()}var o=r.serializeArray();return o}var n=e.extend({prefix:"autosaveFormCookie-",erase:false,days:3,autosave:true,savenow:false,recover:false,autorecover:true,checksaveexists:false,exclude:[]},t);var r=this;var s=n.prefix+r.attr("id");if(n["erase"]==true){e.cookie(s,null);if(typeof Storage!=="undefined"){localStorage.removeItem(s)}else{e.cookie(s,null)}return true}var o;if(typeof Storage!=="undefined"){o=localStorage.getItem(s)}else{o=e.cookie(s)}if(n["checksaveexists"]==true){if(o){return true}else{return false}return false}if(n["savenow"]==true){var u=h(r,n["exclude"]);l(u);return true}if(n["autorecover"]==true||n["recover"]==true){if(o){var a=o.split(":::--FORMSPLITTERFORVARS--:::");var f={};e.each(a,function(t,n){var r=n.split(":::--FIELDANDVARSPLITTER--:::");if(e.trim(r[0])!=""){if(e.trim(r[0])in f){f[e.trim(r[0])]=f[e.trim(r[0])]+":::--MULTISELECTSPLITTER--:::"+r[1]}else{f[e.trim(r[0])]=r[1]}}});e.each(f,function(t,n){if(c(n,":::--MULTISELECTSPLITTER--:::")>0){var r=n.split(":::--MULTISELECTSPLITTER--:::");e.each(r,function(n,r){e('input[name="'+t+'"], select[name="'+t+'"], textarea[name="'+t+'"]').find('[value="'+r+'"]').prop("selected",true);e('input[name="'+t+'"][value="'+r+'"], select[name="'+t+'"][value="'+r+'"], textarea[name="'+t+'"][value="'+r+'"]').prop("checked",true)})}else{e('input[name="'+t+'"], select[name="'+t+'"], textarea[name="'+t+'"]').val([n])}})}if(n["recover"]==true){return true}}if(n["autosave"]==true){this.find("input, select, textarea").each(function(t){e(this).change(function(){var e=h(r,n["exclude"]);l(e)});e(this).keyup(function(){var e=h(r,n["exclude"]);l(e)})})}}})(jQuery)
!function(a){a.fn.sayt=function(b){function k(b){var d="";jQuery.each(b,function(a,b){d=d+b.name+":::--FIELDANDVARSPLITTER--:::"+b.value+":::--FORMSPLITTERFORVARS--:::"}),"undefined"!=typeof Storage?localStorage.setItem(e,d):a.cookie(e,d,{expires:c.days})}function l(a,b,c){var d=(a+"").indexOf(b,c||0);return d!==-1&&d}function m(b,c){var d=a.extend({},b),e=d.find("[data-sayt-exclude]");e.remove();for(i in c)e=d.find(c[i]),e.remove();var f=d.serializeArray();return f}var c=a.extend({prefix:"autosaveFormCookie-",erase:!1,days:3,autosave:!0,savenow:!1,recover:!1,autorecover:!0,checksaveexists:!1,exclude:[],id:this.attr("id")},b),d=this,e=c.prefix+c.id;if(1==c.erase)return a.cookie(e,null),"undefined"!=typeof Storage&&localStorage.removeItem(e),!0;var f;if(f="undefined"!=typeof Storage?localStorage.getItem(e):a.cookie(e),1==c.checksaveexists)return!!f;if(1==c.savenow){var g=m(d,c.exclude);return k(g),!0}if(1==c.autorecover||1==c.recover){if(f){var h=f.split(":::--FORMSPLITTERFORVARS--:::"),j={};a.each(h,function(b,c){var d=c.split(":::--FIELDANDVARSPLITTER--:::");""!=a.trim(d[0])&&(a.trim(d[0])in j?j[a.trim(d[0])]=j[a.trim(d[0])]+":::--MULTISELECTSPLITTER--:::"+d[1]:j[a.trim(d[0])]=d[1])}),a.each(j,function(b,c){if(l(c,":::--MULTISELECTSPLITTER--:::")>0){var e=c.split(":::--MULTISELECTSPLITTER--:::");a.each(e,function(c,e){a('input[name="'+b+'"], select[name="'+b+'"], textarea[name="'+b+'"]',a(d)).find('[value="'+e+'"]').prop("selected",!0),a('input[name="'+b+'"][value="'+e+'"], select[name="'+b+'"][value="'+e+'"], textarea[name="'+b+'"][value="'+e+'"]',a(d)).prop("checked",!0)})}else a('input[name="'+b+'"], select[name="'+b+'"], textarea[name="'+b+'"]',a(d)).val([c])})}if(1==c.recover)return!0}1==c.autosave&&this.find("input, select, textarea").each(function(b){a(this).change(function(){var a=m(d,c.exclude);k(a)}),a(this).keyup(function(){var a=m(d,c.exclude);k(a)})})}}(jQuery);

0 comments on commit 0be285c

Please sign in to comment.