-
Notifications
You must be signed in to change notification settings - Fork 17
/
browse-columns.js
370 lines (333 loc) · 11.4 KB
/
browse-columns.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
(function() {
"use strict";
window.GOVUK = window.GOVUK || {};
var $ = window.jQuery;
function BrowseColumns(options){
if(options.$el.length === 0) return;
if(!GOVUK.support.history()) return;
if($(window).width() < 640) return; // don't ajax navigation on mobile
this.$el = options.$el;
this.$root = this.$el.find('#root');
this.$section = this.$el.find('#section');
this.$subsection = this.$el.find('#subsection');
this.$breadcrumbs = $('.govuk-breadcrumbs');
this.animateSpeed = 330;
if(this.$section.length === 0){
this.$section = $('<div id="section" class="section-pane pane with-sort" />');
this.$el.prepend(this.$section);
this.$el.addClass('section');
}
if(this.$subsection.length === 0){
this.$subsection = $('<div id="subsection" class="subsection-pane pane" />').hide();
this.$el.prepend(this.$subsection);
} else {
this.$subsection.show();
}
this.displayState = this.$el.data('state');
if(typeof this.displayState === 'undefined'){
this.displayState = 'root';
}
this._cache = {};
this.lastState = this.parsePathname(window.location.pathname);
this.$el.on('click', 'a', this.navigate.bind(this));
$(window).on('popstate', this.popState.bind(this));
}
BrowseColumns.prototype = {
popState: function(e){
var state = e.originalEvent.state,
loadPromise;
if(!state){ // state will be null if there was no state set
state = this.parsePathname(window.location.pathname);
}
if(this.lastState.slug === state.slug){
return; // nothing has changed
}
if(state.slug == ''){
loadPromise = this.showRoot();
} else if(state.subsection){
loadPromise = this.restoreSubsection(state);
} else {
loadPromise = this.loadSectionFromState(state, true);
}
loadPromise.then(function(){
this.trackPageview(state);
}.bind(this));
},
restoreSubsection: function(state){
// are we displaying the correct section for the subsection?
if(this.lastState.section != state.section){
// load the section then load the subsection after
var sectionPathname = window.location.pathname.split('/').slice(0,-1).join('/');
var sectionState = this.parsePathname(sectionPathname);
return this.loadSectionFromState(sectionState, true)
.then(function(){
return this.loadSectionFromState(state, true);
}.bind(this));
} else {
return this.loadSectionFromState(state, true);
}
},
sectionCache: function(prefix, slug, data){
if(typeof data === 'undefined'){
return this._cache[prefix + slug];
} else {
this._cache[prefix + slug] = data;
}
},
isDesktop: function(){
return $(window).width() > 768;
},
showRoot: function(){
this.$section.html('');
this.displayState = 'root';
this.$root.find('h1').focus();
var out = new $.Deferred()
return out.resolve();
},
showSection: function(state){
state.title = this.getTitle(state.slug);
this.setTitle(state.title);
this.$section.html(state.sectionData.html)
this.highlightSection('root', state.path);
this.removeLoading();
this.updateBreadcrumbs(state);
var animationDone;
if(this.displayState === 'subsection'){
// animate to the right position and update the data
animationDone = this.animateSubsectionToSectionDesktop();
} else if(this.displayState === 'root'){
animationDone = this.animateRootToSectionDesktop();
} else {
animationDone = new $.Deferred();
animationDone.resolve();
}
return animationDone.then(function(){
this.$section.find('h1').focus();
}.bind(this));
},
animateSubsectionToSectionDesktop: function(){
var out = new $.Deferred()
function afterAnimate(){
this.displayState = 'section';
this.$el.removeClass('subsection').addClass('section');
this.$section.attr('style', '');
this.$section.find('.pane-inner').attr('style', '');
this.$section.addClass('with-sort');
this.$root.attr('style', '');
out.resolve();
}
// animate to the right position and update the data
this.$root.css({ position: 'absolute', width: this.$root.width() });
this.$subsection.hide();
this.$section.css('margin-right', '63%');
if(this.isDesktop()){
this.$section.find('.pane-inner.curated').animate({
paddingLeft: '30px'
}, this.animateSpeed);
this.$section.find('.pane-inner.alphabetical').animate({
paddingLeft: '96px'
}, this.animateSpeed);
var sectionProperties = {
width: '35%',
marginLeft: '0%',
marginRight: '40%'
};
} else {
var sectionProperties = {
width: '30%',
marginLeft: '0%',
marginRight: '45%'
};
}
this.$section.animate(sectionProperties, this.animateSpeed, afterAnimate.bind(this));
return out;
},
animateRootToSectionDesktop: function(){
var out = new $.Deferred()
this.displayState = 'section';
this.$el.removeClass('subsection').addClass('section');
return out.resolve();
},
showSubsection: function(state){
state.title = this.getTitle(state.slug);
this.setTitle(state.title);
this.$subsection.html(state.sectionData.html);
this.highlightSection('section', state.path);
this.highlightSection('root', '/browse/' + state.section);
this.removeLoading();
this.updateBreadcrumbs(state);
var animationDone;
if(this.displayState !== 'subsection'){
animationDone = this.animateSectionToSubsectionDesktop();
} else {
animationDone = new $.Deferred();
animationDone.resolve();
}
return animationDone.then(function(){
this.$subsection.find('h1').focus();
}.bind(this));
},
animateSectionToSubsectionDesktop: function(){
var out = new $.Deferred();
// animate to the right position and update the data
this.$root.css({ position: 'absolute', width: this.$root.width() });
this.$section.find('.sort-order').hide();
this.$section.find('.pane-inner').animate({
paddingLeft: '0'
}, this.animateSpeed);
if(this.isDesktop()){
var sectionProperties = {
width: '25%',
marginLeft: '-13%',
marginRight: '63%'
};
} else {
var sectionProperties = {
width: '30%',
marginLeft: '-18%',
marginRight: '63%'
};
}
this.$section.animate(sectionProperties, this.animateSpeed, function(){
this.$el.removeClass('section').addClass('subsection');
this.$subsection.show();
this.$section.removeClass('with-sort');
this.displayState = 'subsection';
this.$section.find('.sort-order').attr('style', '');
this.$section.attr('style', '');
this.$section.find('.pane-inner').attr('style', '');
this.$root.attr('style', '');
out.resolve();
}.bind(this));
return out;
},
getTitle: function(slug){
var $link = this.$el.find('a[href$="/browse/'+slug+'"]:first'),
$heading = $link.find('h3');
if($heading.length > 0){
return $heading.text();
} else {
return $link.text();
}
},
setTitle: function(title){
$('title').text(title);
},
addLoading: function($el){
this.$el.attr('aria-busy', 'true');
$el.addClass('loading');
},
removeLoading: function(){
this.$el.attr('aria-busy', 'false');
this.$el.find('a.loading').removeClass('loading');
},
// getSectionData: returns a promise which will contain the data
// Returns data from the cache if it is there or puts the data in the cache
// if it is not.
getSectionData: function(state){
var cacheForSlug = this.sectionCache('section', state.slug),
out = new $.Deferred(),
url = '/browse/' + state.slug + '.json';
if(typeof state.sectionData !== 'undefined'){
out.resolve(state.sectionData);
} else if(typeof cacheForSlug !== 'undefined'){
out.resolve(cacheForSlug);
} else {
$.ajax({
url: url
}).then(function(data){
this.sectionCache('section', state.slug, data);
out.resolve(data);
}.bind(this));
}
return out;
},
highlightSection: function(section, slug){
this.$el.find('#'+section+' .active').removeClass('active')
this.$el.find('a[href$="'+slug+'"]').parent().addClass('active');
},
parsePathname: function(pathname){
var out = {
path: pathname,
slug: pathname.replace(/\/browse\/?/, '')
};
if(out.slug.indexOf('/') > -1){
out.section = out.slug.split('/')[0];
out.subsection = out.slug.split('/')[1];
} else {
out.section = out.slug;
}
return out;
},
scrollToBrowse: function(){
var $body = $('body'),
elTop = this.$el.offset().top;
if( $body.scrollTop() > elTop ){
$('body').animate({scrollTop: elTop}, this.animateSpeed);
}
},
loadSectionFromState: function(state, poppingState){
var donePromise = this.getSectionData(state);
if(state.subsection){
var sectionPromise = donePromise;
donePromise = $.when(sectionPromise);
}
return donePromise
.then(function(sectionData){
state.sectionData = sectionData;
this.scrollToBrowse();
this.lastState = state;
if(state.subsection){
return this.showSubsection(state);
} else {
return this.showSection(state);
}
}.bind(this))
.then(function() {
if(typeof poppingState === 'undefined'){
history.pushState(state, '', state.path);
this.trackPageview(state);
}
}.bind(this));
},
navigate: function(e){
if(e.currentTarget.pathname.match(/^\/browse\/[^\/]+(\/[^\/]+)?$/)){
var $target = $(e.currentTarget);
e.preventDefault();
var state = this.parsePathname(e.currentTarget.pathname);
state.title = $target.text();
if(state.path === window.location.pathname){
return;
}
this.addLoading($target);
this.loadSectionFromState(state);
}
},
updateBreadcrumbs: function(state) {
var $newBreadcrumbs = $(state.sectionData.breadcrumbs);
this.$breadcrumbs.html($newBreadcrumbs.html());
},
trackPageview: function(state){
var sectionTitle = this.$section.find('h1').text();
sectionTitle = sectionTitle ? sectionTitle.toLowerCase() : 'browse';
var navigationPageType = 'none';
if(this.displayState == 'section') {
navigationPageType = 'First Level Browse';
} else if(this.displayState == 'subsection') {
navigationPageType = 'Second Level Browse';
}
if (GOVUK.analytics && GOVUK.analytics.trackPageview) {
GOVUK.analytics.trackPageview(
state.path,
null,
{
dimension1: sectionTitle,
dimension32: navigationPageType
}
);
}
}
};
GOVUK.BrowseColumns = BrowseColumns;
$(function(){ GOVUK.browseColumns = new BrowseColumns({$el: $('.browse-panes')}); })
}());