-
Notifications
You must be signed in to change notification settings - Fork 0
/
listingform-accordion.js
179 lines (132 loc) · 4.17 KB
/
listingform-accordion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 2014-08-11
// Depends on jquery and cookie.js
function AccordionMemory( opts )
{
var $ = jQuery;
this.headerClassSelector = '.section-header';
this.wrapperClassSelector = '.section-wrapper';
this.sectionHeaders = [];
this.sectionWrappers = [];
this.sectionHeaders = $( this.headerClassSelector );
this.sectionWrappers = $( this.wrapperClassSelector );
// Create a cookie to store pinned open form section preferences in an array
this.cookie = new Cookie('realtycore', {expires: 5} );
if( !this.cookie.get('keepOpen') ) { this.cookie.set('keepOpen',[]); }
this.construct = function()
{
// close all sections initially (unless specified in cookie/storage to stay open)
this.closeUnPinnedSections();
this.watchToggleAll();
// watch all section headers for clicks
this.watchSectionHeaders();
this.watchSectionPins();
};
// Closes form sections EXCEPT those the user wants to keep open all the time
// user choice stored in cookie or local storage
this.closeUnPinnedSections = function()
{
var cookieData = this.cookie.get('keepOpen');
//console.log(this);
this.sectionWrappers.each( function( index, node )
{
$node = $(node);
// if sections node.id is Not found in cookie, hide section
if( cookieData.indexOf( node.id ) < 0 )
{ node.style.display="none"; }
// if it is found, change class of section-pin element to indicate it's pinned open
else
{
// find this sections wrappers section-header & section-pin
var header = $node.prev();
var pin = header.find('.section-pin');
// change section pin class name
pin.toggleClass('pinned');
}
} );
}
// observe the expand/collapse all button
this.watchToggleAll = function()
{
var api = this;
$('#toggleAll').click( function(clik){ api.toggleAllSections(clik); } );
}
this.watchSectionHeaders = function()
{
var api = this;
this.sectionHeaders.click(
function(clickevent)
{
api.toggleSection( clickevent.target );
}
)
}
this.watchSectionPins = function()
{
var api = this;
$('.section-pin').click(
function(clickevent)
{
api.storeSectionPref( clickevent.target );
}
)
}
this.toggleSection = function( header )
{
// find next sibling that wraps the form fields and expand it
var wrapper = $(header).next('.section-wrapper');
wrapper.slideToggle("0.1");
}
this.toggleAllSections = function (clickevent)
{
if( typeof allToggleState === 'undefined' )
{
// base initial toggle state on whatever the first section is doing
var wrappers = this.sectionWrappers;
if(wrappers[0].style.display === '' )
{ allToggleState = 'openstate'; } //open
else { allToggleState = 'closedstate'; } //close
}
if( allToggleState === 'closedstate' )
{ this.expandAllSections(); allToggleState = 'openstate'; }
else { this.collapseAllSections(); allToggleState = 'closedstate'; }
}
// forces all sections to close
this.collapseAllSections = function ()
{
this.sectionWrappers.slideUp();
}
// forces all sections to open
this.expandAllSections = function ()
{
this.sectionWrappers.slideDown();
}
this.storeSectionPref = function( element )
{
$element = $(element);
// if text of element at time of clicking said "keep open"
// then keep it open
var text = $element.text();
//console.log(text);
var section = $element.parent().next('.section-wrapper');
//console.log(section);
var cookieData = this.cookie.get('keepOpen');
//console.log(cookieData);
// if element does not already have classname "pinned", add its section id to list of keepOpen sections in cookie
if( $element.hasClass('pinned') == false )
{
if(cookieData.indexOf( section[0].id ) < 0 ) { cookieData.push( section[0].id ) }
}
// if element already has the classname 'pinned' remove its section id from list of keepOpen sections in cookie
else {
var find = cookieData.indexOf( section[0].id );
cookieData.splice( find, 1 ); // delete it from array
}
// Inspect newest cookie data based changes that may have happened above.
console.log(cookieData);
// The pinned class is toggled every time a pin is clicked.
$element.toggleClass('pinned')
// Replace keepOpen array in cookie with latest info.
this.cookie.set('keepOpen', cookieData);
}
this.construct();
} // end LFJS app object wrapper