-
Notifications
You must be signed in to change notification settings - Fork 88
/
pinit_main.js
4658 lines (4492 loc) · 171 KB
/
pinit_main.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
/* jshint indent: false, maxlen: false */
// return buttons to Pinterest Red
(function (w, d, n, a) {
var $ = (w[a.k] = {
w: w,
d: d,
n: n,
a: a,
s: {},
f: (function () {
return {
// an empty array for callbacks to be added later
callback: [],
// console.log only if debug is on
debug: function (obj) {
if ($.v.config.debug) {
if ($.w.console && $.w.console.log) {
$.w.console.log(obj);
}
}
},
// add and remove event listeners in a cross-browser fashion
listen: function (el, ev, fn, detach) {
if (!detach) {
// add listener
if (typeof $.w.addEventListener !== "undefined") {
el.addEventListener(ev, fn, false);
} else if (typeof $.w.attachEvent !== "undefined") {
el.attachEvent("on" + ev, fn);
}
} else {
// remove listener
if (typeof el.removeEventListener !== "undefined") {
el.removeEventListener(ev, fn, false);
} else if (typeof el.detachEvent !== "undefined") {
el.detachEvent("on" + ev, fn);
}
}
},
// find an event's target element
// via PPK (http://www.quirksmode.org/js/events_properties.html)
getEl: function (e) {
var el = null;
if (e.target) {
el = e.target.nodeType === 3 ? e.target.parentNode : e.target;
} else {
el = e.srcElement;
}
return el;
},
// add or remove a class
changeClass: function (el, delta) {
// delta of {foo: true, bar: false } will add foo and remove bar
var remove = function (str) {
var target = new RegExp(str, "ig");
el.className = el.className
.replace(target, "")
.replace(/ +/g, " ");
};
for (var k in delta) {
var selector = $.a.k + "_" + k;
// always remove any possible existing incidences
remove(selector);
// are we adding?
if (delta[k] === true) {
el.className = el.className + " " + selector;
}
}
},
// get a DOM property or text attribute
get: function (el, att) {
var v = "";
if (typeof el[att] === "string") {
v = el[att];
} else {
v = el.getAttribute(att);
}
return v;
},
loadFont: function (font) {
// this assumes $.v.ourStyles exists; the timeout is suspenders-and-belt
$.w.setTimeout(function () {
$.v.ourStyles.sheet.insertRule(
'@font-face { font-family: "' +
font.name +
'"; src: url("' +
font.url +
'"); font-weight: normal; font-style: normal; }'
);
}, 1);
},
// get a data: attribute
getData: function (el, att) {
att = $.a.dataAttributePrefix + att;
return $.f.get(el, att);
},
// set a DOM property or text attribute
set: function (el, att, string) {
if (typeof el[att] === "string") {
el[att] = string;
} else {
el.setAttribute(att, string);
}
},
// create a DOM element
make: function (obj) {
var el = false,
tag,
att;
for (tag in obj) {
if (obj[tag] && obj[tag].hasOwnProperty) {
el = $.d.createElement(tag);
for (att in obj[tag]) {
if (obj[tag][att] && obj[tag][att].hasOwnProperty) {
if (typeof obj[tag][att] === "string") {
$.f.set(el, att, obj[tag][att]);
}
}
}
break;
}
}
return el;
},
// remove a DOM element
kill: function (obj) {
if (typeof obj === "string") {
obj = $.d.getElementById(obj);
}
if (obj && obj.parentNode) {
obj.parentNode.removeChild(obj);
}
},
// replace one DOM element with another
replace: function (before, after) {
if (typeof before === "object" && typeof after === "object") {
$.w.setTimeout(function () {
before.parentNode.insertBefore(after, before);
$.w.setTimeout(function () {
$.f.kill(before);
}, 1);
}, 1);
}
},
// parse an URL, return values for specified keys in the query string
parse: function (str, keys) {
var query, pair, part, i, n, v, ret;
ret = {};
// remove url hash, split to find query
query = str.split("#")[0].split("?");
// found query?
if (query[1]) {
// split to pairs
pair = query[1].split("&");
// loop through pairs
for (i = 0, n = pair.length; i < n; i = i + 1) {
// split on equals
part = pair[i].split("=");
// found exactly two parts?
if (part.length === 2) {
// first part is key; do we have a match in keys?
if (keys[part[0]]) {
// attempt to decode this
try {
v = decodeURIComponent(part[1]);
} catch (e) {
v = part[1];
}
// yes: set return value for key to second part, which is value
ret[part[0]] = v;
}
}
}
}
return ret;
},
// stop the default event action
preventDefault: function (v) {
if (v.preventDefault) {
v.preventDefault();
} else {
v.returnValue = false;
}
},
// return moz, webkit, ms, etc
getVendorPrefix: function () {
var x = /^(moz|webkit|ms)(?=[A-Z])/i;
var r = "";
for (var p in $.d.b.style) {
if (x.test(p)) {
r = "-" + p.match(x)[0].toLowerCase() + "-";
break;
}
}
return r;
},
// call an API endpoint; fire callback if specified
call: function (url, callback) {
var n,
id,
tag,
msg,
sep = "?";
// $.f.callback starts as an empty array
n = $.f.callback.length;
// new SCRIPT tags get IDs so we can find them, query them, and delete them later
id = $.a.k + ".f.callback[" + n + "]";
// the callback will fire only when the API returns
$.f.callback[n] = function (r) {
// do we have output?
if (r) {
// send the original call back with the callback so we can munge href URLs if needed
r.theCall = url;
// do we need to log an error?
if (r.status && r.status === "failure") {
// some errors don't have messages; fall back to status
msg = r.message || r.status;
// has the site operator specified a callback?
if (typeof $.v.config.error === "string") {
// does the callback function actually exist?
if (typeof $.w[$.v.config.error] === "function") {
$.w[$.v.config.error](msg);
}
}
// scope gotcha: recreate id string from n instead of relying on it already being in id
tag = $.d.getElementById($.a.k + ".f.callback[" + n + "]");
// found it?
if (tag) {
// does it have a src attribute?
if (tag.src) {
// log only the URL part
$.f.log(
"&event=api_error&code=" +
r.code +
"&msg=" +
msg +
"&url=" +
encodeURIComponent(tag.src.split("?")[0])
);
}
}
}
}
// if a callback exists, pass the API output
if (typeof callback === "function") {
callback(r, n);
}
// clean up the SCRIPT tag after it's run
$.f.kill(id);
};
// some calls may come with a query string already set
if (url.match(/\?/)) {
sep = "&";
}
// make and call the new SCRIPT tag
$.d.b.appendChild(
$.f.make({
SCRIPT: {
id: id,
type: "text/javascript",
charset: "utf-8",
src: url + sep + "callback=" + id
}
})
);
},
// super-light base-64 encoder; guaranteed to choke on Unicode
// via Dave Chambers (https://github.com/davidchambers/Base64.js)
btoa: function (s) {
var d = "data:image/svg+xml;base64,";
if ($.w.btoa) {
d = d + $.w.btoa(s);
} else {
for (
var a =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
b,
c,
i = 0;
s.charAt(i | 0) || ((a = "="), i % 1);
d = d + a.charAt(63 & (b >> (8 - (i % 1) * 8)))
) {
c = s.charCodeAt((i += 0.75));
b = (b << 8) | c;
}
}
return d;
},
// turn a path and some values into an SVG
makeSVG: function (obj, fill) {
var i, n, svg;
// start svg
svg =
'<svg xmlns="http://www.w3.org/2000/svg" height="%h%px" width="%w%px" viewBox="%x1% %y1% %x2% %y2%"><g>';
// height and width
svg = svg.replace(/%h%/, obj.h);
svg = svg.replace(/%w%/, obj.w);
// view box defaults to 0, 0, w, h but can be overriden (side count bubble)
svg = svg.replace(/%x1%/, obj.x1 || "0");
svg = svg.replace(/%y1%/, obj.y1 || "0");
svg = svg.replace(/%x2%/, obj.x2 || obj.w);
svg = svg.replace(/%y2%/, obj.y2 || obj.h);
// compute svg data for each path (round Pinterest logo has two)
for (i = 0, n = obj.p.length; i < n; i = i + 1) {
// start the path
svg = svg + '<path d="' + obj.p[i].d + '"';
// use alternate fill color if specified (white Pin It logotype)
svg = svg + ' fill="#' + (fill || obj.p[i].f || "#000") + '"';
// stroke
if (obj.p[i].s) {
svg = svg + ' stroke="#' + obj.p[i].s + '"';
// stroke-width
if (!obj.p[i].w) {
obj.p[i].w = "2";
}
svg = svg + ' stroke-width="' + obj.p[i].w + '"';
}
// done
svg = svg + "></path>";
}
// end svg
svg = svg + "</g></svg>";
return $.f.btoa(svg);
},
// build stylesheet
buildStyleSheet: function () {
var css, rules, k, re, repl;
css = $.f.make({ STYLE: { type: "text/css" } });
rules = $.v.css;
// each rule has our randomly-created key at its root to minimize style collisions
rules = rules.replace(/\._/g, "." + a.k + "_");
// strings to replace in CSS rules
var repl = {
"%prefix%": $.f.getVendorPrefix(),
// css directives
"%thinShadow%": "0 0 1px rgba(0,0,0,.5)",
"%widgetBorderRadius%": "16px",
"%buttonBorderRadius%": "3px",
"%buttonBorderRadiusTall%": "3px",
"%saveButtonBackgroundColor%": "#e60023",
// SVG replacements
"%play%": $.f.makeSVG($.a.svg.play),
"%pause%": $.f.makeSVG($.a.svg.pause),
"%forward%": $.f.makeSVG($.a.svg.forward),
"%backward%": $.f.makeSVG($.a.svg.backward),
"%above%": $.f.makeSVG($.a.svg.above),
"%beside%": $.f.makeSVG($.a.svg.beside),
"%logo%": $.f.makeSVG($.a.svg.logo),
"%lockup%": $.f.makeSVG($.a.svg.lockup),
"%pinit_en_red%": $.f.makeSVG($.a.svg.pinit_en),
"%pinit_en_white%": $.f.makeSVG($.a.svg.pinit_en, "fff"),
"%pinit_ja_red%": $.f.makeSVG($.a.svg.pinit_ja),
"%pinit_ja_white%": $.f.makeSVG($.a.svg.pinit_ja, "fff")
};
$.f.makeSVG($.a.svg.pinit_en, "fff");
// replace everything in repl throughout rules
for (k in repl) {
if (repl[k].hasOwnProperty) {
// re = new RegExp(k, 'g');
rules = rules.replace(new RegExp(k, "g"), repl[k]);
}
}
// add rules to stylesheet
if (css.styleSheet) {
css.styleSheet.cssText = rules;
} else {
css.appendChild($.d.createTextNode(rules));
}
// add stylesheet to page
if ($.d.h) {
$.d.h.appendChild(css);
} else {
$.d.b.appendChild(css);
}
$.v.ourStyles = css;
},
// recursive function to make rules out of a Sass-like object
presentation: function (obj, str) {
// make CSS rules
var name,
i,
k,
pad,
key,
rules = "",
selector = str || "";
for (k in obj) {
if (obj[k].hasOwnProperty) {
// found a rule
if (typeof obj[k] === "string") {
rules = rules + "\n " + k + ": " + obj[k] + ";";
}
}
}
// add selector and rules to stylesheet
if (selector && rules) {
$.v.css = $.v.css + selector + " { " + rules + "\n}\n";
}
// any children we need to handle?
for (k in obj) {
if (obj[k].hasOwnProperty) {
if (typeof obj[k] === "object") {
// replace & with parent selector
// var key = k.replace(/&/g, selector);
key = selector + " " + k;
key = key.replace(/ &/g, "");
key = key.replace(/,/g, ", " + selector);
$.f.presentation(obj[k], key);
}
}
}
// if this is our root, remove from current context and make stylesheet
if (obj === $.a.styles) {
$.w.setTimeout(function () {
$.f.buildStyleSheet();
}, 1);
}
},
// send logging information
log: function (str) {
// don't log from our networks
if (
!$.v.here.url.match(/^https?:\/\/(.*?\.|)(pinterest|pinadmin)\.com\//)
) {
// query always starts with type=pidget&guid=something
var query = "?type=pidget&guid=" + $.v.guid,
ping = new Image();
// add test version if found
if ($.a.tv) {
query = query + "&tv=" + $.a.tv;
}
// add optional string &foo=bar
if (str) {
query = query + str;
}
// add user-specified logging tag, if present
if ($.v.config.tag) {
query = query + "&tag=" + $.v.config.tag;
}
// add the page we're looking at right now
query = query + "&via=" + encodeURIComponent($.v.here.url);
// did we derive via from an alternate to document.URL
if ($.v.here.src !== 'doc') {
// pin, canonical, or og
query = query + "&viaSrc=" + $.v.here.src;
}
// did we modify this URL due to a forbidden parameter?
if ($.v.here.mod) {
query = query + "&viaMod=1";
}
// debug what we're about to send
$.f.debug("Logging: " + query);
ping.src = $.a.endpoint.log + query;
}
},
// build a query
buildQuery: function (params) {
var query = "";
for (var key in params) {
if (params.hasOwnProperty(key) && params[key]) {
if (query) {
query = query + "&";
}
query = query + key + "=" + encodeURIComponent(params[key]);
}
}
return query;
},
// things that happen on click, exposed for site operators to call if needed
util: {
// story pin video controls
control: function (me) {
// default play
var directive = "play";
// if we're pausing, set it to pause
if (me.el.className.match("_pause")) {
directive = "pause";
}
// get our pin
var top = me.el.parentNode.parentNode.parentNode;
// get the first video in the pin and pause or play it
top.getElementsByTagName("video")[0][directive]();
// find our play and pause controls
var controls = {
play: me.el.parentNode.getElementsByClassName($.a.k + "_play")[0],
pause: me.el.parentNode.getElementsByClassName($.a.k + "_pause")[0]
}
// show them both
controls.play.style.display = controls.pause.style.display = "block";
// hide the one we just clicked
controls[directive].style.display = "none";
},
// story pin no-op (so we don't open the pin when someone clicks where nav has just disappeared)
noop: function () {},
// story pin navigation
navigate: function (me) {
var dir = $.f.getData(me.el, "log").split("_").pop();
// get our pin
var top = me.el.parentNode.parentNode.parentNode;
// get the current index
var current = $.f.get(top, "data-pin-current") - 0;
// get the pages
var pages = top.getElementsByClassName($.a.k + "_page");
// stop playing video
if (pages[current].className.match("hazVideo")) {
var video = pages[current].getElementsByTagName("video");
if (video[0]) {
video[0].pause();
}
// if we are moving off page zero, hide pause/play controls
if (!current) {
var controls = me.el.parentNode.getElementsByClassName($.a.k + "_controls")[0];
controls.style.display = "none";
// reset controls so Pause isn't showing when we go back
controls.getElementsByClassName($.a.k + "_play")[0].style.display = "block";
controls.getElementsByClassName($.a.k + "_pause")[0].style.display = "none";
}
}
// move the current page into the past or future
if (dir === "forward") {
// move the present page into the past
$.f.changeClass(pages[current], { past: true });
// increment current
current = current + 1;
} else {
// move the present page into the future
$.f.changeClass(pages[current], { future: true });
// increment current
current = current - 1;
// are we on the first page?
if (!current) {
// do we have a video?
if (pages[current].className.match("hazVideo")) {
$.w.setTimeout(function () {
me.el.parentNode.getElementsByClassName($.a.k + "_controls")[0].style.display = "block";
// wait .25s so the animation can finish before throwing up the controller
}, 250);
}
}
}
// set the new index
$.f.set(top, "data-pin-current", current);
// update progress dots
var dots = top.getElementsByClassName($.a.k + "_indicator");
for (i = 0; i < dots.length; i = i + 1) {
$.f.changeClass(dots[i], { current: i === current });
}
// move the new page into the present
if (dir === "forward") {
// we've gone forward so we are no longer at the beginning
$.f.changeClass(top, { atStart: false });
// move the future page into the present
$.f.changeClass(pages[current], { future: false });
// hide the forward arrow
if (current === pages.length - 1) {
// we're at the end
$.f.changeClass(top, { atEnd: true });
}
} else {
// we've gone backward so we are no longer at the end
$.f.changeClass(top, { atEnd: false });
// move the past page into the present
$.f.changeClass(pages[current], { past: false });
// hide the forward arrow
if (current === 0) {
// we're at the beginning
$.f.changeClass(top, { atStart: true });
}
}
// start playing if we are not on page 0, which has a player control
if (pages[current].className.match("hazVideo") && current) {
var video = pages[current].getElementsByTagName("video");
if (video[0]) {
video[0].play();
}
}
},
// open an URL
open: function (me) {
$.w.open(me.href, "_blank");
},
// open pinmarklet
pinAny: function () {
$.f.debug("opening the grid");
$.d.b.appendChild(
$.f.make({
SCRIPT: {
type: "text/javascript",
charset: "utf-8",
pinMethod: "button",
guid: $.v.guid,
src: $.a.endpoint.bookmark + "?guid=" + $.v.guid
}
})
);
},
// pin an image
pinOne: function (o) {
if (o.href) {
// parsing an URL, pinning
var q = $.f.parse(o.href, {
url: true,
media: true,
description: true
});
// found valid URLs?
if (
q.url &&
q.url.match(/^http/i) &&
q.media &&
q.media.match(/^http/i)
) {
// log an error for Pin It buttons that don't have default descriptions
if (!q.description) {
q.description = $.d.title;
}
// don't pass more than 500 characters to the board picker
if (q.description.length > 500) {
q.description = q.description.substring(0, 500);
}
// pop the pin form
$.w.open(
o.href,
"pin" + new Date().getTime(),
$.a.pop.base.replace("%dim%", $.a.pop.size)
);
} else {
// fire up the bookmarklet and hope for the best
$.f.util.pinAny();
}
} else {
// we're pinning an image
if (o.media) {
if (!o.url) {
o.url = $.v.here.url;
}
if (!o.description) {
o.description = $.d.title;
}
// don't pass more than 500 characters to the board picker
if (o.description.length > 500) {
o.description = o.description.substring(0, 500);
}
// pop the pin form
$.f.log("&event=button_pinit_custom");
o.href =
$.v.config.pinterest +
"/pin/create/button/?guid=" +
$.v.guid +
"&url=" +
encodeURIComponent(o.url) +
"&media=" +
encodeURIComponent(o.media) +
"&description=" +
encodeURIComponent(o.description);
$.w.open(
o.href,
"pin" + new Date().getTime(),
$.a.pop.base.replace("%dim%", $.a.pop.size)
);
} else {
// no media
$.f.util.pinAny();
}
}
if (o.v && o.v.preventDefault) {
o.v.preventDefault();
} else {
$.w.event.returnValue = false;
}
},
// open repin dialog from hoverbutton
repinHoverButton: function (id) {
$.f.util.repin(id, true);
},
// open repin dialog
repin: function (data, fromHover) {
var href, logType, pinId;
if (typeof data === "object") {
if (data.href) {
pinId = data.href.split("/")[4];
}
} else {
pinId = data;
}
if (parseInt(pinId)) {
var href =
$.v.config.pinterest +
$.a.path.repin.replace("%s", pinId) +
"?guid=" +
$.v.guid;
$.w.open(
href,
"pin" + new Date().getTime(),
$.a.pop.base.replace("%dim%", $.a.pop.size)
);
} else {
$.f.debug($.v.config.util + ".repin requires an integer pinId");
}
},
// open follow dialog
follow: function (o) {
$.w.open(
o.href,
"pin" + new Date().getTime(),
$.a.pop.base.replace("%dim%", $.a.pop.size)
);
},
// send a log request
log: function (params) {
if (params) {
$.f.log("&" + $.f.buildQuery(params));
} else {
$.f.debug($.v.config.util + ".log requires valid query params");
}
}
},
// build a complex element from a JSON template
buildOne: function (obj, el) {
if (!el) {
var opts = {};
// Add links to buttons for SEO.
if (obj.tagName === "A" && obj.href) {
opts.A = {
className:
$.a.k + "_" + obj.className.replace(/ /g, " " + $.a.k + "_"),
href: obj.href
};
} else {
opts.SPAN = {
className:
$.a.k + "_" + obj.className.replace(/ /g, " " + $.a.k + "_")
};
}
var root = $.f.make(opts);
$.f.buildOne(obj, root);
// return a pointer to the object that will eventually contain the entire built structure
return root;
} else {
if (obj && obj.length) {
// do we have an array of children to build?
for (var i = 0; i < obj.length; i = i + 1) {
$.f.buildOne(obj[i], el);
}
} else {
// check keys; set attributes if they're strings
for (var key in obj) {
if (typeof obj[key] === "string") {
// set an attribute
var value = obj[key];
// set text
if (key === "text") {
el.innerHTML = el.innerHTML + value;
}
// add calss names
if (key === "addClass") {
var classesToAdd = value.split(" ");
for (var i = 0; i < classesToAdd.length; i = i + 1) {
el.className =
el.className + " " + $.a.k + "_" + classesToAdd[i];
}
}
// only some style attributes are allowed
if ($.a.build.setStyle[key]) {
if (key === "backgroundImage") {
el.style[key] = "url(" + value + ")";
} else {
el.style[key] = value;
}
}
// only some data attributes are allowed
if ($.a.build.setData[key]) {
$.f.set(el, "data-pin-" + key, value);
}
} else {
// we have an object
if (key !== "video") {
// create a new container for the child element
var child = $.f.make({
SPAN: {
className: $.a.k + "_" + key.replace(/ /g, " " + $.a.k),
// where we go on click
"data-pin-href": $.f.getData(el, "href"),
// what we log (and potentially what we do) on click
"data-pin-log": $.f.getData(el, "log")
}
});
// append the child to our current element
el.appendChild(child);
// check for grandchildren
$.f.buildOne(obj[key], child);
} else {
// we have a video (currently only available in story pins)
var addVideoClass = "";
if (obj.video.addClass) {
addVideoClass = " " + $.a.k + "_" + obj.video.addClass;
}
var myVideo = $.f.make({
VIDEO: {
poster: obj.video.poster,
preload: "auto",
loop: "loop",
playsinline: "playsinline",
class: $.a.k + "_video" + addVideoClass
}
});
// append the mp4 first or Firefox will complain in console about m3u8 not being supported
if (obj.video.mp4) {
myVideo.appendChild(
$.f.make({
SOURCE: {
src: obj.video.mp4,
type: "video/mp4"
}
})
);
}
// do the right thing for Webkit browsers
if (obj.video.m3u8) {
myVideo.appendChild(
$.f.make({
SOURCE: {
src: obj.video.m3u8,
type: "video/m3u8"
}
})
);
}
// append the video
el.appendChild(myVideo);
}
}
}
}
}
},
// a click!
click: function (v) {
v = v || $.w.event;
var el, log, x, pinId, href;
el = $.f.getEl(v);
if (el) {
log = $.f.getData(el, "log");
// custom buttons with child nodes may not pass clicks; check one level up
if (!log && el.parentNode) {
el = el.parentNode;
log = $.f.getData(el, "log");
}
// is it one of ours?
if (log) {
x = $.f.getData(el, "x") || "";
href = $.f.getData(el, "href");
if (x) {
// sticky and hoverbuttons will pass in &tall=1&round=1
// embedded pin widget will show a naked value so report &x=value
if (x.substr(0, 1) !== "&") {
x = "&x=" + encodeURIComponent(x);
}
}
$.f.log(
"&event=click&target=" +
log +
"&lang=" +
$.v.lang +
"&sub=" +
$.v.sub +
x +
"&href=" +
encodeURIComponent(href)
);
if (typeof $.f.util[$.a.util[log]] === "function") {
// got a special utility handler? run it
$.f.util[$.a.util[log]]({ el: el, href: href, v: v });
} else {
if (href) {
// some elements are controls, like pause/play and menu toggle; they won't open new pages
$.w.open(href, "_blank");
}
}
}
}
},
// BEGIN HOVERBUTTON-RELATED STUFF
// return the selected text, if any
getSelection: function () {
return (
"" +
($.w.getSelection
? $.w.getSelection()
: $.d.getSelection
? $.d.getSelection()
: $.d.selection.createRange().text)
).replace(/(^\s+|\s+$)/g, "");
},
// return current style property for element
// via PPK (http://www.quirksmode.org/dom/getstyles.html)
getStyle: function (el, prop, getNum) {
var r = null;
// modern browsers
if ($.w.getComputedStyle) {
r = $.w.getComputedStyle(el).getPropertyValue(prop);
} else {
// IE browsers
if (el.currentStyle) {
r = el.currentStyle[prop];
}
}
// if we only want the numeric part, shave off px
if (r && getNum) {
r = parseInt(r) || 0;
}
return r;
},
// get position of an element
getPos: function (el) {
var rect = el.getBoundingClientRect();
return {
top: rect.top + $.w.scrollY,
left: rect.left + $.w.scrollX,
bottom: rect.bottom + $.w.scrollY,
right: rect.right + $.w.scrollX
};
},
// show hoverbuttons and stickybuttons
showHoverButton: function (el, sticky) {
// always try to kill it
$.f.kill($.s.hoverButton);
// get config options
var c = {
id: $.f.getData(el, "id"),
url: $.f.getData(el, "url"),
media: $.f.getData(el, "media"),
description: $.f.getData(el, "description"),
height: $.f.getData(el, "height") || $.v.config.height || "20",
color: $.f.getData(el, "color") || $.v.config.color || "gray",
shape: $.f.getData(el, "shape") || $.v.config.shape || "rect",
lang: $.f.getLang($.f.getData(el, "lang") || $.v.config.lang || $.v.lang),
// new params
tall: $.f.getData(el, "tall") || $.v.config.tall,
round: $.f.getData(el, "round") || $.v.config.round
};
// legacy translations
if (c.height === "28") {
c.tall = true;
}
if (c.shape === "round") {
c.round = true;
}
var h, w;