Skip to content

Commit

Permalink
storage working again
Browse files Browse the repository at this point in the history
  • Loading branch information
deltadada committed Oct 14, 2014
1 parent 3d50648 commit 8f1ae84
Show file tree
Hide file tree
Showing 30 changed files with 262 additions and 2,529 deletions.
99 changes: 81 additions & 18 deletions PrefModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,47 @@ var PrefModel = {
aPrefs :
{
intScheme : INTSCHEME_A,
riq_LoFret : 0,
riq_HiFret :19,
riq_StrDepth : 5,
riq_FretDepth : 5,
rrq_LoFret: 0,
rrq_HiFret: 19,
rrq_LoStr: 1,
rrq_HiStr: 6,
rrq_Notegroups : [],
},
usingUserPrefs:false
,
is_chrome :false
,
init : function(aNotegroups, maxFret, maxString){
this.aPrefs.rrq_HiFret = maxFret;
this.aPrefs.rrq_HiStr = maxString;
this.aPrefs.rrq_HiFret = this.aPrefs.riq_HiFret = maxFret;
this.aPrefs.rrq_HiStr = this.aPrefs.riq_HiStr = maxString;
// local chrome will not save cookies
this.is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
this.aPrefs.rrq_Notegroups = [];
for(ng in aNotegroups){
// Note: only pushing scales not arps or anything else
this.aPrefs.rrq_Notegroups.push([dictScales[ng].ngtype+'_'+dictScales[ng].varname, true]);
}

}
,
usingUserPrefs:false
,

writePrefCookie : function(){
// put form values in mPref.aPrefs and save to cookie, or local storage if chrome

mPref.aPrefs.riq_LoFret = $('#spRIQ_LoFret').spinner( "value" );
mPref.aPrefs.riq_HiFret = $('#spRIQ_HiFret').spinner( "value" );
mPref.aPrefs.riq_StrDepth = $('#spRIQ_StrDepth').spinner( "value" );
mPref.aPrefs.riq_FretDepth = $('#spRIQ_FretDepth').spinner( "value" );

// random root
mPref.aPrefs.rrq_LoFret = $('#spRR_LoFret').spinner().spinner( "value" );
mPref.aPrefs.rrq_HiFret = $('#spRR_HiFret').spinner().spinner( "value" );
mPref.aPrefs.rrq_LoStr = $('#spRR_LoStr').spinner().spinner( "value" );
mPref.aPrefs.rrq_HiStr = $('#spRR_HiStr').spinner().spinner( "value" );
mPref.aPrefs.rrq_LoFret = $('#spRR_LoFret').spinner( "value" );
mPref.aPrefs.rrq_HiFret = $('#spRR_HiFret').spinner( "value" );
mPref.aPrefs.rrq_LoStr = $('#spRR_LoStr').spinner( "value" );
mPref.aPrefs.rrq_HiStr = $('#spRR_HiStr').spinner( "value" );
// go through Notegroup inputs in the preferences popup and align with aPrefs.rrq_Notegroups
// 0 is name and 1 is true or false (checked or unchecked)
for (var ng in mPref.aPrefs.rrq_Notegroups){
Expand All @@ -50,26 +66,66 @@ var PrefModel = {
mPref.aPrefs.rrq_Notegroups[ng][1] = false;
}
}


// local chrome will not save cookies
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
//set cookies
if(is_chrome)$.localStorage('fs_userPrefs', this.aPrefs);
else $.cookies.set('fs_userPrefs', this.aPrefs);
if(this.is_chrome){
var data = this.aPrefs;
$.localStorage('fs_userPrefs', data);
}
else {
$.cookie('fs_userPrefs', this.aPrefs);
}

var testStorage;
// test cookie
if(this.is_chrome){
testStorage = $.localStorage('fs_userPrefs');
}else{
testStorage = $.cookie('fs_userPrefs')
}
//fs_userPrefs =(this.is_chrome)?$.localStorage('fs_userPrefs'):$.cookie('fs_userPrefs');
alert('wrote: '+testStorage);
}
,
retrieveUserPrefs : function(){
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

//get cookies
var fs_userPrefs=(is_chrome)?$.localStorage('fs_userPrefs'):$.cookies.get('fs_userPrefs');
if(fs_userPrefs!=null){
// cookie test-- Chrome does not save cookie if there is no webserver
// $.cookie('test_cookie', 'test', { expires: 1 });
// cookietest = $.cookie('test_cookie'); // => "the_value"
// $.removeCookie('test_cookie');

//get cookies-- Chrome does not save cookie if there is no webserver; must use storage
var fs_userPrefs = null;
//fs_userPrefs =(is_chrome)?$.localStorage('fs_userPrefs'):$.cookies.get('fs_userPrefs');

if (this.is_chrome){
fs_userPrefs = $.localStorage('fs_userPrefs');
}else{
fs_userPrefs = $.cookie('fs_userPrefs');
}
if(fs_userPrefs!=null ){
var storageGood = false;
try {
if(fs_userPrefs.rrq_Notegroups.length >1){
storageGood = true}
}
catch (e) {
// statements to handle any exceptions
storageGood = false;
}
if(storageGood){
this.aPrefs = fs_userPrefs;
this.usingUserPrefs = true;
}
}
//alert( fs_userPrefs +' '+fs_userPrefs.rrq_LoFret+' '+fs_userPrefs.rrq_HiFret);

},
removePrefCookie : function(){
if(this.is_chrome){
$.localStorage('fs_userPrefs', null)
} else {
$.removeCookie('fs_userPrefs');
}
}
,
notegroupInPrefs : function(ngKey){
Expand All @@ -82,6 +138,13 @@ var PrefModel = {
}
return rBool;
}
,
checkAllNotegroups : function(){
for (var ng in mPref.aPrefs.rrq_Notegroups){
mPref.aPrefs.rrq_Notegroups[ng][1] = true;
}

}


}
126 changes: 115 additions & 11 deletions boxfrets.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ var ctl_newRandRoot = function(){
}

// check boxes according to Pref model
var ctl_updateRRQprefView = function(){
var ctl_updateRRQnotegroupsToPrefModel = function(){
for (var ng in mPref.aPrefs.rrq_Notegroups){
//0: scale name, 1: t/f checked in prefs
if(mPref.aPrefs.rrq_Notegroups[ng][1]){
Expand All @@ -707,6 +707,21 @@ var ctl_updateRRQprefView = function(){
}
};

var ctl_setPrefFormToPrefModel = function(){

$( "#spRIQ_LoFret" ).spinner("value",mPref.aPrefs.riq_LoFret);
$( "#spRIQ_HiFret" ).spinner("value",mPref.aPrefs.riq_HiFret);
$( "#spRIQ_StrDepth" ).spinner("value",mPref.aPrefs.riq_StrDepth);
$( "#spRIQ_FretDepth" ).spinner("value",mPref.aPrefs.riq_FretDepth);

$( "#spRR_LoFret" ).spinner("value",mPref.aPrefs.rrq_LoFret);
$( "#spRR_HiFret" ).spinner("value",mPref.aPrefs.rrq_HiFret);
$( "#spRR_LoStr" ).spinner("value",mPref.aPrefs.rrq_LoStr);
$( "#spRR_HiStr" ).spinner("value",mPref.aPrefs.rrq_HiStr);
ctl_updateRRQnotegroupsToPrefModel();


}
//when dialog is closed, update the Pref model with data in popup form controls
var ctl_updatePrefs = function(){
//RandomRootQuiz
Expand All @@ -715,6 +730,17 @@ var ctl_updatePrefs = function(){
mPref.writePrefCookie();

};
// returns preferences to defaults and rewrites cookie
var ctl_resetDefaultPrefs = function(){
mPref.removePrefCookie();
var testRemoveCookie = fs_userPrefs =(this.is_chrome)?$.localStorage('fs_userPrefs'):$.cookie('fs_userPrefs');
mPref.init(dictScales, GUITAR_STRINGS[0].length, GUITAR_STRINGS.length);
mPref.checkAllNotegroups();
ctl_setPrefFormToPrefModel();
//ctl_updateRRQprefView();
mPref.writePrefCookie();

}

// more utilities
//
Expand Down Expand Up @@ -975,10 +1001,34 @@ var populateNotegroupsRandRootTab = function(){
ng = dictScales[mPref.aPrefs.rrq_Notegroups[ng][0]]; // 0 is name, 1 is t/f for checked box
html += '<input type="checkbox" id="prefRRQ_'+ng.ngtype+'_'+ng.varname+'" name="'+ng.ngtype+'_'+ng.varname+'" value="'+ng.ngtype+'_'+ng.varname+'"> '+ng.name+'<br>';
}
html+='<span class="RR_scaleOptionsCol">';
//html+='<span class="RR_scaleOptionsCol">';
html+='</span>';
$("#RR_scaleOptionsDiv").append(html);
// attach function that makes sure at least one ng selection is checked.
for (var ng in mPref.aPrefs.rrq_Notegroups){
$('#prefRRQ_'+mPref.aPrefs.rrq_Notegroups[ng][0]).click(function(){
oneRRQalwaysChecked(this);
});
// if($('#prefRRQ_'+mPref.aPrefs.rrq_Notegroups[ng][0]).is(':checked')){
// mPref.aPrefs.rrq_Notegroups[ng][1] = true;
// } else{
// mPref.aPrefs.rrq_Notegroups[ng][1] = false;
// }
}
};

var oneRRQalwaysChecked = function(lastClicked){
var checkedNGs =0;
for (var ng in mPref.aPrefs.rrq_Notegroups){
if($('#prefRRQ_'+mPref.aPrefs.rrq_Notegroups[ng][0]).is(':checked')){
checkedNGs++;
}
}
if (checkedNGs == 0){
$(lastClicked).prop('checked', true);
}
}


// Unabridged divs are the notegroup div objects in the left hand div under the fretboard
// with all the tabs for each key. These are all the scales and arpeggios, unabridged.
Expand Down Expand Up @@ -1087,40 +1137,89 @@ jQuery(document).ready(function() {
// make tabs in popup
$("#prefTabs").tabs();

// set up RandomRoot quiz prefs
// set up Random Interval quiz prefs
var spRI_LoFret = $( "#spRIQ_LoFret" ).spinner({
min: mPref.aPrefs.riq_LoFret,
max: mPref.aPrefs.riq_HiFret,
value: mPref.aPrefs.riq_LoFret,
change: function( event, ui ) {
if(this.value > $("#spRIQ_HiFret").spinner( "value" )){
$("#spRIQ_HiFret").spinner( "value", this.value );
}
},
});
var spRI_HiFret = $( "#spRIQ_HiFret" ).spinner({
min: mPref.aPrefs.riq_LoFret,
max: mPref.aPrefs.riq_HiFret,
value: mPref.aPrefs.riq_HiFret,
change: function( event, ui ) {
if(this.value < $("#spRIQ_LoFret").spinner( "value" )){
$("#spRIQ_LoFret").spinner( "value", this.value );
}
},
});

var spRI_StrDepth = $( "#spRIQ_StrDepth" ).spinner({
min: mPref.aPrefs.riq_LoStr,
max: mPref.aPrefs.riq_HiStr - 1,
value: mPref.aPrefs.riq_StrDepth
});
var spRI_FretDepth = $( "#spRIQ_FretDepth" ).spinner({
min: 1,
max: mPref.aPrefs.riq_HiFret - 1,
value: mPref.aPrefs.riq_FretDepth
});
// set up Random Root quiz prefs
var spRR_LoFret = $( "#spRR_LoFret" ).spinner({
min: mPref.aPrefs.rrq_LoFret,
max: mPref.aPrefs.rrq_HiFret,
value: mPref.aPrefs.rrq_LoFret
value: mPref.aPrefs.rrq_LoFret,
change: function( event, ui ) {
if(this.value > $("#spRR_HiFret").spinner( "value" )){
$("#spRR_HiFret").spinner( "value", this.value );
}
},
});
var spRR_HiFret = $("#spRR_HiFret").spinner({
min: mPref.aPrefs.rrq_LoFret,
max: mPref.aPrefs.rrq_HiFret,
value: mPref.aPrefs.rrq_HiFret
value: mPref.aPrefs.rrq_HiFret,
change: function( event, ui ) {
if(this.value < $("#spRR_LoFret").spinner( "value" )){
$("#spRR_LoFret").spinner( "value", this.value );
}
},
});
var spRR_LoStr = $("#spRR_LoStr").spinner({
min: mPref.aPrefs.rrq_LoStr,
max: mPref.aPrefs.rrq_HiStr,
value: mPref.aPrefs.rrq_LoStr
value: mPref.aPrefs.rrq_LoStr,
change: function( event, ui ) {
if(this.value > $("#spRR_HiStr").spinner( "value" )){
$("#spRR_HiStr").spinner( "value", this.value );
}
},
});
var spRR_HiStr = $("#spRR_HiStr").spinner({
min: mPref.aPrefs.rrq_LoStr,
max: mPref.aPrefs.rrq_HiStr,
value: mPref.aPrefs.rrq_HiStr,
change: function( event, ui ) {
if(this.value < $("#spRR_LoStr").spinner( "value" )){
$("#spRR_LoStr").spinner( "value", this.value );
}
},
});

// set up prefs per user cookie or default
mPref.retrieveUserPrefs();

spRR_LoFret.spinner("value",mPref.aPrefs.rrq_LoFret);
spRR_HiFret.spinner("value",mPref.aPrefs.rrq_HiFret);
spRR_LoStr.spinner("value",mPref.aPrefs.rrq_LoStr);
spRR_HiStr.spinner("value",mPref.aPrefs.rrq_HiStr);

// dynamically insert notegroup checkboxes
populateNotegroupsRandRootTab();
// check boxes according to Pref model
ctl_updateRRQprefView();
ctl_setPrefFormToPrefModel();
//ctl_updateRRQprefView();


// get any url parameters and adjust model(s) appropriately
Expand Down Expand Up @@ -1367,6 +1466,7 @@ jQuery(document).ready(function() {

});


// Bind set root button -- when on, next fret note clicked will be new Key in Fretboard Model, then button reverts to off
//
$('#setRoot').click(function(){
Expand Down Expand Up @@ -1435,6 +1535,10 @@ jQuery(document).ready(function() {
loadFromUrl();
});

// reset defaults in preferences/general
$('#btnResetDefaults').click(function(){
ctl_resetDefaultPrefs();
});



Expand Down
Loading

0 comments on commit 8f1ae84

Please sign in to comment.