-
Notifications
You must be signed in to change notification settings - Fork 208
/
jquery.seat-charts.js
627 lines (527 loc) · 20 KB
/
jquery.seat-charts.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*!
* jQuery-Seat-Charts v1.1.5
* https://github.com/mateuszmarkowski/jQuery-Seat-Charts
*
* Copyright 2013, 2016 Mateusz Markowski
* Released under the MIT license
*/
(function($) {
//'use strict';
$.fn.seatCharts = function (setup) {
//if there's seatCharts object associated with the current element, return it
if (this.data('seatCharts')) {
return this.data('seatCharts');
}
var fn = this,
seats = {},
seatIds = [],
legend,
settings = {
animate : false, //requires jQuery UI
naming : {
top : true,
left : true,
getId : function(character, row, column) {
return row + '_' + column;
},
getLabel : function (character, row, column) {
return column;
}
},
legend : {
node : null,
items : []
},
click : function() {
if (this.status() == 'available') {
return 'selected';
} else if (this.status() == 'selected') {
return 'available';
} else {
return this.style();
}
},
focus : function() {
if (this.status() == 'available') {
return 'focused';
} else {
return this.style();
}
},
blur : function() {
return this.status();
},
seats : {}
},
//seat will be basically a seat object which we'll when generating the map
seat = (function(seatCharts, seatChartsSettings) {
return function (setup) {
var fn = this;
fn.settings = $.extend({
status : 'available', //available, unavailable, selected
style : 'available',
//make sure there's an empty hash if user doesn't pass anything
data : seatChartsSettings.seats[setup.character] || {}
//anything goes here?
}, setup);
fn.settings.$node = $('<div></div>');
fn.settings.$node
.attr({
id : fn.settings.id,
role : 'checkbox',
'aria-checked' : false,
focusable : true,
tabIndex : -1 //manual focus
})
.text(fn.settings.label)
.addClass(['seatCharts-seat', 'seatCharts-cell', 'available'].concat(
//let's merge custom user defined classes with standard JSC ones
fn.settings.classes,
typeof seatChartsSettings.seats[fn.settings.character] == "undefined" ?
[] : seatChartsSettings.seats[fn.settings.character].classes
).join(' '));
//basically a wrapper function
fn.data = function() {
return fn.settings.data;
};
fn.char = function() {
return fn.settings.character;
};
fn.node = function() {
return fn.settings.$node;
};
/*
* Can either set or return status depending on arguments.
*
* If there's no argument, it will return the current style.
*
* If you pass an argument, it will update seat's style
*/
fn.style = function() {
return arguments.length == 1 ?
(function(newStyle) {
var oldStyle = fn.settings.style;
//if nothing changes, do nothing
if (newStyle == oldStyle) {
return oldStyle;
}
//focused is a special style which is not associated with status
fn.settings.status = newStyle != 'focused' ? newStyle : fn.settings.status;
fn.settings.$node
.attr('aria-checked', newStyle == 'selected');
//if user wants to animate status changes, let him do this
seatChartsSettings.animate ?
fn.settings.$node.switchClass(oldStyle, newStyle, 200) :
fn.settings.$node.removeClass(oldStyle).addClass(newStyle);
return fn.settings.style = newStyle;
})(arguments[0]) : fn.settings.style;
};
//either set or retrieve
fn.status = function() {
return fn.settings.status = arguments.length == 1 ?
fn.style(arguments[0]) : fn.settings.status;
};
//using immediate function to convienietly get shortcut variables
(function(seatSettings, character, seat) {
//attach event handlers
$.each(['click', 'focus', 'blur'], function(index, callback) {
//we want to be able to call the functions for each seat object
fn[callback] = function() {
if (callback == 'focus') {
//if there's already a focused element, we have to remove focus from it first
if (seatCharts.attr('aria-activedescendant') !== undefined) {
seats[seatCharts.attr('aria-activedescendant')].blur();
}
seatCharts.attr('aria-activedescendant', seat.settings.id);
seat.node().focus();
}
/*
* User can pass his own callback function, so we have to first check if it exists
* and if not, use our default callback.
*
* Each callback function is executed in the current seat context.
*/
return fn.style(typeof seatSettings[character][callback] === 'function' ?
seatSettings[character][callback].apply(seat) : seatChartsSettings[callback].apply(seat));
};
});
//the below will become seatSettings, character, seat thanks to the immediate function
})(seatChartsSettings.seats, fn.settings.character, fn);
fn.node()
//the first three mouse events are simple
.on('click', fn.click)
.on('mouseenter', fn.focus)
.on('mouseleave', fn.blur)
//keydown requires quite a lot of logic, because we have to know where to move the focus
.on('keydown', (function(seat, $seat) {
return function (e) {
var $newSeat;
//everything depends on the pressed key
switch (e.which) {
//spacebar will just trigger the same event mouse click does
case 32:
e.preventDefault();
seat.click();
break;
//UP & DOWN
case 40:
case 38:
e.preventDefault();
/*
* This is a recursive, immediate function which searches for the first "focusable" row.
*
* We're using immediate function because we want a convenient access to some DOM elements
* We're using recursion because sometimes we may hit an empty space rather than a seat.
*
*/
$newSeat = (function findAvailable($rows, $seats, $currentRow) {
var $newRow;
//let's determine which row should we move to
if (!$rows.index($currentRow) && e.which == 38) {
//if this is the first row and user has pressed up arrow, move to the last row
$newRow = $rows.last();
} else if ($rows.index($currentRow) == $rows.length-1 && e.which == 40) {
//if this is the last row and user has pressed down arrow, move to the first row
$newRow = $rows.first();
} else {
//using eq to get an element at the desired index position
$newRow = $rows.eq(
//if up arrow, then decrement the index, if down increment it
$rows.index($currentRow) + (e.which == 38 ? (-1) : (+1))
);
}
//now that we know the row, let's get the seat using the current column position
$newSeat = $newRow.find('.seatCharts-seat,.seatCharts-space').eq($seats.index($seat));
//if the seat we found is a space, keep looking further
return $newSeat.hasClass('seatCharts-space') ?
findAvailable($rows, $seats, $newRow) : $newSeat;
})($seat
//get a reference to the parent container and then select all rows but the header
.parents('.seatCharts-container')
.find('.seatCharts-row:not(.seatCharts-header)'),
$seat
//get a reference to the parent row and then find all seat cells (both seats & spaces)
.parents('.seatCharts-row:first')
.find('.seatCharts-seat,.seatCharts-space'),
//get a reference to the current row
$seat.parents('.seatCharts-row:not(.seatCharts-header)')
);
//we couldn't determine the new seat, so we better give up
if (!$newSeat.length) {
return;
}
//remove focus from the old seat and put it on the new one
seat.blur();
seats[$newSeat.attr('id')].focus();
$newSeat.focus();
//update our "aria" reference with the new seat id
seatCharts.attr('aria-activedescendant', $newSeat.attr('id'));
break;
//LEFT & RIGHT
case 37:
case 39:
e.preventDefault();
/*
* The logic here is slightly different from the one for up/down arrows.
* User will be able to browse the whole map using just left/right arrow, because
* it will move to the next row when we reach the right/left-most seat.
*/
$newSeat = (function($seats) {
if (!$seats.index($seat) && e.which == 37) {
//user has pressed left arrow and we're currently on the left-most seat
return $seats.last();
} else if ($seats.index($seat) == $seats.length -1 && e.which == 39) {
//user has pressed right arrow and we're currently on the right-most seat
return $seats.first();
} else {
//simply move one seat left or right depending on the key
return $seats.eq($seats.index($seat) + (e.which == 37 ? (-1) : (+1)));
}
})($seat
.parents('.seatCharts-container:first')
.find('.seatCharts-seat:not(.seatCharts-space)'));
if (!$newSeat.length) {
return;
}
//handle focus
seat.blur();
seats[$newSeat.attr('id')].focus();
$newSeat.focus();
//update our "aria" reference with the new seat id
seatCharts.attr('aria-activedescendant', $newSeat.attr('id'));
break;
default:
break;
}
};
})(fn, fn.node()));
//.appendTo(seatCharts.find('.' + row));
}
})(fn, settings);
fn.addClass('seatCharts-container');
//true -> deep copy!
$.extend(true, settings, setup);
//Generate default row ids unless user passed his own
settings.naming.rows = settings.naming.rows || (function(length) {
var rows = [];
for (var i = 1; i <= length; i++) {
rows.push(i);
}
return rows;
})(settings.map.length);
//Generate default column ids unless user passed his own
settings.naming.columns = settings.naming.columns || (function(length) {
var columns = [];
for (var i = 1; i <= length; i++) {
columns.push(i);
}
return columns;
})(settings.map[0].split('').length);
if (settings.naming.top) {
var $headerRow = $('<div></div>')
.addClass('seatCharts-row seatCharts-header');
if (settings.naming.left) {
$headerRow.append($('<div></div>').addClass('seatCharts-cell'));
}
$.each(settings.naming.columns, function(index, value) {
$headerRow.append(
$('<div></div>')
.addClass('seatCharts-cell')
.text(value)
);
});
}
fn.append($headerRow);
//do this for each map row
$.each(settings.map, function(row, characters) {
var $row = $('<div></div>').addClass('seatCharts-row');
if (settings.naming.left) {
$row.append(
$('<div></div>')
.addClass('seatCharts-cell seatCharts-space')
.text(settings.naming.rows[row])
);
}
/*
* Do this for each seat (letter)
*
* Now users will be able to pass custom ID and label which overwrite the one that seat would be assigned by getId and
* getLabel
*
* New format is like this:
* a[ID,label]a[ID]aaaaa
*
* So you can overwrite the ID or label (or both) even for just one seat.
* Basically ID should be first, so if you want to overwrite just label write it as follows:
* a[,LABEL]
*
* Allowed characters in IDs areL 0-9, a-z, A-Z, _
* Allowed characters in labels are: 0-9, a-z, A-Z, _, ' ' (space)
*
*/
$.each(characters.match(/[a-z_]{1}(\[[0-9a-z_]{0,}(,[0-9a-z_ ]+)?\])?/gi), function (column, characterParams) {
var matches = characterParams.match(/([a-z_]{1})(\[([0-9a-z_ ,]+)\])?/i),
//no matter if user specifies [] params, the character should be in the second element
character = matches[1],
//check if user has passed some additional params to override id or label
params = typeof matches[3] !== 'undefined' ? matches[3].split(',') : [],
//id param should be first
overrideId = params.length ? params[0] : null,
//label param should be second
overrideLabel = params.length === 2 ? params[1] : null;
$row.append(character != '_' ?
//if the character is not an underscore (empty space)
(function(naming) {
//so users don't have to specify empty objects
settings.seats[character] = character in settings.seats ? settings.seats[character] : {};
var id = overrideId ? overrideId : naming.getId(character, naming.rows[row], naming.columns[column]);
seats[id] = new seat({
id : id,
label : overrideLabel ?
overrideLabel : naming.getLabel(character, naming.rows[row], naming.columns[column]),
row : row,
column : column,
character : character
});
seatIds.push(id);
return seats[id].node();
})(settings.naming) :
//this is just an empty space (_)
$('<div></div>').addClass('seatCharts-cell seatCharts-space')
);
});
fn.append($row);
});
//if there're any legend items to be rendered
settings.legend.items.length ? (function(legend) {
//either use user-defined container or create our own and insert it right after the seat chart div
var $container = (legend.node || $('<div></div>').insertAfter(fn))
.addClass('seatCharts-legend');
var $ul = $('<ul></ul>')
.addClass('seatCharts-legendList')
.appendTo($container);
$.each(legend.items, function(index, item) {
$ul.append(
$('<li></li>')
.addClass('seatCharts-legendItem')
.append(
$('<div></div>')
//merge user defined classes with our standard ones
.addClass(['seatCharts-seat', 'seatCharts-cell', item[1]].concat(
settings.classes,
typeof settings.seats[item[0]] == "undefined" ? [] : settings.seats[item[0]].classes).join(' ')
)
)
.append(
$('<span></span>')
.addClass('seatCharts-legendDescription')
.text(item[2])
)
);
});
return $container;
})(settings.legend) : null;
fn.attr({
tabIndex : 0
});
//when container's focused, move focus to the first seat
fn.focus(function() {
if (fn.attr('aria-activedescendant')) {
seats[fn.attr('aria-activedescendant')].blur();
}
fn.find('.seatCharts-seat:not(.seatCharts-space):first').focus();
seats[seatIds[0]].focus();
});
//public methods of seatCharts
fn.data('seatCharts', {
seats : seats,
seatIds : seatIds,
//set for one, set for many, get for one
status: function() {
var fn = this;
return arguments.length == 1 ? fn.seats[arguments[0]].status() : (function(seatsIds, newStatus) {
return typeof seatsIds == 'string' ? fn.seats[seatsIds].status(newStatus) : (function() {
$.each(seatsIds, function(index, seatId) {
fn.seats[seatId].status(newStatus);
});
})();
})(arguments[0], arguments[1]);
},
each : function(callback) {
var fn = this;
for (var seatId in fn.seats) {
if (false === callback.call(fn.seats[seatId], seatId)) {
return seatId;//return last checked
}
}
return true;
},
node : function() {
var fn = this;
//basically create a CSS query to get all seats by their DOM ids
return $('#' + fn.seatIds.join(',#'));
},
find : function(query) {//D, a.available, unavailable
var fn = this;
var seatSet = fn.set();
//is RegExp
return query instanceof RegExp ?
(function () {
fn.each(function (id) {
if (id.match(query)) {
seatSet.push(id, this);
}
});
return seatSet;
})() :
(query.length == 1 ?
(function (character) {
//user searches just for a particual character
fn.each(function () {
if (this.char() == character) {
seatSet.push(this.settings.id, this);
}
});
return seatSet;
})(query) :
(function () {
//user runs a more sophisticated query, so let's see if there's a dot
return query.indexOf('.') > -1 ?
(function () {
//there's a dot which separates character and the status
var parts = query.split('.');
fn.each(function (seatId) {
if (this.char() == parts[0] && this.status() == parts[1]) {
seatSet.push(this.settings.id, this);
}
});
return seatSet;
})() :
(function () {
fn.each(function () {
if (this.status() == query) {
seatSet.push(this.settings.id, this);
}
});
return seatSet;
})();
})()
);
},
set : function set() {//inherits some methods
var fn = this;
return {
seats : [],
seatIds : [],
length : 0,
status : function() {
var args = arguments,
that = this;
//if there's just one seat in the set and user didn't pass any params, return current status
return this.length == 1 && args.length == 0 ? this.seats[0].status() : (function() {
//otherwise call status function for each of the seats in the set
$.each(that.seats, function() {
this.status.apply(this, args);
});
})();
},
node : function() {
return fn.node.call(this);
},
each : function() {
return fn.each.call(this, arguments[0]);
},
get : function() {
return fn.get.call(this, arguments[0]);
},
find : function() {
return fn.find.call(this, arguments[0]);
},
set : function() {
return set.call(fn);
},
push : function(id, seat) {
this.seats.push(seat);
this.seatIds.push(id);
++this.length;
}
};
},
//get one object or a set of objects
get : function(seatsIds) {
var fn = this;
return typeof seatsIds == 'string' ?
fn.seats[seatsIds] : (function() {
var seatSet = fn.set();
$.each(seatsIds, function(index, seatId) {
if (typeof fn.seats[seatId] === 'object') {
seatSet.push(seatId, fn.seats[seatId]);
}
});
return seatSet;
})();
}
});
return fn.data('seatCharts');
}
})(jQuery);