Last active
February 27, 2025 21:48
Revisions
-
Digiman revised this gist
Mar 16, 2015 . 1 changed file with 0 additions and 141 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,141 +0,0 @@ -
Digiman revised this gist
Mar 16, 2015 . 1 changed file with 21 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,11 +18,17 @@ module StorageHelper { // class for working with local storage in browser (common that can use other classes for store some data) export class LocalStorageWorker { localStorageSupported: boolean; constructor() { this.localStorageSupported = typeof window['localStorage'] != "undefined" && window['localStorage'] != null; } // add value to storage add(key: string, item: string) { if (this.localStorageSupported) { localStorage.setItem(key, item); } } // get all values from storage (all items) @@ -58,18 +64,26 @@ module StorageHelper { // get one item by key from storage get(key: string): string { if (this.localStorageSupported) { var item = localStorage.getItem(key); return item; } else { return null; } } // remove value from storage remove(key: string) { if (this.localStorageSupported) { localStorage.removeItem(key); } } // clear storage (remove all items from it) clear() { if (this.localStorageSupported) { localStorage.clear(); } } } @@ -137,7 +151,7 @@ module StorageHelper { this.save(); } } // clear all data about emails clear() { // remove data by key from storage -
Digiman revised this gist
Mar 16, 2015 . 1 changed file with 141 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,141 @@ // module with classes and logic for working with local storage in browsers via JavaScript // see also: http://professorweb.ru/my/html/html5/level5/5_1.php var StorageHelper; (function (StorageHelper) { var StorageItem = (function () { function StorageItem(data) { this.key = data.key; this.value = data.value; } return StorageItem; })(); StorageHelper.StorageItem = StorageItem; // class for working with local storage in browser (common that can use other classes for store some data) var LocalStorageWorker = (function () { function LocalStorageWorker() { } // add value to storage LocalStorageWorker.prototype.add = function (key, item) { localStorage.setItem(key, item); }; // get all values from storage (all items) LocalStorageWorker.prototype.getAllItems = function () { var list = new Array(); for (var i = 0; i < localStorage.length; i++) { var key = localStorage.key(i); var value = localStorage.getItem(key); list.push(new StorageItem({ key: key, value: value })); } return list; }; // get only all values from localStorage LocalStorageWorker.prototype.getAllValues = function () { var list = new Array(); for (var i = 0; i < localStorage.length; i++) { var key = localStorage.key(i); var value = localStorage.getItem(key); list.push(value); } return list; }; // get one item by key from storage LocalStorageWorker.prototype.get = function (key) { var item = localStorage.getItem(key); return item; }; // remove value from storage LocalStorageWorker.prototype.remove = function (key) { localStorage.removeItem(key); }; // clear storage (remove all items from it) LocalStorageWorker.prototype.clear = function () { localStorage.clear(); }; return LocalStorageWorker; })(); StorageHelper.LocalStorageWorker = LocalStorageWorker; // custom class for store emails in local storage var EmailStorage = (function () { function EmailStorage(storageKey) { this.storageWorker = new LocalStorageWorker(); this.storageKey = storageKey; this.addresses = new Array(); this.activate(); } // activate custom storage for emails EmailStorage.prototype.activate = function () { //this.clear(); this.loadAll(); }; // load all emails from storage to list for working with it EmailStorage.prototype.loadAll = function () { var storageData = this.storageWorker.get(this.storageKey); if (storageData != null && storageData.length > 0) { var emails = JSON.parse(storageData); console.log(emails); if (emails != null) { this.addresses = emails; } console.log(this.addresses); } }; // add new email (without duplicate) EmailStorage.prototype.addEmail = function (email) { if (email.length > 0) { // 1. Split email addresses if needed (if we get list of emails) var mas = email.split(/,|;/g); for (var i = 0; i < mas.length; i++) { // check if not exist and not add new (duplicate) var index = this.addresses.indexOf(mas[i].trim()); if (index < 0) { this.addresses.push(mas[i].trim()); } } console.log(this.addresses); // 3. save to storage this.save(); } }; // clear all data about emails EmailStorage.prototype.clear = function () { // remove data by key from storage this.storageWorker.add(this.storageKey, ""); // or remove with key //this.storageWorker.remove(this.storageKey); }; // save to storage (save as JSON string) EmailStorage.prototype.save = function () { var jsonEmails = JSON.stringify(this.addresses); this.storageWorker.add(this.storageKey, jsonEmails); }; return EmailStorage; })(); StorageHelper.EmailStorage = EmailStorage; })(StorageHelper || (StorageHelper = {})); -
Digiman created this gist
Mar 13, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,156 @@ // module with classes and logic for working with local storage in browsers via JavaScript // see also: http://professorweb.ru/my/html/html5/level5/5_1.php module StorageHelper { export interface IStorageItem { key: string; value: any; } export class StorageItem { key: string; value: any; constructor(data: IStorageItem) { this.key = data.key; this.value = data.value; } } // class for working with local storage in browser (common that can use other classes for store some data) export class LocalStorageWorker { constructor() { } // add value to storage add(key: string, item: string) { localStorage.setItem(key, item); } // get all values from storage (all items) getAllItems(): Array<StorageItem> { var list = new Array<StorageItem>(); for (var i = 0; i < localStorage.length; i++) { var key = localStorage.key(i); var value = localStorage.getItem(key); list.push(new StorageItem({ key: key, value: value })); } return list; } // get only all values from localStorage getAllValues(): Array<any> { var list = new Array<any>(); for (var i = 0; i < localStorage.length; i++) { var key = localStorage.key(i); var value = localStorage.getItem(key); list.push(value); } return list; } // get one item by key from storage get(key: string): string { var item = localStorage.getItem(key); return item; } // remove value from storage remove(key: string) { localStorage.removeItem(key); } // clear storage (remove all items from it) clear() { localStorage.clear(); } } // custom class for store emails in local storage export class EmailStorage { storageWorker: LocalStorageWorker; // main key that use for store list of emails storageKey: string; // list of emails addresses: Array<string>; constructor(storageKey: string) { this.storageWorker = new LocalStorageWorker(); this.storageKey = storageKey; this.addresses = new Array<string>(); this.activate(); } // activate custom storage for emails activate() { //this.clear(); this.loadAll(); } // load all emails from storage to list for working with it loadAll() { var storageData = this.storageWorker.get(this.storageKey); if (storageData != null && storageData.length > 0) { var emails = JSON.parse(storageData); console.log(emails); if (emails != null) { this.addresses = emails; } console.log(this.addresses); } } // add new email (without duplicate) addEmail(email: string) { if (email.length > 0) { // 1. Split email addresses if needed (if we get list of emails) var mas = email.split(/,|;/g); //console.log(mas); // 2. Add each email in the splited list for (var i = 0; i < mas.length; i++) { // check if not exist and not add new (duplicate) var index = this.addresses.indexOf(mas[i].trim()); if (index < 0) { this.addresses.push(mas[i].trim()); } } console.log(this.addresses); // 3. save to storage this.save(); } } // clear all data about emails clear() { // remove data by key from storage this.storageWorker.add(this.storageKey, ""); // or remove with key //this.storageWorker.remove(this.storageKey); } // save to storage (save as JSON string) save() { var jsonEmails = JSON.stringify(this.addresses); this.storageWorker.add(this.storageKey, jsonEmails); } } }