Compare commits
105 Commits
Author | SHA1 | Date |
---|---|---|
Chakib Benziane | aaf4d75d44 | 9 years ago |
Chakib Benziane | 2bb247b9f7 | 9 years ago |
Chakib Benziane | 1b05f4ec63 | 10 years ago |
Chakib Benziane | 85585772e0 | 10 years ago |
Chakib Benziane | f08babe2b0 | 10 years ago |
Chakib Benziane | 15ad8a603e | 10 years ago |
Ariel Mashraki | a07d51c3e7 | 10 years ago |
Ariel Mashraki | 99764b1dc8 | 10 years ago |
Ariel Mashraki | 4ca796cddc | 10 years ago |
Ben Sinther | 144e73c581 | 10 years ago |
Ariel Mashraki | afd4abc9fb | 10 years ago |
Ariel Mashraki | 71e71fc3c0 | 10 years ago |
Ariel Mashraki | 83c8f8aabf | 10 years ago |
Eddie Monge | 0f4db7979b | 10 years ago |
Ariel Mashraki | 950450f637 | 10 years ago |
Ben Sinther | 63def516a6 | 10 years ago |
Ariel Mashraki | 0f28dadbd8 | 10 years ago |
Ariel Mashraki | e31b3eab5c | 10 years ago |
Subodh | df71bac63c | 10 years ago |
Ariel Mashraki | 6fe6f93783 | 10 years ago |
Gernot Höflechner | 1e14e3efa4 | 10 years ago |
Ariel Mashraki | 3c5e2ec435 | 10 years ago |
Ariel Mashraki | ae0c28aba9 | 10 years ago |
Ariel Mashraki | 6ae576d82e | 10 years ago |
Ariel Mashraki | e88c4f5f5f | 10 years ago |
Ariel Mashraki | 1b4fd8d451 | 10 years ago |
Ariel Mashraki | 9066f619d9 | 10 years ago |
Arthur Helmel | a71d928d0e | 10 years ago |
Ariel Mashraki | 5fc726c19c | 10 years ago |
Arthur Helmel | 8223e647c9 | 10 years ago |
Ariel Mashraki | dbd1913d9a | 10 years ago |
Brian Feister | 8e9638dccf | 10 years ago |
Ariel Mashraki | d85bdb5b24 | 10 years ago |
Ariel Mashraki | 94be1481aa | 10 years ago |
Ariel Mashraki | af9708d43b | 10 years ago |
Ariel Mashraki | 1917e5748c | 10 years ago |
Ariel Mashraki | 2d4b402739 | 10 years ago |
Ariel Mashraki | 52d04ad7ce | 10 years ago |
Ariel Mashraki | d4e3f76c38 | 10 years ago |
Gregory Pike | 9cf1e0729d | 10 years ago |
hermantran | cdf2dd07c3 | 10 years ago |
Ariel Mashraki | 6fdec3ed78 | 10 years ago |
Ariel Mashraki | 6a3c74b43a | 10 years ago |
Ariel Mashraki | a425b43caa | 10 years ago |
Gregory Pike | aef73ba676 | 10 years ago |
Ariel Mashraki | 3f3cd06bae | 10 years ago |
Ariel Mashraki | 45fd4c9d9d | 10 years ago |
Ariel Mashraki | 6d763a76c8 | 10 years ago |
Ariel Mashraki | bacadf1c0f | 10 years ago |
Ariel Mashraki | ed422c0476 | 10 years ago |
Lucas Andión Montáns | db87118a0c | 10 years ago |
Ariel Mashraki | 6db17f5ed9 | 10 years ago |
Ariel Mashraki | fe26708204 | 10 years ago |
Ariel Mashraki | ae13ec45fd | 10 years ago |
Ariel Mashraki | 5b9cd352ec | 10 years ago |
Ariel Mashraki | eeec036e9e | 10 years ago |
Ariel Mashraki | a10a1a7a05 | 10 years ago |
Ariel Mashraki | da357b763c | 10 years ago |
Ariel Mashraki | f95a2420c3 | 10 years ago |
Ariel Mashraki | 738e5cae75 | 10 years ago |
Den Teresh | 5948d2be36 | 10 years ago |
Den Teresh | 887b682815 | 10 years ago |
Kevin Salter | cbf90c4af3 | 10 years ago |
Ariel Mashraki | 36926d8cf5 | 10 years ago |
Ariel Mashraki | 97c504e7e3 | 10 years ago |
Ariel Mashraki | a8d48ff5c7 | 10 years ago |
Ariel Mashraki | 6dcc4f1a58 | 10 years ago |
Ariel Mashraki | b12cc0493d | 10 years ago |
Ariel Mashraki | d2bc39f2ba | 10 years ago |
Ariel Mashraki | 8a4bdc248b | 10 years ago |
Ariel Mashraki | 7493cd88e0 | 10 years ago |
Ariel Mashraki | 44575f4028 | 10 years ago |
Ariel Mashraki | fd2fc272fa | 10 years ago |
Gregory Pike | 4b25996bac | 10 years ago |
Ariel Mashraki | 82b59ed1b5 | 10 years ago |
Ariel Mashraki | bf33743093 | 10 years ago |
Gregory Pike | 74457d4782 | 10 years ago |
Gregory Pike | 4304808d6d | 10 years ago |
Sten Muchow | ff25b6d895 | 10 years ago |
Gregory Pike | 0afea0e801 | 10 years ago |
Gregory Pike | 58da1088d8 | 10 years ago |
Gregory Pike | b5c681b6f6 | 10 years ago |
Gregory Pike | e01246e356 | 10 years ago |
Gregory Pike | 5616a668d1 | 10 years ago |
Gregory Pike | 7d86e3321c | 10 years ago |
Eugenio Cordoba | 0ec19f1153 | 10 years ago |
Gregory Pike | b4750937fd | 10 years ago |
Guillermo Cruz | 7bbbb8899b | 10 years ago |
Osama Khalil | d2c25d1e1e | 10 years ago |
Gregory Pike | 76eddc4cb8 | 10 years ago |
Yihang Ho | 0ff995d63b | 10 years ago |
Yihang Ho | 0b8d7820e9 | 10 years ago |
Yihang Ho | 84a9b404b6 | 10 years ago |
FelixLC | 65ecddfc76 | 10 years ago |
Gregory Pike | 296db308bc | 10 years ago |
Ariel Mashraki | 61463bd73f | 10 years ago |
Ariel Mashraki | b1b426698a | 10 years ago |
Ariel Mashraki | ea2065e51c | 10 years ago |
Ariel Mashraki | 50fb50d58c | 10 years ago |
Ariel Mashraki | 38a769ea88 | 10 years ago |
Ariel Mashraki | 0c86054aba | 10 years ago |
Ariel Mashraki | e692f70bc2 | 10 years ago |
Ariel Mashraki | 24b6f4d1d6 | 10 years ago |
Sebastian Gronewold | f7753704c2 | 10 years ago |
Sebastian Gronewold | 0b8be0935a | 10 years ago |
@ -0,0 +1,8 @@
|
|||||||
|
<a name="0.1.1"></a>
|
||||||
|
# 0.1.1 (2014-10-06)
|
||||||
|
|
||||||
|
|
||||||
|
## Breaking Changes
|
||||||
|
|
||||||
|
- update your `index.html` file to reference angular-local-storage at its new
|
||||||
|
path inside the `dist` directory `/angular-local-storage/dist/angular-local-storage.js`
|
@ -1,60 +1,378 @@
|
|||||||
angular-local-storage
|
angular-local-storage
|
||||||
=====================
|
=====================
|
||||||
|
**Note to myself: Used for an other project, do not delete.**
|
||||||
|
|
||||||
An Angular module that gives you access to the browsers local storage
|
An Angular module that gives you access to the browsers local storage, **v0.1.5**
|
||||||
|
|
||||||
[![Build Status](https://secure.travis-ci.org/grevory/angular-local-storage.png?branch=master)](https://travis-ci.org/grevory/)
|
[![NPM version][npm-image]][npm-url]
|
||||||
|
[![Build status][travis-image]][travis-url]
|
||||||
|
[![Test coverage][coveralls-image]][coveralls-url]
|
||||||
|
[![Dependency Status][david-image]][david-url]
|
||||||
|
[![License][license-image]][license-url]
|
||||||
|
[![Downloads][downloads-image]][downloads-url]
|
||||||
|
|
||||||
Installation:
|
##Table of contents:
|
||||||
|
- [Get Started](#get-started)
|
||||||
|
- [Development](#development)
|
||||||
|
- [Configuration](#configuration)
|
||||||
|
- [setPrefix](#setprefix)
|
||||||
|
- [setStorageType](#setstoragetype)
|
||||||
|
- [setStorageCookie](#setstoragecookie)
|
||||||
|
- [setStorageCookieDomain](#setstoragecookiedomain)
|
||||||
|
- [setNotify](#setnotify)
|
||||||
|
- [Example](#configuration-example)
|
||||||
|
- [API Documentation](#api-documentation)
|
||||||
|
- [isSupported](#issupported)
|
||||||
|
- [getStorageType](#getstoragetype)
|
||||||
|
- [set](#set)
|
||||||
|
- [get](#get)
|
||||||
|
- [keys](#keys)
|
||||||
|
- [remove](#remove)
|
||||||
|
- [clearAll](#clearall)
|
||||||
|
- [bind](#bind)
|
||||||
|
- [deriveKey](#derivekey)
|
||||||
|
- [length](#length)
|
||||||
|
- [cookie](#cookie)
|
||||||
|
- [isSupported](#cookieissupported)
|
||||||
|
- [set](#cookieset)
|
||||||
|
- [get](#cookieget)
|
||||||
|
- [remove](#cookieremove)
|
||||||
|
- [clearAll](#cookieclearall)
|
||||||
|
|
||||||
|
##Get Started
|
||||||
|
**(1)** You can install angular-local-storage using 2 different ways:<br/>
|
||||||
|
**Git:**
|
||||||
|
clone & build [this](https://github.com/grevory/angular-local-storage.git) repository<br/>
|
||||||
|
**Bower:**
|
||||||
```bash
|
```bash
|
||||||
bower install angular-local-storage
|
$ bower install angular-local-storage
|
||||||
```
|
```
|
||||||
|
**npm:**
|
||||||
|
```bash
|
||||||
|
$ npm install angular-local-storage
|
||||||
|
```
|
||||||
|
**(2)** Include `angular-local-storage.js` (or `angular-local-storage.min.js`) in your `index.html`, after including Angular itself.
|
||||||
|
|
||||||
Example use:
|
**(3)** Add `'LocalStorageModule'` to your main module's list of dependencies.
|
||||||
|
|
||||||
```javascript
|
When you're done, your setup should look similar to the following:
|
||||||
angular.module('yourModule', ['LocalStorageModule'])
|
|
||||||
.controller('yourCtrl', [
|
|
||||||
'$scope',
|
|
||||||
'localStorageService',
|
|
||||||
function($scope, localStorageService) {
|
|
||||||
// Start fresh
|
|
||||||
localStorageService.clearAll();
|
|
||||||
|
|
||||||
// Set a key
|
```html
|
||||||
localStorageService.set('Favorite Sport','Ultimate Frisbee');
|
<!doctype html>
|
||||||
|
<html ng-app="myApp">
|
||||||
|
<head>
|
||||||
|
|
||||||
// Delete a key
|
</head>
|
||||||
localStorageService.delete('Favorite Sport');
|
<body>
|
||||||
}]);
|
...
|
||||||
|
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
|
||||||
|
<script src="bower_components/js/angular-local-storage.min.js"></script>
|
||||||
|
...
|
||||||
|
<script>
|
||||||
|
var myApp = angular.module('myApp', ['LocalStorageModule']);
|
||||||
|
|
||||||
/*
|
</script>
|
||||||
To set the prefix of your localStorage name, you can use the setPrefix method
|
...
|
||||||
available on the localStorageServiceProvider
|
</body>
|
||||||
*/
|
</html>
|
||||||
angular.module('yourModule', ['LocalStorageModule'])
|
```
|
||||||
.config(['localStorageServiceProvider', function(localStorageServiceProvider){
|
##Configuration
|
||||||
localStorageServiceProvider.setPrefix('newPrefix');
|
###setPrefix
|
||||||
}]);
|
You could set a prefix to avoid overwriting any local storage variables from the rest of your app<br/>
|
||||||
|
**Default prefix:** `ls.<your-key>`
|
||||||
|
```js
|
||||||
|
myApp.config(function (localStorageServiceProvider) {
|
||||||
|
localStorageServiceProvider
|
||||||
|
.setPrefix('yourAppName');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###setStorageType
|
||||||
|
You could change web storage type to localStorage or sessionStorage<br/>
|
||||||
|
**Default storage:** `localStorage`
|
||||||
|
```js
|
||||||
|
myApp.config(function (localStorageServiceProvider) {
|
||||||
|
localStorageServiceProvider
|
||||||
|
.setStorageType('sessionStorage');
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
###setStorageCookie
|
||||||
|
Set cookie options (usually in case of fallback)<br/>
|
||||||
|
**expiry:** number of days before cookies expire (0 = does not expire). **default:** `30`<br/>
|
||||||
|
**path:** the web path the cookie represents. **default:** `'/'`
|
||||||
|
```js
|
||||||
|
myApp.config(function (localStorageServiceProvider) {
|
||||||
|
localStorageServiceProvider
|
||||||
|
.setStorageCookie(45, '<path>');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###setStorageCookieDomain
|
||||||
|
Set the cookie domain, since this runs inside a the `config()` block, only providers and constants can be injected. As a result, `$location` service can't be used here, use a hardcoded string or `window.location`.<br/>
|
||||||
|
**No default value**
|
||||||
|
```js
|
||||||
|
myApp.config(function (localStorageServiceProvider) {
|
||||||
|
localStorageServiceProvider
|
||||||
|
.setStorageCookieDomain('<domain>');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
For local testing (when you are testing on localhost) set the domain to an empty string ''. Setting the domain to 'localhost' will not work on all browsers (eg. Chrome) since some browsers only allow you to set domain cookies for registry controlled domains, i.e. something ending in .com or so, but not IPs **or intranet hostnames** like localhost. </br>
|
||||||
|
|
||||||
#### How to bind to a $scope variable:
|
###setNotify
|
||||||
Usage: localStorageService.bind(scope, key, def);
|
Send signals for each of the following actions:<br/>
|
||||||
|
**setItem** , default: `true`<br/>
|
||||||
|
**removeItem** , default: `false`
|
||||||
|
```js
|
||||||
|
myApp.config(function (localStorageServiceProvider) {
|
||||||
|
localStorageServiceProvider
|
||||||
|
.setNotify(true, true);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###Configuration Example
|
||||||
|
Using all together
|
||||||
|
```js
|
||||||
|
myApp.config(function (localStorageServiceProvider) {
|
||||||
|
localStorageServiceProvider
|
||||||
|
.setPrefix('myApp')
|
||||||
|
.setStorageType('sessionStorage')
|
||||||
|
.setNotify(true, true)
|
||||||
|
});
|
||||||
|
```
|
||||||
|
##API Documentation
|
||||||
|
##isSupported
|
||||||
|
Checks if the browser support the current storage type(e.g: `localStorage`, `sessionStorage`).
|
||||||
|
**Returns:** `Boolean`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
if(localStorageService.isSupported) {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###getStorageType
|
||||||
|
**Returns:** `String`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
var storageType = localStorageService.getStorageType(); //e.g localStorage
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###set
|
||||||
|
Directly adds a value to local storage.<br/>
|
||||||
|
If local storage is not supported, use cookies instead.<br/>
|
||||||
|
**Returns:** `Boolean`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
function submit(key, val) {
|
||||||
|
return localStorageService.set(key, val);
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###get
|
||||||
|
Directly get a value from local storage.<br/>
|
||||||
|
If local storage is not supported, use cookies instead.<br/>
|
||||||
|
**Returns:** `value from local storage`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
function getItem(key) {
|
||||||
|
return localStorageService.get(key);
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###keys
|
||||||
|
Return array of keys for local storage, ignore keys that not owned.<br/>
|
||||||
|
**Returns:** `value from local storage`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
var lsKeys = localStorageService.keys();
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###remove
|
||||||
|
Remove an item from local storage by key.<br/>
|
||||||
|
If local storage is not supported, use cookies instead.<br/>
|
||||||
|
**Returns:** `Boolean`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
function removeItem(key) {
|
||||||
|
return localStorageService.remove(key);
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
```
|
```
|
||||||
// Example
|
###clearAll
|
||||||
$scope.anArtist = {'firstname':'Pablo', 'lastname':'Picasso'};
|
Remove all data for this app from local storage.<br/>
|
||||||
|
If local storage is not supported, use cookies instead.<br/>
|
||||||
|
**Note:** Optionally takes a regular expression string and removes matching.<br/>
|
||||||
|
**Returns:** `Boolean`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
function clearNumbers(key) {
|
||||||
|
return localStorageService.clearAll(/^\d+$/);
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
function clearAll() {
|
||||||
|
return localStorageService.clearAll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###bind
|
||||||
|
Bind $scope key to localStorageService.
|
||||||
|
**Usage:** `localStorageService.bind(scope, property, value[optional], key[optional])`
|
||||||
|
***key:*** The corresponding key used in local storage
|
||||||
|
**Returns:** deregistration function for this listener.
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
localStorageService.set('property', 'oldValue');
|
||||||
|
$scope.unbind = localStorageService.bind($scope, 'property');
|
||||||
|
|
||||||
// Bind to local storage service
|
//Test Changes
|
||||||
localStorageService.bind($scope, 'anArtist', anArtist);
|
$scope.update = function(val) {
|
||||||
|
$scope.property = val;
|
||||||
|
$timeout(function() {
|
||||||
|
alert("localStorage value: " + localStorageService.get('property'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
```html
|
||||||
|
<div ng-controller="MainCtrl">
|
||||||
|
<p>{{property}}</p>
|
||||||
|
<input type="text" ng-model="lsValue"/>
|
||||||
|
<button ng-click="update(lsValue)">update</button>
|
||||||
|
<button ng-click="unbind()">unbind</button>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
// get bound data:
|
###deriveKey
|
||||||
console.log(localStorageService.get('anArtist'));
|
Return the derive key
|
||||||
|
**Returns** `String`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
localStorageService.set('property', 'oldValue');
|
||||||
|
//Test Result
|
||||||
|
console.log(localStorageService.deriveKey('property')); // ls.property
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###length
|
||||||
|
Return localStorageService.length, ignore keys that not owned.
|
||||||
|
**Returns** `Number`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
var lsLength = localStorageService.length(); // e.g: 7
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
##Cookie
|
||||||
|
Deal with browser's cookies directly.
|
||||||
|
##cookie.isSupported
|
||||||
|
Checks if cookies are enabled in the browser.
|
||||||
|
**Returns:** `Boolean`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
if(localStorageService.cookie.isSupported) {
|
||||||
|
//...
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###cookie.set
|
||||||
|
Directly adds a value to cookies.<br/>
|
||||||
|
**Note:** Typically used as a fallback if local storage is not supported.<br/>
|
||||||
|
**Returns:** `Boolean`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
function submit(key, val) {
|
||||||
|
return localStorageService.cookie.set(key, val);
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###cookie.get
|
||||||
|
Directly get a value from a cookie.<br/>
|
||||||
|
**Returns:** `value from local storage`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
function getItem(key) {
|
||||||
|
return localStorageService.cookie.get(key);
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
```
|
```
|
||||||
|
###cookie.remove
|
||||||
|
Remove directly value from a cookie.<br/>
|
||||||
|
**Returns:** `Boolean`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
function removeItem(key) {
|
||||||
|
return localStorageService.cookie.remove(key);
|
||||||
|
}
|
||||||
|
//...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
###cookie.clearAll
|
||||||
|
Remove all data for this app from cookie.<br/>
|
||||||
|
**Returns:** `Boolean`
|
||||||
|
```js
|
||||||
|
myApp.controller('MainCtrl', function($scope, localStorageService) {
|
||||||
|
//...
|
||||||
|
function clearAll() {
|
||||||
|
return localStorageService.cookie.clearAll();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Check out the full demo at http://gregpike.net/demos/angular-local-storage/demo.html
|
||||||
|
|
||||||
Check out the full demo and documentation at http://gregpike.net/demos/angular-local-storage/demo.html
|
##Development:
|
||||||
|
* Don't forget about tests.
|
||||||
|
* If you planning add some feature please create issue before.
|
||||||
|
|
||||||
To do:
|
Clone the project:
|
||||||
- Add tests
|
```sh
|
||||||
- Expand Readme
|
$ git clone https://github.com/<your-repo>/angular-local-storage.git
|
||||||
|
$ npm install
|
||||||
|
$ bower install
|
||||||
|
```
|
||||||
|
Run the tests:
|
||||||
|
```sh
|
||||||
|
$ grunt test
|
||||||
|
```
|
||||||
|
**Deploy:**<br/>
|
||||||
|
Run the build task, update version before(bower,package)
|
||||||
|
```sh
|
||||||
|
$ grunt dist
|
||||||
|
$ git tag 0.*.*
|
||||||
|
$ git push origin master --tags
|
||||||
|
```
|
||||||
|
|
||||||
|
[npm-image]: https://img.shields.io/npm/v/angular-local-storage.svg?style=flat-square
|
||||||
|
[npm-url]: https://npmjs.org/package/angular-local-storage
|
||||||
|
[travis-image]: https://img.shields.io/travis/grevory/angular-local-storage.svg?style=flat-square
|
||||||
|
[travis-url]: https://travis-ci.org/grevory/angular-local-storage
|
||||||
|
[coveralls-image]: https://img.shields.io/coveralls/grevory/angular-local-storage.svg?style=flat-square
|
||||||
|
[coveralls-url]: https://coveralls.io/r/grevory/angular-local-storage
|
||||||
|
[david-image]: http://img.shields.io/david/grevory/angular-local-storage.svg?style=flat-square
|
||||||
|
[david-url]: https://david-dm.org/grevory/angular-local-storage
|
||||||
|
[license-image]: http://img.shields.io/npm/l/angular-local-storage.svg?style=flat-square
|
||||||
|
[license-url]: LICENSE
|
||||||
|
[downloads-image]: http://img.shields.io/npm/dm/angular-local-storage.svg?style=flat-square
|
||||||
|
[downloads-url]: https://npmjs.org/package/angular-local-storage
|
||||||
|
@ -1 +0,0 @@
|
|||||||
(function(){"use strict";var a=angular.module("LocalStorageModule",[]);a.provider("localStorageService",function(){this.prefix="ls",this.storageType="localStorage",this.cookie={expiry:30,path:"/"},this.notify={setItem:!0,removeItem:!1},this.setPrefix=function(a){this.prefix=a},this.setStorageType=function(a){this.storageType=a},this.setStorageCookie=function(a,b){this.cookie={expiry:a,path:b}},this.setStorageCookieDomain=function(a){this.cookie.domain=a},this.setNotify=function(a,b){this.notify={setItem:a,removeItem:b}},this.$get=["$rootScope","$window","$document",function(a,b,c){var d,e=this,f=e.prefix,g=e.cookie,h=e.notify,i=e.storageType;c?c[0]&&(c=c[0]):c=document,"."!==f.substr(-1)&&(f=f?f+".":"");var j=function(a){return f+a},k=function(){try{var c=i in b&&null!==b[i],e=j("__"+Math.round(1e7*Math.random()));return c&&(d=b[i],d.setItem(e,""),d.removeItem(e)),c}catch(f){return i="cookie",a.$broadcast("LocalStorageModule.notification.error",f.message),!1}}(),l=function(b,c){if(!k||"cookie"===e.storageType)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),h.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:b,newvalue:c,storageType:"cookie"}),r(b,c);"undefined"==typeof c&&(c=null);try{(angular.isObject(c)||angular.isArray(c))&&(c=angular.toJson(c)),d&&d.setItem(j(b),c),h.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:b,newvalue:c,storageType:e.storageType})}catch(f){return a.$broadcast("LocalStorageModule.notification.error",f.message),r(b,c)}return!0},m=function(b){if(!k||"cookie"===e.storageType)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),s(b);var c=d?d.getItem(j(b)):null;return c&&"null"!==c?"{"===c.charAt(0)||"["===c.charAt(0)?angular.fromJson(c):c:null},n=function(b){if(!k)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),h.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:"cookie"}),t(b);try{d.removeItem(j(b)),h.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:e.storageType})}catch(c){return a.$broadcast("LocalStorageModule.notification.error",c.message),t(b)}return!0},o=function(){if(!k)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),!1;var b=f.length,c=[];for(var e in d)if(e.substr(0,b)===f)try{c.push(e.substr(b))}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.Description),[]}return c},p=function(b){b=b||"";var c=f.slice(0,-1),e=new RegExp(c+"."+b);if(!k)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),u();var g=f.length;for(var h in d)if(e.test(h))try{n(h.substr(g))}catch(i){return a.$broadcast("LocalStorageModule.notification.error",i.message),u()}return!0},q=function(){try{return navigator.cookieEnabled||"cookie"in c&&(c.cookie.length>0||(c.cookie="test").indexOf.call(c.cookie,"test")>-1)}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},r=function(b,d){if("undefined"==typeof d)return!1;if(!q())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var e="",f=new Date,h="";if(null===d?(f.setTime(f.getTime()+-864e5),e="; expires="+f.toGMTString(),d=""):0!==g.expiry&&(f.setTime(f.getTime()+24*g.expiry*60*60*1e3),e="; expires="+f.toGMTString()),b){var i="; path="+g.path;g.domain&&(h="; domain="+g.domain),c.cookie=j(b)+"="+encodeURIComponent(d)+e+i+h}}catch(k){return a.$broadcast("LocalStorageModule.notification.error",k.message),!1}return!0},s=function(b){if(!q())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var d=c.cookie&&c.cookie.split(";")||[],e=0;e<d.length;e++){for(var g=d[e];" "===g.charAt(0);)g=g.substring(1,g.length);if(0===g.indexOf(j(b)+"="))return decodeURIComponent(g.substring(f.length+b.length+1,g.length))}return null},t=function(a){r(a,null)},u=function(){for(var a=null,b=f.length,d=c.cookie.split(";"),e=0;e<d.length;e++){for(a=d[e];" "===a.charAt(0);)a=a.substring(1,a.length);var g=a.substring(b,a.indexOf("="));t(g)}},v=function(){return i},w=function(a,b,c){var d=m(b);null===d&&angular.isDefined(c)?d=c:angular.isObject(d)&&angular.isObject(c)&&(d=angular.extend(c,d)),a[b]=d,a.$watchCollection(b,function(a){l(b,a)})};return{isSupported:k,getStorageType:v,set:l,add:l,get:m,keys:o,remove:n,clearAll:p,bind:w,deriveKey:j,cookie:{set:r,add:r,get:s,remove:t,clearAll:u}}}]})}).call(this);
|
|
@ -0,0 +1,456 @@
|
|||||||
|
/**
|
||||||
|
* An Angular module that gives you access to the browsers local storage
|
||||||
|
* @version v0.1.5 - 2015-01-22
|
||||||
|
* @link https://github.com/grevory/angular-local-storage
|
||||||
|
* @author grevory <greg@gregpike.ca>
|
||||||
|
* @license MIT License, http://www.opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
(function ( window, angular, undefined ) {
|
||||||
|
/*jshint globalstrict:true*/
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var isDefined = angular.isDefined,
|
||||||
|
isUndefined = angular.isUndefined,
|
||||||
|
isNumber = angular.isNumber,
|
||||||
|
isObject = angular.isObject,
|
||||||
|
isArray = angular.isArray,
|
||||||
|
extend = angular.extend,
|
||||||
|
toJson = angular.toJson,
|
||||||
|
fromJson = angular.fromJson;
|
||||||
|
|
||||||
|
|
||||||
|
// Test if string is only contains numbers
|
||||||
|
// e.g '1' => true, "'1'" => true
|
||||||
|
function isStringNumber(num) {
|
||||||
|
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, ''));
|
||||||
|
}
|
||||||
|
|
||||||
|
var angularLocalStorage = angular.module('LocalStorageModule', []);
|
||||||
|
|
||||||
|
angularLocalStorage.provider('localStorageService', function() {
|
||||||
|
|
||||||
|
// You should set a prefix to avoid overwriting any local storage variables from the rest of your app
|
||||||
|
// e.g. localStorageServiceProvider.setPrefix('yourAppName');
|
||||||
|
// With provider you can use config as this:
|
||||||
|
// myApp.config(function (localStorageServiceProvider) {
|
||||||
|
// localStorageServiceProvider.prefix = 'yourAppName';
|
||||||
|
// });
|
||||||
|
this.prefix = 'ls';
|
||||||
|
|
||||||
|
// You could change web storage type localstorage or sessionStorage
|
||||||
|
this.storageType = 'localStorage';
|
||||||
|
|
||||||
|
// Cookie options (usually in case of fallback)
|
||||||
|
// expiry = Number of days before cookies expire // 0 = Does not expire
|
||||||
|
// path = The web path the cookie represents
|
||||||
|
this.cookie = {
|
||||||
|
expiry: 30,
|
||||||
|
path: '/'
|
||||||
|
};
|
||||||
|
|
||||||
|
// Send signals for each of the following actions?
|
||||||
|
this.notify = {
|
||||||
|
setItem: true,
|
||||||
|
removeItem: false
|
||||||
|
};
|
||||||
|
|
||||||
|
// Setter for the prefix
|
||||||
|
this.setPrefix = function(prefix) {
|
||||||
|
this.prefix = prefix;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Setter for the storageType
|
||||||
|
this.setStorageType = function(storageType) {
|
||||||
|
this.storageType = storageType;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Setter for cookie config
|
||||||
|
this.setStorageCookie = function(exp, path) {
|
||||||
|
this.cookie = {
|
||||||
|
expiry: exp,
|
||||||
|
path: path
|
||||||
|
};
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Setter for cookie domain
|
||||||
|
this.setStorageCookieDomain = function(domain) {
|
||||||
|
this.cookie.domain = domain;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Setter for notification config
|
||||||
|
// itemSet & itemRemove should be booleans
|
||||||
|
this.setNotify = function(itemSet, itemRemove) {
|
||||||
|
this.notify = {
|
||||||
|
setItem: itemSet,
|
||||||
|
removeItem: itemRemove
|
||||||
|
};
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.$get = ['$rootScope', '$window', '$document', '$parse', function($rootScope, $window, $document, $parse) {
|
||||||
|
var self = this;
|
||||||
|
var prefix = self.prefix;
|
||||||
|
var cookie = self.cookie;
|
||||||
|
var notify = self.notify;
|
||||||
|
var storageType = self.storageType;
|
||||||
|
var webStorage;
|
||||||
|
|
||||||
|
// When Angular's $document is not available
|
||||||
|
if (!$document) {
|
||||||
|
$document = document;
|
||||||
|
} else if ($document[0]) {
|
||||||
|
$document = $document[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is a prefix set in the config lets use that with an appended period for readability
|
||||||
|
if (prefix.substr(-1) !== '.') {
|
||||||
|
prefix = !!prefix ? prefix + '.' : '';
|
||||||
|
}
|
||||||
|
var deriveQualifiedKey = function(key) {
|
||||||
|
return prefix + key;
|
||||||
|
};
|
||||||
|
// Checks the browser to see if local storage is supported
|
||||||
|
var browserSupportsLocalStorage = (function () {
|
||||||
|
try {
|
||||||
|
var supported = (storageType in $window && $window[storageType] !== null);
|
||||||
|
|
||||||
|
// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
|
||||||
|
// is available, but trying to call .setItem throws an exception.
|
||||||
|
//
|
||||||
|
// "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage
|
||||||
|
// that exceeded the quota."
|
||||||
|
var key = deriveQualifiedKey('__' + Math.round(Math.random() * 1e7));
|
||||||
|
if (supported) {
|
||||||
|
webStorage = $window[storageType];
|
||||||
|
webStorage.setItem(key, '');
|
||||||
|
webStorage.removeItem(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return supported;
|
||||||
|
} catch (e) {
|
||||||
|
storageType = 'cookie';
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Directly adds a value to local storage
|
||||||
|
// If local storage is not available in the browser use cookies
|
||||||
|
// Example use: localStorageService.add('library','angular');
|
||||||
|
var addToLocalStorage = function (key, value) {
|
||||||
|
// Let's convert undefined values to null to get the value consistent
|
||||||
|
if (isUndefined(value)) {
|
||||||
|
value = null;
|
||||||
|
} else if (isObject(value) || isArray(value) || isNumber(+value || value)) {
|
||||||
|
value = toJson(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this browser does not support local storage use cookies
|
||||||
|
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
|
||||||
|
if (!browserSupportsLocalStorage) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (notify.setItem) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'cookie'});
|
||||||
|
}
|
||||||
|
return addToCookies(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (isObject(value) || isArray(value)) {
|
||||||
|
value = toJson(value);
|
||||||
|
}
|
||||||
|
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
|
||||||
|
if (notify.setItem) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
|
||||||
|
return addToCookies(key, value);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Directly get a value from local storage
|
||||||
|
// Example use: localStorageService.get('library'); // returns 'angular'
|
||||||
|
var getFromLocalStorage = function (key) {
|
||||||
|
|
||||||
|
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
|
||||||
|
if (!browserSupportsLocalStorage) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
|
||||||
|
}
|
||||||
|
|
||||||
|
return getFromCookies(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
var item = webStorage ? webStorage.getItem(deriveQualifiedKey(key)) : null;
|
||||||
|
// angular.toJson will convert null to 'null', so a proper conversion is needed
|
||||||
|
// FIXME not a perfect solution, since a valid 'null' string can't be stored
|
||||||
|
if (!item || item === 'null') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.charAt(0) === "{" || item.charAt(0) === "[" || isStringNumber(item)) {
|
||||||
|
return fromJson(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return item;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remove an item from local storage
|
||||||
|
// Example use: localStorageService.remove('library'); // removes the key/value pair of library='angular'
|
||||||
|
var removeFromLocalStorage = function (key) {
|
||||||
|
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
|
||||||
|
if (!browserSupportsLocalStorage) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (notify.removeItem) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: 'cookie'});
|
||||||
|
}
|
||||||
|
return removeFromCookies(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
webStorage.removeItem(deriveQualifiedKey(key));
|
||||||
|
if (notify.removeItem) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: self.storageType});
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
|
||||||
|
return removeFromCookies(key);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Return array of keys for local storage
|
||||||
|
// Example use: var keys = localStorageService.keys()
|
||||||
|
var getKeysForLocalStorage = function () {
|
||||||
|
|
||||||
|
if (!browserSupportsLocalStorage) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var prefixLength = prefix.length;
|
||||||
|
var keys = [];
|
||||||
|
for (var key in webStorage) {
|
||||||
|
// Only return keys that are for this app
|
||||||
|
if (key.substr(0,prefixLength) === prefix) {
|
||||||
|
try {
|
||||||
|
keys.push(key.substr(prefixLength));
|
||||||
|
} catch (e) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error', e.Description);
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keys;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Remove all data for this app from local storage
|
||||||
|
// Also optionally takes a regular expression string and removes the matching key-value pairs
|
||||||
|
// Example use: localStorageService.clearAll();
|
||||||
|
// Should be used mostly for development purposes
|
||||||
|
var clearAllFromLocalStorage = function (regularExpression) {
|
||||||
|
|
||||||
|
regularExpression = regularExpression || "";
|
||||||
|
//accounting for the '.' in the prefix when creating a regex
|
||||||
|
var tempPrefix = prefix.slice(0, -1);
|
||||||
|
var testRegex = new RegExp(tempPrefix + '.' + regularExpression);
|
||||||
|
|
||||||
|
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
|
||||||
|
if (!browserSupportsLocalStorage) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
|
||||||
|
}
|
||||||
|
|
||||||
|
return clearAllFromCookies();
|
||||||
|
}
|
||||||
|
|
||||||
|
var prefixLength = prefix.length;
|
||||||
|
|
||||||
|
for (var key in webStorage) {
|
||||||
|
// Only remove items that are for this app and match the regular expression
|
||||||
|
if (testRegex.test(key)) {
|
||||||
|
try {
|
||||||
|
removeFromLocalStorage(key.substr(prefixLength));
|
||||||
|
} catch (e) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
|
||||||
|
return clearAllFromCookies();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Checks the browser to see if cookies are supported
|
||||||
|
var browserSupportsCookies = (function() {
|
||||||
|
try {
|
||||||
|
return $window.navigator.cookieEnabled ||
|
||||||
|
("cookie" in $document && ($document.cookie.length > 0 ||
|
||||||
|
($document.cookie = "test").indexOf.call($document.cookie, "test") > -1));
|
||||||
|
} catch (e) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}());
|
||||||
|
|
||||||
|
// Directly adds a value to cookies
|
||||||
|
// Typically used as a fallback is local storage is not available in the browser
|
||||||
|
// Example use: localStorageService.cookie.add('library','angular');
|
||||||
|
var addToCookies = function (key, value) {
|
||||||
|
|
||||||
|
if (isUndefined(value)) {
|
||||||
|
return false;
|
||||||
|
} else if(isArray(value) || isObject(value)) {
|
||||||
|
value = toJson(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!browserSupportsCookies) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
var expiry = '',
|
||||||
|
expiryDate = new Date(),
|
||||||
|
cookieDomain = '';
|
||||||
|
|
||||||
|
if (value === null) {
|
||||||
|
// Mark that the cookie has expired one day ago
|
||||||
|
expiryDate.setTime(expiryDate.getTime() + (-1 * 24 * 60 * 60 * 1000));
|
||||||
|
expiry = "; expires=" + expiryDate.toGMTString();
|
||||||
|
value = '';
|
||||||
|
} else if (cookie.expiry !== 0) {
|
||||||
|
expiryDate.setTime(expiryDate.getTime() + (cookie.expiry * 24 * 60 * 60 * 1000));
|
||||||
|
expiry = "; expires=" + expiryDate.toGMTString();
|
||||||
|
}
|
||||||
|
if (!!key) {
|
||||||
|
var cookiePath = "; path=" + cookie.path;
|
||||||
|
if(cookie.domain){
|
||||||
|
cookieDomain = "; domain=" + cookie.domain;
|
||||||
|
}
|
||||||
|
$document.cookie = deriveQualifiedKey(key) + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Directly get a value from a cookie
|
||||||
|
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
|
||||||
|
var getFromCookies = function (key) {
|
||||||
|
if (!browserSupportsCookies) {
|
||||||
|
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var cookies = $document.cookie && $document.cookie.split(';') || [];
|
||||||
|
for(var i=0; i < cookies.length; i++) {
|
||||||
|
var thisCookie = cookies[i];
|
||||||
|
while (thisCookie.charAt(0) === ' ') {
|
||||||
|
thisCookie = thisCookie.substring(1,thisCookie.length);
|
||||||
|
}
|
||||||
|
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) {
|
||||||
|
var storedValues = decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length))
|
||||||
|
try{
|
||||||
|
var obj = JSON.parse(storedValues);
|
||||||
|
return fromJson(obj)
|
||||||
|
}catch(e){
|
||||||
|
return storedValues
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
var removeFromCookies = function (key) {
|
||||||
|
addToCookies(key,null);
|
||||||
|
};
|
||||||
|
|
||||||
|
var clearAllFromCookies = function () {
|
||||||
|
var thisCookie = null, thisKey = null;
|
||||||
|
var prefixLength = prefix.length;
|
||||||
|
var cookies = $document.cookie.split(';');
|
||||||
|
for(var i = 0; i < cookies.length; i++) {
|
||||||
|
thisCookie = cookies[i];
|
||||||
|
|
||||||
|
while (thisCookie.charAt(0) === ' ') {
|
||||||
|
thisCookie = thisCookie.substring(1, thisCookie.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
var key = thisCookie.substring(prefixLength, thisCookie.indexOf('='));
|
||||||
|
removeFromCookies(key);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var getStorageType = function() {
|
||||||
|
return storageType;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add a listener on scope variable to save its changes to local storage
|
||||||
|
// Return a function which when called cancels binding
|
||||||
|
var bindToScope = function(scope, key, def, lsKey) {
|
||||||
|
lsKey = lsKey || key;
|
||||||
|
var value = getFromLocalStorage(lsKey);
|
||||||
|
|
||||||
|
if (value === null && isDefined(def)) {
|
||||||
|
value = def;
|
||||||
|
} else if (isObject(value) && isObject(def)) {
|
||||||
|
value = extend(def, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
$parse(key).assign(scope, value);
|
||||||
|
|
||||||
|
return scope.$watch(key, function(newVal) {
|
||||||
|
addToLocalStorage(lsKey, newVal);
|
||||||
|
}, isObject(scope[key]));
|
||||||
|
};
|
||||||
|
|
||||||
|
// Return localStorageService.length
|
||||||
|
// ignore keys that not owned
|
||||||
|
var lengthOfLocalStorage = function() {
|
||||||
|
var count = 0;
|
||||||
|
var storage = $window[storageType];
|
||||||
|
for(var i = 0; i < storage.length; i++) {
|
||||||
|
if(storage.key(i).indexOf(prefix) === 0 ) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
isSupported: browserSupportsLocalStorage,
|
||||||
|
getStorageType: getStorageType,
|
||||||
|
set: addToLocalStorage,
|
||||||
|
put: addToLocalStorage,
|
||||||
|
add: addToLocalStorage, //DEPRECATED
|
||||||
|
get: getFromLocalStorage,
|
||||||
|
keys: getKeysForLocalStorage,
|
||||||
|
remove: removeFromLocalStorage,
|
||||||
|
clearAll: clearAllFromLocalStorage,
|
||||||
|
bind: bindToScope,
|
||||||
|
deriveKey: deriveQualifiedKey,
|
||||||
|
length: lengthOfLocalStorage,
|
||||||
|
cookie: {
|
||||||
|
isSupported: browserSupportsCookies,
|
||||||
|
set: addToCookies,
|
||||||
|
add: addToCookies, //DEPRECATED
|
||||||
|
get: getFromCookies,
|
||||||
|
remove: removeFromCookies,
|
||||||
|
clearAll: clearAllFromCookies
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}];
|
||||||
|
});
|
||||||
|
})( window, window.angular );
|
@ -0,0 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* An Angular module that gives you access to the browsers local storage
|
||||||
|
* @version v0.1.5 - 2015-01-22
|
||||||
|
* @link https://github.com/grevory/angular-local-storage
|
||||||
|
* @author grevory <greg@gregpike.ca>
|
||||||
|
* @license MIT License, http://www.opensource.org/licenses/MIT
|
||||||
|
*/!function(a,b){"use strict";function c(a){return/^-?\d+\.?\d*$/.test(a.replace(/["']/g,""))}var d=b.isDefined,e=b.isUndefined,f=b.isNumber,g=b.isObject,h=b.isArray,i=b.extend,j=b.toJson,k=b.fromJson,l=b.module("LocalStorageModule",[]);l.provider("localStorageService",function(){this.prefix="ls",this.storageType="localStorage",this.cookie={expiry:30,path:"/"},this.notify={setItem:!0,removeItem:!1},this.setPrefix=function(a){return this.prefix=a,this},this.setStorageType=function(a){return this.storageType=a,this},this.setStorageCookie=function(a,b){return this.cookie={expiry:a,path:b},this},this.setStorageCookieDomain=function(a){return this.cookie.domain=a,this},this.setNotify=function(a,b){return this.notify={setItem:a,removeItem:b},this},this.$get=["$rootScope","$window","$document","$parse",function(a,b,l,m){var n,o=this,p=o.prefix,q=o.cookie,r=o.notify,s=o.storageType;l?l[0]&&(l=l[0]):l=document,"."!==p.substr(-1)&&(p=p?p+".":"");var t=function(a){return p+a},u=function(){try{var c=s in b&&null!==b[s],d=t("__"+Math.round(1e7*Math.random()));return c&&(n=b[s],n.setItem(d,""),n.removeItem(d)),c}catch(e){return s="cookie",a.$broadcast("LocalStorageModule.notification.error",e.message),!1}}(),v=function(b,c){if(e(c)?c=null:(g(c)||h(c)||f(+c||c))&&(c=j(c)),!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),r.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:b,newvalue:c,storageType:"cookie"}),B(b,c);try{(g(c)||h(c))&&(c=j(c)),n&&n.setItem(t(b),c),r.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:b,newvalue:c,storageType:o.storageType})}catch(d){return a.$broadcast("LocalStorageModule.notification.error",d.message),B(b,c)}return!0},w=function(b){if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),C(b);var d=n?n.getItem(t(b)):null;return d&&"null"!==d?"{"===d.charAt(0)||"["===d.charAt(0)||c(d)?k(d):d:null},x=function(b){if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),r.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:"cookie"}),D(b);try{n.removeItem(t(b)),r.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:o.storageType})}catch(c){return a.$broadcast("LocalStorageModule.notification.error",c.message),D(b)}return!0},y=function(){if(!u)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),!1;var b=p.length,c=[];for(var d in n)if(d.substr(0,b)===p)try{c.push(d.substr(b))}catch(e){return a.$broadcast("LocalStorageModule.notification.error",e.Description),[]}return c},z=function(b){b=b||"";var c=p.slice(0,-1),d=new RegExp(c+"."+b);if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),E();var e=p.length;for(var f in n)if(d.test(f))try{x(f.substr(e))}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.message),E()}return!0},A=function(){try{return b.navigator.cookieEnabled||"cookie"in l&&(l.cookie.length>0||(l.cookie="test").indexOf.call(l.cookie,"test")>-1)}catch(c){return a.$broadcast("LocalStorageModule.notification.error",c.message),!1}}(),B=function(b,c){if(e(c))return!1;if((h(c)||g(c))&&(c=j(c)),!A)return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var d="",f=new Date,i="";if(null===c?(f.setTime(f.getTime()+-864e5),d="; expires="+f.toGMTString(),c=""):0!==q.expiry&&(f.setTime(f.getTime()+24*q.expiry*60*60*1e3),d="; expires="+f.toGMTString()),b){var k="; path="+q.path;q.domain&&(i="; domain="+q.domain),l.cookie=t(b)+"="+encodeURIComponent(c)+d+k+i}}catch(m){return a.$broadcast("LocalStorageModule.notification.error",m.message),!1}return!0},C=function(b){if(!A)return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var c=l.cookie&&l.cookie.split(";")||[],d=0;d<c.length;d++){for(var e=c[d];" "===e.charAt(0);)e=e.substring(1,e.length);if(0===e.indexOf(t(b)+"=")){var f=decodeURIComponent(e.substring(p.length+b.length+1,e.length));try{var g=JSON.parse(f);return k(g)}catch(h){return f}}}return null},D=function(a){B(a,null)},E=function(){for(var a=null,b=p.length,c=l.cookie.split(";"),d=0;d<c.length;d++){for(a=c[d];" "===a.charAt(0);)a=a.substring(1,a.length);var e=a.substring(b,a.indexOf("="));D(e)}},F=function(){return s},G=function(a,b,c,e){e=e||b;var f=w(e);return null===f&&d(c)?f=c:g(f)&&g(c)&&(f=i(c,f)),m(b).assign(a,f),a.$watch(b,function(a){v(e,a)},g(a[b]))},H=function(){for(var a=0,c=b[s],d=0;d<c.length;d++)0===c.key(d).indexOf(p)&&a++;return a};return{isSupported:u,getStorageType:F,set:v,put:v,add:v,get:w,keys:y,remove:x,clearAll:z,bind:G,deriveKey:t,length:H,cookie:{isSupported:A,set:B,add:B,get:C,remove:D,clearAll:E}}}]})}(window,window.angular);
|
@ -0,0 +1,18 @@
|
|||||||
|
/*jshint globalstrict:true*/
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var isDefined = angular.isDefined,
|
||||||
|
isUndefined = angular.isUndefined,
|
||||||
|
isNumber = angular.isNumber,
|
||||||
|
isObject = angular.isObject,
|
||||||
|
isArray = angular.isArray,
|
||||||
|
extend = angular.extend,
|
||||||
|
toJson = angular.toJson,
|
||||||
|
fromJson = angular.fromJson;
|
||||||
|
|
||||||
|
|
||||||
|
// Test if string is only contains numbers
|
||||||
|
// e.g '1' => true, "'1'" => true
|
||||||
|
function isStringNumber(num) {
|
||||||
|
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, ''));
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
'use strict';
|
||||||
|
//Mock localStorage
|
||||||
|
function localStorageMock() {
|
||||||
|
var storage = {};
|
||||||
|
Object.defineProperties(storage, {
|
||||||
|
setItem: {
|
||||||
|
value: function(key, value) {
|
||||||
|
storage[key] = value || '';
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
},
|
||||||
|
getItem: {
|
||||||
|
value: function(key) {
|
||||||
|
return storage[key];
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
},
|
||||||
|
removeItem: {
|
||||||
|
value: function(key) {
|
||||||
|
delete storage[key];
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
},
|
||||||
|
length: {
|
||||||
|
get: function() {
|
||||||
|
return Object.keys(storage).length;
|
||||||
|
},
|
||||||
|
enumerable: false
|
||||||
|
},
|
||||||
|
key: {
|
||||||
|
value: function(i) {
|
||||||
|
var aKeys = Object.keys(storage);
|
||||||
|
return aKeys[i] || null;
|
||||||
|
},
|
||||||
|
enumerable: false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return storage;
|
||||||
|
}
|
Reference in New Issue