Skip to content

Instantly share code, notes, and snippets.

@russellf9
Created December 23, 2014 15:35
Show Gist options
  • Save russellf9/2b996c23afbb1d2110af to your computer and use it in GitHub Desktop.
Save russellf9/2b996c23afbb1d2110af to your computer and use it in GitHub Desktop.
Schedule App
'use strict';
/**
* A directive to handle local storage
*/
tool.directive('storage', ['$log', '$modal', 'storageSVC', 'locationSVC', function($log, $modal, storageSVC, locationSVC) {
return {
restrict: 'EA',
scope: {
},
controller: function($scope) {
$scope.openUserModal = function() {
var modalInstance = $modal.open({
templateUrl: 'partials/modal/set-user-modal.html',
controller: 'ModalInstanceCtrl',
resolve : {
edit: function() {
return $scope.edit;
}
}
});
modalInstance.result.then(function(result) {
// resolved
$scope.edit = result;
// when the user selected their type the pseudo cookie 'visited` can be set to true
// and the edit type can be evaluated.
storageSVC.setVisited('true');
storageSVC.setEditMode(result);
}, function() {
// dismissed
}).finally(function() {
});
};
},
link: {
post: function(scope, element, attr) {
if (Modernizr.localstorage) {
// use for testing!
//storageSVC.setEditMode('false');
//storageSVC.setVisited('false');
// 1. check the query string first
if (locationSVC.getVisited()) {
storageSVC.setVisited(locationSVC.getVisited());
}
if (!storageSVC.visited()) {
scope.openUserModal();
}
} else {
// no native support for HTML5 storage :(
// maybe try dojox.storage or a third-party solution
$log.error('no native support for HTML5 storage :- maybe try dojox.storage or a third-party solution');
}
}
}
};
}]);
'use strict';
scheduleServices.factory('storageSVC', ['$log', function($log) {
return {
visited: function() {
// keep the values as strings
return (localStorage.visited) ? (localStorage.visited && localStorage.visited === 'true') : false;
},
setVisited : function(value) {
localStorage.setItem('visited', value);
},
isEditMode : function() {
return (localStorage.editMode) ? (localStorage.editMode && localStorage.editMode === 'true') : false;
},
/**
* Sets the property of the edit mode to the supplied value
* If the new value is undefined will set the property to false
* @param value
*/
setEditMode : function(value) {
if (value && value.toLowerCase() === 'true') {
localStorage.setItem('editMode', 'true');
} else if (!value || value.toLowerCase() === 'false') {
localStorage.setItem('editMode', 'false');
} else {
$log.error('Can only set `editMode` to `true` or `false` - not: ', value);
}
}
};
}]);
'use strict';
scheduleServices.factory('locationSVC', ['$location', function($location) {
return {
getEditMode : function() {
return $location.search().edit;
},
getVisited : function() {
return $location.search().visited;
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment