Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Splitting code base up into smaller modules

Storage and cookie storage modules have now been split up into separate
files for easier maintenance. Corresponding specs have been moved swell.
  • Loading branch information...
commit 93eebce0243b526a006c80c5d3a13ed53188bead 1 parent 684d5db
@danielstocks authored
View
5 spec/buster.js
@@ -5,7 +5,10 @@ config["jQuery Collapse specs"] = {
environment: "browser",
sources: [
"vendor/jquery-1.7.2.js",
- "src/*.js"
+ "vendor/json2.js",
+ "src/jquery.collapse.js",
+ "src/jquery.collapse_storage.js",
+ "src/jquery.collapse_cookie_storage.js"
],
tests: [
"spec/*_spec.js"
View
21 spec/jquery.collapse_cookie_storage_spec.coffee
@@ -0,0 +1,21 @@
+buster.spec.expose()
+
+describe 'Cookie Storage', ->
+
+ before ->
+ @cookieStorage = $.fn.collapse.cookieStorage
+
+ it 'should have setItem method', ->
+ expect(@cookieStorage.setItem).toBeFunction()
+
+ it 'should have a getItem method', ->
+ expect(@cookieStorage.getItem).toBeFunction()
+
+ it 'should set a cookie', ->
+ @cookieStorage.cookies = ""
+ @cookieStorage.setItem("test", "holla")
+ expect(@cookieStorage.cookies).toEqual "test=holla; expires=" + @cookieStorage.expires + "; path=/"
+
+ it 'should get a cookie value', ->
+ @cookieStorage.cookies = "blah=bleh; test=holla;"
+ expect(@cookieStorage.getItem('test')).toEqual 'holla'
View
60 spec/jquery.collapse_spec.coffee
@@ -348,63 +348,3 @@ describe "jQuery Collapse", ->
@jq.close(2)
expect(@jq.db.write).toHaveBeenCalledWith(2,0)
- describe 'Storage', ->
-
- before ->
- @storage = new jQueryCollapseStorage("xyz")
- @storage.db =
- setItem : @stub()
- getItem : ->
-
- it "should set an id", ->
- expect(@storage.id).toBe "xyz"
-
- it "should have an array data property", ->
- expect(@storage.data).toBeArray()
-
- it 'should set the data array to five zero-values', ->
- @storage.write(4,0)
- expect(@storage.data).toEqual [0,0,0,0,0]
-
- it 'should set the 3:rd value in array to 1', ->
- @storage.write(2,1)
- expect(@storage.data).toEqual [0,0,1]
-
- it 'should write stringified array to storage backend', ->
- @storage.write(4,1)
- arg = JSON.stringify("xyz" : [0,0,0,0,1])
- expect(@storage.db.setItem).toHaveBeenCalledWith("jQuery-Collapse", arg)
-
- it 'should re-save previously written items from storage', ->
- @stub(@storage.db, 'getItem').returns('{ "abc" : [1] }')
- @storage.write(1,0)
- obj =
- "abc": [1]
- "xyz": [0,0]
- expect(@storage.db.setItem)
- .toHaveBeenCalledWith("jQuery-Collapse", JSON.stringify(obj))
-
- it 'should return stored data', ->
- @stub(@storage.db, 'getItem').returns('{ "xyz" : [1,0,1] }')
- expect(@storage.read()).toEqual [1,0,1]
-
- describe 'Cookie Storage', ->
-
- before ->
- @cookieStorage = jQueryCollapseCookieStorage
-
- it 'should have setItem method', ->
- expect(@cookieStorage.setItem).toBeFunction()
-
- it 'should have a getItem method', ->
- expect(@cookieStorage.getItem).toBeFunction()
-
- it 'should set a cookie', ->
- @cookieStorage.cookies = ""
- @cookieStorage.setItem("test", "holla")
- expect(@cookieStorage.cookies).toEqual "test=holla; expires=" + @cookieStorage.expires + "; path=/"
-
- it 'should get a cookie value', ->
- @cookieStorage.cookies = "blah=bleh; test=holla;"
- expect(@cookieStorage.getItem('test')).toEqual 'holla'
-
View
41 spec/jquery.collapse_storage_spec.coffee
@@ -0,0 +1,41 @@
+buster.spec.expose()
+
+describe 'jQuery Collapse Storage', ->
+
+ before ->
+ @storage = new jQueryCollapseStorage("xyz")
+ @storage.db =
+ setItem : @stub()
+ getItem : ->
+
+ it "should set an id", ->
+ expect(@storage.id).toBe "xyz"
+
+ it "should have an array data property", ->
+ expect(@storage.data).toBeArray()
+
+ it 'should set the data array to five zero-values', ->
+ @storage.write(4,0)
+ expect(@storage.data).toEqual [0,0,0,0,0]
+
+ it 'should set the 3:rd value in array to 1', ->
+ @storage.write(2,1)
+ expect(@storage.data).toEqual [0,0,1]
+
+ it 'should write stringified array to storage backend', ->
+ @storage.write(4,1)
+ arg = JSON.stringify("xyz" : [0,0,0,0,1])
+ expect(@storage.db.setItem).toHaveBeenCalledWith("jQuery-Collapse", arg)
+
+ it 'should re-save previously written items from storage', ->
+ @stub(@storage.db, 'getItem').returns('{ "abc" : [1] }')
+ @storage.write(1,0)
+ obj =
+ "abc": [1]
+ "xyz": [0,0]
+ expect(@storage.db.setItem)
+ .toHaveBeenCalledWith("jQuery-Collapse", JSON.stringify(obj))
+
+ it 'should return stored data', ->
+ @stub(@storage.db, 'getItem').returns('{ "xyz" : [1,0,1] }')
+ expect(@storage.read()).toEqual [1,0,1]
View
65 src/jquery.collapse.js
@@ -29,7 +29,7 @@
_this.options = options;
_this.sections = [];
_this.isAccordion = options.accordion || false;
- _this.db = options.persist ? new Storage(el[0].id) : false;
+ _this.db = options.persist ? new jQueryCollapseStorage(el[0].id) : false;
_this.states = _this.db ? _this.db.read() : [];
// For every pair of elements in given
@@ -148,65 +148,6 @@
}
}
- var STORAGE_KEY = "jQuery-Collapse";
-
- function Storage(id) {
- this.id = id;
- this.db = window.localStorage || cookieStorage;
- this.data = [];
- }
- Storage.prototype = {
- write: function(position, state) {
- var _this = this;
- _this.data[position] = state;
- // Pad out data array with zero values
- $.each(_this.data, function(i) {
- if(typeof _this.data[i] == 'undefined') {
- _this.data[i] = 0;
- }
- });
- var obj = this.getDataObject();
- obj[this.id] = this.data;
- this.db.setItem(STORAGE_KEY, JSON.stringify(obj));
- },
- read: function() {
- var obj = this.getDataObject();
- return obj[this.id] || [];
- },
- getDataObject: function() {
- var string = this.db.getItem(STORAGE_KEY);
- return string ? JSON.parse(string) : {}
- }
- }
-
- var cookieStorage = {
- expires: function() {
- var now = new Date();
- return now.setDate(now.getDate() + 1);
- }(),
- cookies : document.cookie,
- setItem: function(key, value) {
- this.cookies = key + '=' + value + '; expires=' + this.expires +'; path=/'
- },
- getItem: function(key) {
- key+= "=";
- var item = "";
- $.each(this.cookies.split(';'), function(i, cookie) {
- while (cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length);
- if(cookie.indexOf(key) == 0) {
- item = cookie.substring(key.length,cookie.length);
- }
- });
- return item;
- }
- }
-
- // Expose constructor to
- // global namespace
- jQueryCollapse = Collapse;
- jQueryCollapseStorage = Storage;
- jQueryCollapseCookieStorage = cookieStorage;
-
// Add shortcut method
// to jQuery API
$.fn.extend({
@@ -228,4 +169,8 @@
$.fn.collapse(false, true)
});
+ // Expose constructor to
+ // global namespace
+ jQueryCollapse = Collapse;
+
}(window.jQuery);
View
38 src/jquery.collapse_cookie_storage.js
@@ -0,0 +1,38 @@
+/*
+ * Cookie Storage for jQuery Collapse
+ * --
+ * source: http://github.com/danielstocks/jQuery-Collapse/
+ * site: http://webcloud.se/code/jQuery-Collapse
+ *
+ * @author Daniel Stocks (http://webcloud.se)
+ * Copyright 2012, Daniel Stocks
+ * Released under the MIT, BSD, and GPL Licenses.
+ */
+
+!function($) {
+
+ var cookieStorage = {
+ expires: function() {
+ var now = new Date();
+ return now.setDate(now.getDate() + 1);
+ }(),
+ cookies : document.cookie,
+ setItem: function(key, value) {
+ this.cookies = key + '=' + value + '; expires=' + this.expires +'; path=/'
+ },
+ getItem: function(key) {
+ key+= "=";
+ var item = "";
+ $.each(this.cookies.split(';'), function(i, cookie) {
+ while (cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length);
+ if(cookie.indexOf(key) == 0) {
+ item = cookie.substring(key.length,cookie.length);
+ }
+ });
+ return item;
+ }
+ }
+
+ $.fn.collapse.cookieStorage = cookieStorage;
+
+}(jQuery);
View
47 src/jquery.collapse_storage.js
@@ -0,0 +1,47 @@
+/*
+ * Storage for jQuery Collapse
+ * --
+ * source: http://github.com/danielstocks/jQuery-Collapse/
+ * site: http://webcloud.se/code/jQuery-Collapse
+ *
+ * @author Daniel Stocks (http://webcloud.se)
+ * Copyright 2012, Daniel Stocks
+ * Released under the MIT, BSD, and GPL Licenses.
+ */
+
+!function($) {
+
+ var STORAGE_KEY = "jQuery-Collapse";
+
+ function Storage(id) {
+ this.id = id;
+ this.db = window.localStorage || $.fn.collapse.cookieStorage;
+ this.data = [];
+ }
+ Storage.prototype = {
+ write: function(position, state) {
+ var _this = this;
+ _this.data[position] = state;
+ // Pad out data array with zero values
+ $.each(_this.data, function(i) {
+ if(typeof _this.data[i] == 'undefined') {
+ _this.data[i] = 0;
+ }
+ });
+ var obj = this.getDataObject();
+ obj[this.id] = this.data;
+ this.db.setItem(STORAGE_KEY, JSON.stringify(obj));
+ },
+ read: function() {
+ var obj = this.getDataObject();
+ return obj[this.id] || [];
+ },
+ getDataObject: function() {
+ var string = this.db.getItem(STORAGE_KEY);
+ return string ? JSON.parse(string) : {}
+ }
+ }
+
+ jQueryCollapseStorage = Storage;
+
+}(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.