From 50f6e3ccfc64885e632be5b70148cfdfab12a8a1 Mon Sep 17 00:00:00 2001 From: Danil Flores Date: Fri, 21 Jun 2013 22:48:49 -0400 Subject: [PATCH 1/2] Added jStorage plugin --- README.md | 1 + jstorage/jstorage-tests.ts | 74 +++++++++++++++++ jstorage/jstorage.d.ts | 159 +++++++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 jstorage/jstorage-tests.ts create mode 100644 jstorage/jstorage.d.ts diff --git a/README.md b/README.md index e220f7bef..3cfeba4d1 100755 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ List of Definitions * [jScrollPane](http://jscrollpane.kelvinluck.com) (by [Dániel Tar](https://github.com/qcz)) * [JSDeferred](http://cho45.stfuawsc.com/jsdeferred/) (by [Daisuke Mino](https://github.com/minodisk)) * [JSONEditorOnline](https://github.com/josdejong/jsoneditoronline) (by [Vincent Bortone](https://github.com/vbortone/)) +* [jStorage](http://www.jstorage.info/) (by [Danil Flores](https://github.com/dflor003/)) * [JWPlayer](http://developer.longtailvideo.com/trac/) (by [Martin Duparc](https://github.com/martinduparc/)) * [KeyboardJS](https://github.com/RobertWHurst/KeyboardJS) (by [Vincent Bortone](https://github.com/vbortone/)) * [Knockback](http://kmalakoff.github.com/knockback/) (by [Marcel Binot](https://github.com/docgit)) diff --git a/jstorage/jstorage-tests.ts b/jstorage/jstorage-tests.ts new file mode 100644 index 000000000..a8bd01af4 --- /dev/null +++ b/jstorage/jstorage-tests.ts @@ -0,0 +1,74 @@ +/// + +// Test set first overload +var storedValue = $.jStorage.set("testObj", { foo: 'bar' }); +console.assert(storedValue.foo === "bar"); + +// Test set second overload +$.jStorage.set("testNum", 42, { TTL: 65535 }); +var readValue = $.jStorage.get("testNum"); +console.assert(readValue + 5 === 47); + +// Test deleteKey +if ($.jStorage.deleteKey("testObj") === true) { + console.log('deleted'); +} + +// Test setTTL/getTTL +$.jStorage.setTTL("testNum", 100); +console.assert($.jStorage.getTTL("testNum") === 100); + +// Test flush +console.assert($.jStorage.flush() === true); + +// Test storageObj +var storeObj = $.jStorage.storageObj(); +console.assert(storeObj["testNum"] !== null); + +// Test index +var keys = $.jStorage.index(); +console.assert(keys.length > 0); + +// Test storageSize +var size = $.jStorage.storageSize(); +console.assert(size > 0); + +// Test currentBackend +var currentBackend = $.jStorage.currentBackend(); +console.assert(currentBackend != null && typeof currentBackend.getItem !== "undefined"); + +// Test storageAvailable +var isStorageAvailable = $.jStorage.storageAvailable(); +console.assert(isStorageAvailable === true); + +// Test listenKeyChange +$.jStorage.listenKeyChange("testNum", (key, value) => { + console.assert(key.length > 0); + console.assert(value != null); +} ); + +$.jStorage.listenKeyChange("testNum", (key, value) => { + console.assert(key === "testNum"); + console.assert(value + 10 > 0); +} ); + +// Test stopListening +$.jStorage.stopListening("testNum"); +$.jStorage.stopListening("testNum", () => { console.assert(); } ); + +// Test subscribe +$.jStorage.subscribe("ESPN", (channel, value) => { + console.assert(channel !== "ABC"); + console.assert(value !== null); +} ); + +$.jStorage.subscribe("ESPN", (channel, value) => { + console.assert(channel === "ESPN"); + console.assert(value.getDate() > Date.now()); +} ); + +// Test publish +$.jStorage.publish("ESPN", { date: new Date(2013, 4, 26, 7), game: "Miami Heat" }); + +// Test reinit +$.jStorage.reInit(); \ No newline at end of file diff --git a/jstorage/jstorage.d.ts b/jstorage/jstorage.d.ts new file mode 100644 index 000000000..0acc93fbd --- /dev/null +++ b/jstorage/jstorage.d.ts @@ -0,0 +1,159 @@ +// Type definitions for jStorage 0.3.0 +// Project: http://www.jstorage.info/ +// Definitions by: Danil Flores +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module $.jStorage { + + class IStorageOptions { + TTL: number; + } + + interface IJStorage { + [key: string]: any; + } + + /** + * Sets a key's value. + * + * @param key Key to set. If this value is not set or not + * a string an exception is raised. + * @param value Value to set. This can be any value that is JSON + * compatible (Numbers, Strings, Objects etc.). + * @param [options] - possible options to use + * @param [options.TTL] - optional TTL value + * @return the used value + */ + function set (key: string, value: TValue, options?: IStorageOptions): TValue; + + /** + * Looks up a key in cache + * + * @param key - Key to look up. + * @param defaultIfNotFound - Default value to return, if key didn't exist. + * @return the key value, default value or null + */ + function get (key: string, defaultIfNotFound?: TValue): TValue; + + /** + * Deletes a key from cache. + * + * @param key - Key to delete. + * @return true if key existed or false if it didn't + */ + function deleteKey(key: string): boolean; + + /** + * Sets a TTL for a key, or remove it if ttl value is 0 or below + * + * @param key - key to set the TTL for + * @param ttl - TTL timeout in milliseconds + * @return true if key existed or false if it didn't + */ + function setTTL(key: string, ttl: number): boolean; + + /** + * Gets remaining TTL (in milliseconds) for a key or 0 when no TTL has been set + * + * @param key Key to check + * @return Remaining TTL in milliseconds + */ + function getTTL(key: string): number; + + /** + * Deletes everything in cache. + * + * @return Always true + */ + function flush(): boolean; + + /** + * Returns a read-only copy of _storage + * + * @return Read-only copy of _storage + */ + function storageObj(): IJStorage + + /** + * Returns an index of all used keys as an array + * ['key1', 'key2',..'keyN'] + * + * @return Used keys + */ + function index(): string[]; + + /** + * How much space in bytes does the storage take? + * + * @return Storage size in chars (not the same as in bytes, + * since some chars may take several bytes) + */ + function storageSize(): number; + + /** + * Which backend is currently in use? + * + * @return Backend name + */ + function currentBackend(): Storage; + + /** + * Test if storage is available + * + * @return True if storage can be used + */ + function storageAvailable(): boolean; + + /** + * Register change listeners + * + * @param key Key name + * @param callback Function to run when the key changes + */ + function listenKeyChange(key: string, callback: (key: string, value: any) => void ): void; + + /** + * Register change listeners + * + * @param key Key name + * @param callback Function to run when the key changes + */ + function listenKeyChange(key: string, callback: (key: string, value: TValue) => void ): void; + + /** + * Remove change listeners + * + * @param key Key name to unregister listeners against + * @param [callback] If set, unregister the callback, if not - unregister all + */ + function stopListening(key: string, callback?: Function): void; + + /** + * Subscribe to a Publish/Subscribe event stream + * + * @param channel Channel name + * @param callback Function to run when the something is published to the channel + */ + function subscribe(channel: string, callback: (channel: string, value: any) => void ): void; + + /** + * Subscribe to a Publish/Subscribe event stream + * + * @param channel Channel name + * @param callback Function to run when the something is published to the channel + */ + function subscribe(channel: string, callback: (channel: string, value: TValue) => void ): void; + + /** + * Publish data to an event stream + * + * @param channel Channel name + * @param payload Payload to deliver + */ + function publish(channel: string, payload: any): void; + + /** + * Reloads the data from browser storage + */ + function reInit(): void; +} \ No newline at end of file From bca57e2d23064239445e1cabea6fe62a4c72461d Mon Sep 17 00:00:00 2001 From: Danil Flores Date: Fri, 21 Jun 2013 23:09:43 -0400 Subject: [PATCH 2/2] Added ladda library --- README.md | 3 ++- ladda/ladda-tests.ts | 20 ++++++++++++++++++++ ladda/ladda.d.ts | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 ladda/ladda-tests.ts create mode 100644 ladda/ladda.d.ts diff --git a/README.md b/README.md index 3cfeba4d1..2943452fd 100755 --- a/README.md +++ b/README.md @@ -126,8 +126,9 @@ List of Definitions * [KoLite](https://github.com/CodeSeven/kolite) (by [Boris Yankov](https://github.com/borisyankov)) * [Leaflet](https://github.com/Leaflet/Leaflet) (by [Vladimir](https://github.com/rgripper)) * [Libxmljs](https://github.com/polotek/libxmljs) (by [François de Campredon](https://github.com/fdecampredon)) +* [ladda](https://github.com/hakimel/Ladda) (by [Danil Flores](https://github.com/dflor003)) * [linq.js](http://linqjs.codeplex.com/) (by [Marcin Najder](https://github.com/marcinnajder)) -* [Livestamp.js](https://github.com/mattbradley/livestampjs) (by [Vincent Bortone] (https://github.com/vbortone)) +* [Livestamp.js](https://github.com/mattbradley/livestampjs) (by [Vincent Bortone](https://github.com/vbortone)) * [Marked](https://github.com/chjj/marked) (by [William Orr](https://github.com/worr)) * [Modernizr](http://modernizr.com/) (by [Boris Yankov](https://github.com/borisyankov) and [Theodore Brown](https://github.com/theodorejb/)) * [Moment.js](https://github.com/timrwood/moment) (by [Michael Lakerveld](https://github.com/Lakerfield)) diff --git a/ladda/ladda-tests.ts b/ladda/ladda-tests.ts new file mode 100644 index 000000000..9707534cc --- /dev/null +++ b/ladda/ladda-tests.ts @@ -0,0 +1,20 @@ +/// + +// Test bind +Ladda.bind('button.ladda-button', { timeout: 42, callback: btn => alert('Clicked!!!') }); +Ladda.bind('button.ladda-button'); +Ladda.bind(document.createElement('button'), {}); +Ladda.bind(document.createElement('button')); + +// Test stop all +Ladda.stopAll(); + +// Test create +var btnElement = document.createElement('button'); +var laddaBtn = Ladda.create(btnElement); + +// Test operations via chaining +laddaBtn.start().stop().toggle().setProgress(42).enable().disable().start(); + +// Test isLoading +console.assert(laddaBtn.isLoading() === true); \ No newline at end of file diff --git a/ladda/ladda.d.ts b/ladda/ladda.d.ts new file mode 100644 index 000000000..278f00379 --- /dev/null +++ b/ladda/ladda.d.ts @@ -0,0 +1,35 @@ +// Type definitions for jStorage 0.4.0 +// Project: https://github.com/hakimel/Ladda +// Definitions by: Danil Flores +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module Ladda { + + interface ILaddaButton { + start(): ILaddaButton; + + stop(): ILaddaButton; + + toggle(): ILaddaButton; + + setProgress(progress: number): ILaddaButton; + + enable(): ILaddaButton; + + disable(): ILaddaButton; + + isLoading(): boolean; + } + + interface ILaddaOptions { + timeout?: number; + callback?: (instance: ILaddaButton) => void; + } + + function bind(target: HTMLElement, options?: ILaddaOptions): void; + function bind(cssSelector: string, options?: ILaddaOptions): void; + + function create(button: HTMLElement): ILaddaButton; + + function stopAll(): void; +} \ No newline at end of file