Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 903b686

Browse files
author
Maximum Hallinan
committed
Add destroy method and destroy method tests
1 parent a6b51e1 commit 903b686

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

src/jquery.big-slide.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@
1313
}(function($) {
1414
'use strict';
1515

16+
// where inlineCSS is the string value of an element's style attribute
17+
// and toRemove is a string of space-separated CSS properties,
18+
// _cleanInlineCSS removes the CSS declaration for each property in toRemove from inlineCSS
19+
// and returns the resulting string
20+
function _cleanInlineCSS(inlineCSS, toRemove){
21+
var inlineCSSArray = inlineCSS.split(';');
22+
var toRemoveArray = toRemove.split(' ');
23+
24+
var cleaned = '';
25+
var keep;
26+
27+
for (var i = 0, j = inlineCSSArray.length; i < j; i++) {
28+
keep = true;
29+
for (var a = 0, b = toRemoveArray.length; a < b; a++) {
30+
if (inlineCSSArray[i] === '' || inlineCSSArray[i].indexOf(toRemoveArray[a]) !== -1) {
31+
keep = false;
32+
}
33+
}
34+
if(keep) {cleaned += inlineCSSArray[i] + '; ';}
35+
}
36+
37+
return cleaned;
38+
}
39+
40+
1641
$.fn.bigSlide = function(options) {
1742
// store the menuLink in a way that is globally accessible
1843
var menuLink = this;
@@ -33,8 +58,15 @@
3358
'afterClose': function() {}
3459
}, options);
3560

36-
// store the menu's state in the model
61+
// CSS properties set by bigSlide.js on all implicated DOM elements
62+
var baseCSSDictionary = 'transition -o-transition -ms-transition -moz-transitions webkit-transition ' + settings.side;
63+
3764
var model = {
65+
//CSS properties set by bigSlide.js on this.$menu
66+
menuCSSDictionary: baseCSSDictionary + ' position top bottom height width',
67+
//CSS properties set by bigSlide.js on this.$push
68+
pushCSSDictionary: baseCSSDictionary,
69+
// store the menu's state in the model
3870
'state': settings.state
3971
};
4072

@@ -43,6 +75,18 @@
4375
init: function(){
4476
view.init();
4577
},
78+
79+
// remove bigSlide behavior from the menu
80+
destroy: function(){
81+
view.destroy();
82+
83+
delete menuLink.bigSlideAPI;
84+
85+
// return a reference to the DOM selection bigSlide.js was called on
86+
// so that the destroy method is chainable
87+
return menuLink;
88+
},
89+
4690
// update the menu's state
4791
changeState: function(){
4892
if (model.state === 'closed') {
@@ -51,6 +95,7 @@
5195
model.state = 'closed'
5296
}
5397
},
98+
5499
// check the menu's state
55100
getState: function(){
56101
return model.state;
@@ -115,7 +160,29 @@
115160
});
116161
}
117162
},
163+
164+
destroy: function(){
165+
//remove inline styles generated by bigSlide.js while preserving any other inline styles
166+
this.$menu.each(function(){
167+
var $this = $(this);
168+
$this.attr( 'style', _cleanInlineCSS($this.attr('style'), model.menuCSSDictionary).trim() );
169+
});
118170

171+
this.$push.each(function(){
172+
var $this = $(this);
173+
$this.attr( 'style', _cleanInlineCSS($this.attr('style'), model.pushCSSDictionary).trim() );
174+
});
175+
176+
//remove active class and unbind bigSlide event handlers
177+
menuLink
178+
.removeClass(settings.activeBtn)
179+
.off('click.bigSlide touchstart.bigSlide');
180+
181+
//release DOM references to avoid memory leaks
182+
this.$menu = null;
183+
this.$push = null;
184+
},
185+
119186
// toggle the menu open
120187
toggleOpen: function() {
121188
settings.beforeOpen();

test/big-slide.spec.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('exposed internal components', function(){
9696
$menu = $('#menu');
9797

9898
$menuLink = $('.menu-link').bigSlide({
99-
side:'right',
99+
side:'left',
100100
menuWidth: '200px'
101101
});
102102
});
@@ -113,15 +113,15 @@ describe('exposed internal components', function(){
113113
});
114114

115115
it('properties of the exposed settings object should have expected values', function(){
116-
expect($menuLink.bigSlideAPI.settings.side).toEqual('right');
116+
expect($menuLink.bigSlideAPI.settings.side).toEqual('left');
117117
expect($menuLink.bigSlideAPI.settings.menuWidth).toEqual('200px');
118118
});
119119

120120
it('calling view.toggleOpen should open the menu', function(){
121121
$menuLink.bigSlideAPI.view.toggleOpen();
122122

123123
expect($menuLink.bigSlideAPI.model.state).toEqual('open');
124-
expect($menu[0].style.right).toBe("0px");
124+
expect($menu[0].style.left).toBe("0px");
125125
expect($menuLink).toHaveClass('active');
126126
});
127127

@@ -130,8 +130,53 @@ describe('exposed internal components', function(){
130130
$menuLink.bigSlideAPI.view.toggleClose();
131131

132132
expect($menuLink.bigSlideAPI.model.state).toEqual('closed');
133-
expect($menu[0].style.right).toBe("-200px");
133+
expect($menu[0].style.left).toBe("-200px");
134134
expect($menuLink).not.toHaveClass('active');
135135
});
136136

137-
});
137+
});
138+
139+
describe('destroy method', function(){
140+
var $menuLink;
141+
var $panel;
142+
var $push;
143+
144+
beforeEach(function() {
145+
loadFixtures('test.html');
146+
147+
$panel = $('.panel').css('color', 'blue');
148+
$push = $('.push').css('color', 'red');
149+
150+
$menuLink = $('.menu-link').bigSlide({
151+
side:'left',
152+
menuWidth: '200px'
153+
});
154+
155+
$menuLink.trigger('click.bigSlide');
156+
157+
$menuLink.bigSlideAPI.controller.destroy();
158+
});
159+
160+
it('the jQuery object should not have a bigSlide property', function(){
161+
expect($menuLink.bigSlideAPI).not.toBeDefined();
162+
});
163+
164+
it('inline CSS applied by bigSlide should be removed', function(){
165+
expect($push[0].style.left).toBe('');
166+
expect($panel[0].style.left).toBe('');
167+
});
168+
169+
it('inline CSS not applied by bigSlide should be preserved', function(){
170+
expect($push[0].style.cssText.trim()).toBe('color: red;');
171+
expect($panel[0].style.cssText.trim()).toBe('color: blue;');
172+
});
173+
174+
it('the menu link should not have the active class', function(){
175+
expect($menuLink.hasClass('active')).toBe(false);
176+
});
177+
178+
it('no event handlers should be bound to the menu link', function(){
179+
expect($._data($menuLink[0], 'events')).toBe(undefined);
180+
});
181+
});
182+

0 commit comments

Comments
 (0)