-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathskel.js
1044 lines (738 loc) · 23.1 KB
/
skel.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* skel.js v3.0.2-dev | (c) skel.io | MIT licensed */
var skel = (function() { "use strict"; var _ = {
/******************************/
/* Properties */
/******************************/
/**
* IDs of breakpoints that are currently active.
* @type {array}
*/
breakpointIds: null,
/**
* Events.
* @type {object}
*/
events: {},
/**
* Are we initialized?
* @type {bool}
*/
isInit: false,
/**
* Objects.
* @type {object}
*/
obj: {
// Attachments.
attachments: {},
// Breakpoints.
breakpoints: {},
// Head.
head: null,
// States.
states: {}
},
/**
* State ID delimiter (don't change this).
* @type {string}
*/
sd: '/',
/**
* Current state.
* @type {object}
*/
state: null,
/**
* State handlers.
* @type {object}
*/
stateHandlers: {},
/**
* Current state ID.
* @type {string}
*/
stateId: '',
/**
* Internal vars.
* @type {object}
*/
vars: {},
/******************************/
/* Methods: Utility */
/******************************/
/**
* Does stuff when the DOM is ready.
* @param {function} f Function.
*/
DOMReady: null,
/**
* Wrapper/polyfill for (Array.prototype|String).indexOf.
* @param {Array|string} search Object or string to search.
* @param {integer} from Starting index.
* @return {integer} Matching index (or -1 if there's no match).
*/
indexOf: null,
/**
* Wrapper/polyfill for Array.isArray.
* @param {array} x Variable to check.
* @return {bool} If true, x is an array. If false, x is not an array.
*/
isArray: null,
/**
* Safe replacement for "for..in". Avoids stuff that doesn't belong to the array itself (eg. properties added to Array.prototype).
* @param {Array} a Array to iterate.
* @param {function} f(index) Function to call on each element.
*/
iterate: null,
/**
* Determines if a media query matches the current browser state.
* @param {string} query Media query.
* @return {bool} True if it matches, false if not.
*/
matchesMedia: null,
/**
* Extends x by y.
* @param {object} x Target object.
* @param {object} y Source object.
*/
extend: function(x, y) {
_.iterate(y, function(k) {
if (_.isArray(y[k])) {
if (!_.isArray(x[k]))
x[k] = [];
_.extend(x[k], y[k]);
}
else if (typeof y[k] == 'object') {
if (typeof x[k] != 'object')
x[k] = {};
_.extend(x[k], y[k]);
}
else
x[k] = y[k];
});
},
/**
* Creates a new style element.
* @param {string} content Content.
* @return {DOMHTMLElement} Style element.
*/
newStyle: function(content) {
var e = document.createElement('style');
e.type = 'text/css';
e.innerHTML = content;
return e;
},
/******************************/
/* Methods: API */
/******************************/
/**
* Temporary element for canUse()
* @type {DOMElement}
*/
_canUse: null,
/**
* Determines if the browser supports a given property.
* @param {string} p Property.
* @return {bool} True if property is supported, false if not.
*/
canUse: function(p) {
// Create temporary element if it doesn't already exist.
if (!_._canUse)
_._canUse = document.createElement('div');
// Check for property.
var e = _._canUse.style,
up = p.charAt(0).toUpperCase() + p.slice(1);
return (
p in e
|| ('Moz' + up) in e
|| ('Webkit' + up) in e
|| ('O' + up) in e
|| ('ms' + up) in e
);
},
/******************************/
/* Methods: Events */
/******************************/
/**
* Registers one or more events.
* @param {string} names Space-delimited list of event names.
* @param {function} f Function.
*/
on: function(names, f) {
var a = names.split(/[\s]+/);
_.iterate(a, function(i) {
var name = a[i];
// Manually trigger event if applicable.
if (_.isInit) {
// Init.
if (name == 'init') {
// Trigger event.
(f)();
// This only gets called once, so there's no need to actually
// register it.
return;
}
// Change.
else if (name == 'change') {
// Trigger event.
(f)();
}
// Activate / Not.
else {
var x = name.charAt(0);
if (x == '+' || x == '!') {
var y = name.substring(1);
if (y in _.obj.breakpoints) {
// Activate.
if (x == '+' && _.obj.breakpoints[y].active) {
// Trigger event.
(f)();
}
// Not.
else if (x == '!' && !_.obj.breakpoints[y].active) {
// Trigger event.
(f)();
// This only gets called once, so there's no need to actually
// register it.
return;
}
}
}
}
}
// No previous events of this type registered? Set up its array.
if (!_.events[name])
_.events[name] = [];
// Register event.
_.events[name].push(f);
});
return _;
},
/**
* Triggers an event.
* @param {string} name Name.
*/
trigger: function(name) {
// No events registered? Bail.
if (!_.events[name] || _.events[name].length == 0)
return;
// Step through and call events.
_.iterate(_.events[name], function(k) {
(_.events[name][k])();
});
return _;
},
/******************************/
/* Methods: Breakpoints */
/******************************/
/**
* Gets a breakpoint.
* @param {string} id Breakpoint ID.
* @return {Breakpoint} Breakpoint.
*/
breakpoint: function(id) {
return _.obj.breakpoints[id];
},
/**
* Sets breakpoints.
* @param {object} breakpoints Breakpoints.
*/
breakpoints: function(breakpoints) {
// Breakpoint class.
function Breakpoint(id, media) {
this.name = this.id = id;
this.media = media;
this.active = false;
this.wasActive = false;
};
Breakpoint.prototype.matches = function() {
return (_.matchesMedia(this.media));
};
Breakpoint.prototype.sync = function() {
this.wasActive = this.active;
this.active = this.matches();
};
// Create breakpoints.
_.iterate(breakpoints, function(id) {
_.obj.breakpoints[id] = new Breakpoint(id, breakpoints[id]);
});
// Initial poll.
window.setTimeout(function() {
_.poll();
}, 0);
return _;
},
/******************************/
/* Methods: States */
/******************************/
/**
* Adds a state handler.
* @param {string} id ID.
* @param {function} f Handler function.
*/
addStateHandler: function(id, f) {
// Add handler.
_.stateHandlers[id] = f;
// Call it.
//_.callStateHandler(id);
},
/**
* Calls a state handler.
* @param {string} id ID.
*/
callStateHandler: function(id) {
// Call handler.
var attachments = (_.stateHandlers[id])();
// Add attachments to state (if any).
_.iterate(attachments, function(i) {
_.state.attachments.push(attachments[i]);
});
},
/**
* Switches to a different state.
* @param {string} newStateId New state ID.
*/
changeState: function(newStateId) {
// Sync all breakpoints.
_.iterate(_.obj.breakpoints, function(id) {
_.obj.breakpoints[id].sync();
});
// Set last state var.
_.vars.lastStateId = _.stateId;
// Change state ID.
_.stateId = newStateId;
_.breakpointIds = (_.stateId === _.sd ? [] : _.stateId.substring(1).split(_.sd));
console.log('[skel] changing states (id: "' + _.stateId + '")');
// Get state.
if (!_.obj.states[_.stateId]) {
console.log('[skel] - not found. building ...');
// Build state.
_.obj.states[_.stateId] = {
attachments: []
};
_.state = _.obj.states[_.stateId];
// Call all state handlers.
_.iterate(_.stateHandlers, _.callStateHandler);
}
else {
console.log('[skel] - found');
// Get state.
_.state = _.obj.states[_.stateId];
}
// Detach all attachments *EXCEPT* state's.
_.detachAll(_.state.attachments);
// Attach state's attachments.
_.attachAll(_.state.attachments);
// Expose state and stateId as vars.
_.vars.stateId = _.stateId;
_.vars.state = _.state;
// Trigger change event.
_.trigger('change');
// Trigger activate/deactivate events.
_.iterate(_.obj.breakpoints, function(id) {
// Breakpoint is now active ...
if (_.obj.breakpoints[id].active) {
// ... and it wasn't active before? Trigger activate event.
if (!_.obj.breakpoints[id].wasActive)
_.trigger('+' + id);
}
// Breakpoint is not active ...
else {
// ... but it was active before? Trigger deactivate event.
if (_.obj.breakpoints[id].wasActive)
_.trigger('-' + id);
}
});
},
/**
* Generates a state-specific config.
* @param {object} baseConfig Base config.
* @param {object} breakpointConfigs Breakpoint-specific configs.
* @return {object} State-specific config.
*/
generateStateConfig: function(baseConfig, breakpointConfigs) {
var x = {};
// Extend with base config.
_.extend(x, baseConfig);
// Extend with configs for each active breakpoint.
_.iterate(_.breakpointIds, function(k) {
_.extend(x, breakpointConfigs[_.breakpointIds[k]]);
});
return x;
},
/**
* Gets the current state ID.
* @return {string} State ID.
*/
getStateId: function() {
var stateId = '';
_.iterate(_.obj.breakpoints, function(id) {
var b = _.obj.breakpoints[id];
// Active? Append breakpoint ID to state ID.
if (b.matches())
stateId += _.sd + b.id;
});
return stateId;
},
/**
* Polls for state changes.
*/
poll: function() {
var newStateId = '';
// Determine new state.
newStateId = _.getStateId();
if (newStateId === '')
newStateId = _.sd;
// State changed?
if (newStateId !== _.stateId)
_.changeState(newStateId);
},
/******************************/
/* Methods: Attachments */
/******************************/
/**
* Attach point for attach()
* @type {DOMElement}
*/
_attach: null,
/**
* Attaches a single attachment.
* @param {object} attachment Attachment.
* @return bool True on success, false on failure.
*/
attach: function(attachment) {
var h = _.obj.head,
e = attachment.element;
// Already attached? Bail.
if (e.parentNode
&& e.parentNode.tagName)
return false;
// Add to <head>
// No attach point yet? Use <head>'s first child.
if (!_._attach)
_._attach = h.firstChild;
// Insert element.
h.insertBefore(e, _._attach.nextSibling);
// Permanent attachment? Make its element the new attach point.
if (attachment.permanent)
_._attach = e;
console.log('[skel] ' + attachment.id + ': attached (' + attachment.priority + ')');
return true;
},
/**
* Attaches a list of attachments.
* @param {array} attachments Attachments.
*/
attachAll: function(attachments) {
var a = [];
// Organize attachments by priority.
_.iterate(attachments, function(k) {
if (!a[ attachments[k].priority ])
a[ attachments[k].priority ] = [];
a[ attachments[k].priority ].push(attachments[k]);
});
// Reverse array order.
a.reverse();
// Step through each priority.
_.iterate(a, function(k) {
_.iterate(a[k], function(x) {
_.attach(a[k][x]);
});
});
},
/**
* Detaches a single attachment.
* @param {object} attachment Attachment.
* @return bool True on success, false on failure.
*/
detach: function(attachment) {
var e = attachment.element;
// Permanent or already detached? Bail.
if (attachment.permanent
|| !e.parentNode
|| (e.parentNode && !e.parentNode.tagName))
return false;
// Detach.
e.parentNode.removeChild(e);
return true;
},
/**
* Detaches all attachments.
* @param {object} exclude A list of attachments to exclude.
*/
detachAll: function(exclude) {
var l = {};
// Build exclusion list (for faster lookups).
_.iterate(exclude, function(k) {
l[exclude[k].id] = true;
});
_.iterate(_.obj.attachments, function(id) {
// In our exclusion list? Bail.
if (id in l)
return;
// Attempt to detach.
_.detach(_.obj.attachments[id]);
});
},
attachment: function(id) {
return (id in _.obj.attachments ? _.obj.attachments[id] : null);
},
/**
* Creates a new attachment.
* @param {string} id ID.
* @param {DOMElement} element DOM element.
*/
newAttachment: function(id, element, priority, permanent) {
return (_.obj.attachments[id] = {
id: id,
element: element,
priority: priority,
permanent: permanent
});
},
/******************************/
/* Methods: Init */
/******************************/
/**
* Initializes skel.
* This has to be explicitly called by the user.
*/
init: function() {
// Initialize stuff.
_.initMethods();
_.initVars();
_.initEvents();
// Tmp.
_.obj.head = document.getElementsByTagName('head')[0];
// Mark as initialized.
_.isInit = true;
// Trigger init event.
_.trigger('init');
console.log('[skel] initialized.');
},
/**
* Initializes browser events.
*/
initEvents: function() {
// On resize.
_.on('resize', function() { _.poll(); });
// On orientation change.
_.on('orientationChange', function() { _.poll(); });
// Wrap "ready" event.
_.DOMReady(function() {
_.trigger('ready');
});
// Non-destructively register skel events to window.
// Load.
if (window.onload)
_.on('load', window.onload);
window.onload = function() { _.trigger('load'); };
// Resize.
if (window.onresize)
_.on('resize', window.onresize);
window.onresize = function() { _.trigger('resize'); };
// Orientation change.
if (window.onorientationchange)
_.on('orientationChange', window.onorientationchange);
window.onorientationchange = function() { _.trigger('orientationChange'); };
},
/**
* Initializes methods.
*/
initMethods: function() {
// _.DOMReady (based on github.com/ded/domready by @ded; domready (c) Dustin Diaz 2014 - License MIT)
// Hack: Use older version for browsers that don't support addEventListener (*cough* IE8).
if (!document.addEventListener)
!function(e,t){_.DOMReady = t()}("domready",function(e){function p(e){h=1;while(e=t.shift())e()}var t=[],n,r=!1,i=document,s=i.documentElement,o=s.doScroll,u="DOMContentLoaded",a="addEventListener",f="onreadystatechange",l="readyState",c=o?/^loaded|^c/:/^loaded|c/,h=c.test(i[l]);return i[a]&&i[a](u,n=function(){i.removeEventListener(u,n,r),p()},r),o&&i.attachEvent(f,n=function(){/^c/.test(i[l])&&(i.detachEvent(f,n),p())}),e=o?function(n){self!=top?h?n():t.push(n):function(){try{s.doScroll("left")}catch(t){return setTimeout(function(){e(n)},50)}n()}()}:function(e){h?e():t.push(e)}});
// And everyone else.
else
!function(e,t){_.DOMReady = t()}("domready",function(){function s(t){i=1;while(t=e.shift())t()}var e=[],t,n=document,r="DOMContentLoaded",i=/^loaded|^c/.test(n.readyState);return n.addEventListener(r,t=function(){n.removeEventListener(r,t),s()}),function(t){i?t():e.push(t)}});
// _.indexOf
// Wrap existing method if it exists.
if (Array.prototype.indexOf)
_.indexOf = function(x,b) { return x.indexOf(b) };
// Otherwise, polyfill.
else
_.indexOf = function(x,b){if (typeof x=='string') return x.indexOf(b);var c,a=(b)?b:0,e;if(!this){throw new TypeError()}e=this.length;if(e===0||a>=e){return -1}if(a<0){a=e-Math.abs(a)}for(c=a;c<e;c++){if(this[c]===x){return c}}return -1};
// _.isArray
// Wrap existing method if it exists.
if (Array.isArray)
_.isArray = function(x) { return Array.isArray(x) };
// Otherwise, polyfill.
else
_.isArray = function(x) { return (Object.prototype.toString.call(x) === '[object Array]') };
// _.iterate
// Use Object.keys if it exists (= better performance).
if (Object.keys)
_.iterate = function(a, f) {
if (!a)
return [];
var i, k = Object.keys(a);
for (i = 0; k[i]; i++) {
if ((f)(k[i], a[k[i]]) === false)
break;
}
};
// Otherwise, fall back on hasOwnProperty (= slower, but works on older browsers).
else
_.iterate = function(a, f) {
if (!a)
return [];
var i;
for (i in a)
if (Object.prototype.hasOwnProperty.call(a, i)) {
if ((f)(i, a[i]) === false)
break;
}
};
// _.matchesMedia
// Default: Use matchMedia (all modern browsers)
if (window.matchMedia)
_.matchesMedia = function(query) {
if (query == '')
return true;
return window.matchMedia(query).matches;
};
// Polyfill 1: Use styleMedia/media (IE9, older Webkit) (derived from github.com/paulirish/matchMedia.js)
else if (window.styleMedia || window.media)
_.matchesMedia = function(query) {
if (query == '')
return true;
var styleMedia = (window.styleMedia || window.media);
return styleMedia.matchMedium(query || 'all');
};
// Polyfill 2: Use getComputed Style (???) (derived from github.com/paulirish/matchMedia.js)
else if (window.getComputedStyle)
_.matchesMedia = function(query) {
if (query == '')
return true;
var style = document.createElement('style'),
script = document.getElementsByTagName('script')[0],
info = null;
style.type = 'text/css';
style.id = 'matchmediajs-test';
script.parentNode.insertBefore(style, script);
info = ('getComputedStyle' in window) && window.getComputedStyle(style, null) || style.currentStyle;
var text = '@media ' + query + '{ #matchmediajs-test { width: 1px; } }';
if (style.styleSheet)
style.styleSheet.cssText = text;
else
style.textContent = text;
return info.width === '1px';
};
// Polyfill 3: Manually parse (IE<9)
else
_.matchesMedia = function(query) {
// Empty query? Always succeed.
if (query == '')
return true;
// Parse query.
var k, s, a, b, values = { 'min-width': null, 'max-width': null },
found = false;
a = query.split(/\s+and\s+/);
for (k = 0; k < a.length; k++) {
s = a[k];
// Operator (key: value)
if (s.charAt(0) == '(') {
s = s.substring(1, s.length - 1);
b = s.split(/:\s+/);
if (b.length == 2) {
values[ b[0].replace(/^\s+|\s+$/g, '') ] = parseInt( b[1] );
found = true;
}
}
}
// No matches? Query likely contained something unsupported so we automatically fail.
if (!found)
return false;
// Check against viewport.
var w = document.documentElement.clientWidth,
h = document.documentElement.clientHeight;
if ((values['min-width'] !== null && w < values['min-width'])
|| (values['max-width'] !== null && w > values['max-width'])
|| (values['min-height'] !== null && h < values['min-height'])
|| (values['max-height'] !== null && h > values['max-height']))
return false;
return true;
};
// _.newStyle
// IE<9 fix.
if (navigator.userAgent.match(/MSIE ([0-9]+)/)
&& RegExp.$1 < 9)
_.newStyle = function(content) {
var e = document.createElement('span');
e.innerHTML = ' <style type="text/css">' + content + '</style>';
return e;
};
},
/**
* Initializes the vars.
*/
initVars: function() {
var x, y, a, ua = navigator.userAgent;
// browser, browserVersion.
x = 'other';
y = 0;
a = [
['firefox', /Firefox\/([0-9\.]+)/],
['bb', /BlackBerry.+Version\/([0-9\.]+)/],
['bb', /BB[0-9]+.+Version\/([0-9\.]+)/],
['opera', /OPR\/([0-9\.]+)/],
['opera', /Opera\/([0-9\.]+)/],
['edge', /Edge\/([0-9\.]+)/],
['safari', /Version\/([0-9\.]+).+Safari/],
['chrome', /Chrome\/([0-9\.]+)/],
['ie', /MSIE ([0-9]+)/],
['ie', /Trident\/.+rv:([0-9]+)/]
];
_.iterate(a, function(k, v) {
if (ua.match(v[1])) {
x = v[0];
y = parseFloat(RegExp.$1);
return false;
}
});
_.vars.browser = x;
_.vars.browserVersion = y;
// os, osVersion.
x = 'other';
y = 0;
a = [
['ios', /([0-9_]+) like Mac OS X/, function(v) { return v.replace('_', '.').replace('_', ''); }],
['ios', /CPU like Mac OS X/, function(v) { return 0 }],
['wp', /Windows Phone ([0-9\.]+)/, null],
['android', /Android ([0-9\.]+)/, null],
['mac', /Macintosh.+Mac OS X ([0-9_]+)/, function(v) { return v.replace('_', '.').replace('_', ''); }],
['windows', /Windows NT ([0-9\.]+)/, null],
['bb', /BlackBerry.+Version\/([0-9\.]+)/, null],
['bb', /BB[0-9]+.+Version\/([0-9\.]+)/, null]
];
_.iterate(a, function(k, v) {