-
Notifications
You must be signed in to change notification settings - Fork 1
/
facebook-js-sdk.js
14638 lines (13449 loc) · 447 KB
/
facebook-js-sdk.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
/*1329323125,171364642,JIT Construction: v510186,en_US*/
/**
* Copyright Facebook Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*
* @provides fb.prelude
*/
/**
* Prelude.
*
* Namespaces are one honking great idea -- let's do more of those!
* -- Tim Peters
*
* The Prelude is what keeps us from being messy. In order to co-exist with
* arbitary environments, we need to control our footprint. The one and only
* rule to follow here is that we need to limit the globals we introduce. The
* only global we should every have is ``FB``. This is exactly what the prelude
* enables us to do.
*
* The main method to take away from this file is `FB.copy()`_. As the name
* suggests it copies things. Its powerful -- but to get started you only need
* to know that this is what you use when you are augmenting the FB object. For
* example, this is skeleton for how ``FB.Event`` is defined::
*
* FB.provide('Event', {
* subscribe: function() { ... },
* unsubscribe: function() { ... },
* fire: function() { ... }
* });
*
* This is similar to saying::
*
* FB.Event = {
* subscribe: function() { ... },
* unsubscribe: function() { ... },
* fire: function() { ... }
* };
*
* Except it does some housekeeping, prevents redefinition by default and other
* goodness.
*
* .. _FB.copy(): #method_FB.copy
*
* @class FB
* @static
* @access private
*/
if (!window.FB) {
window.FB = {
// use the init method to set these values correctly
_apiKey : null,
_authResponse : null,
_userStatus : 'unknown', // or 'notConnected' or 'connected'
// logging is enabled by default. this is the logging shown to the
// developer and not at all noisy.
_logging: true,
_inCanvas: (
(window.name.indexOf('iframe_canvas') > -1) ||
(window.name.indexOf('app_runner') > -1)),
// Determines if we should use HTTPS when attempting cross-domain
// communication with facebook.com. This is assumed to be the case when
// window.name contains "_fb_https". This value may also be set by the
// response from FB.login() or FB.getLoginStatus()
_https: (window.name.indexOf('_fb_https') > -1),
//
// DYNAMIC DATA
//
// the various domains needed for using Connect
_domain: {
api : 'https://api.facebook.com/',
api_read : 'https://api-read.facebook.com/',
cdn : 'http://static.ak.fbcdn.net/',
https_cdn : 'https://s-static.ak.fbcdn.net/',
graph : 'https://graph.facebook.com/',
staticfb : 'http://static.ak.facebook.com/',
https_staticfb : 'https://s-static.ak.facebook.com/',
www : 'http://www.facebook.com/',
https_www : 'https://www.facebook.com/',
m : 'http://m.facebook.com/',
https_m : 'https://m.facebook.com/'
},
_locale: null,
_localeIsRtl: false,
// CORDOVA PATCH
_nativeInterface : null,
/**
* Retrieve one of the various domains needed for Connect.
*
* @access private
* @param domain (String) The domain to retrieve
* @param noForcedHTTPS (bool) Do not force https domain
*/
getDomain: function(domain, noForcedHTTPS) {
var forceHTTPS = !noForcedHTTPS &&
(window.location.protocol == 'https:' || FB._https);
switch (domain) {
case 'api':
return FB._domain.api;
case 'api_read':
return FB._domain.api_read;
case 'cdn':
return forceHTTPS ? FB._domain.https_cdn : FB._domain.cdn;
case 'cdn_foreign':
return FB._domain.cdn_foreign;
case 'https_cdn':
return FB._domain.https_cdn;
case 'graph':
return FB._domain.graph;
case 'staticfb':
return forceHTTPS ? FB._domain.https_staticfb : FB._domain.staticfb;
case 'https_staticfb':
return FB._domain.https_staticfb;
case 'www':
return forceHTTPS ? FB._domain.https_www : FB._domain.www;
case 'https_www':
return FB._domain.https_www;
case 'm':
return forceHTTPS ? FB._domain.https_m : FB._domain.m;
case 'https_m':
return FB._domain.https_m;
}
},
/**
* Copies things from source into target.
*
* @access private
* @param target {Object} the target object where things will be copied
* into
* @param source {Object} the source object where things will be copied
* from
* @param overwrite {Boolean} indicate if existing items should be
* overwritten
* @param transform {function} [Optional], transformation function for
* each item
*/
copy: function(target, source, overwrite, transform) {
for (var key in source) {
if (overwrite || typeof target[key] === 'undefined') {
target[key] = transform ? transform(source[key]) : source[key];
}
}
return target;
},
/**
* Create a namespaced object.
*
* @access private
* @param name {String} full qualified name ('Util.foo', etc.)
* @param value {Object} value to set. Default value is {}. [Optional]
* @return {Object} The created object
*/
create: function(name, value) {
var node = window.FB, // We will use 'FB' as root namespace
nameParts = name ? name.split('.') : [],
c = nameParts.length;
for (var i = 0; i < c; i++) {
var part = nameParts[i];
var nso = node[part];
if (!nso) {
nso = (value && i + 1 == c) ? value : {};
node[part] = nso;
}
node = nso;
}
return node;
},
/**
* Copy stuff from one object to the specified namespace that
* is FB.<target>.
* If the namespace target doesn't exist, it will be created automatically.
*
* @access private
* @param target {Object|String} the target object to copy into
* @param source {Object} the source object to copy from
* @param overwrite {Boolean} indicate if we should overwrite
* @return {Object} the *same* target object back
*/
provide: function(target, source, overwrite) {
// a string means a dot separated object that gets appended to, or created
return FB.copy(
typeof target == 'string' ? FB.create(target) : target,
source,
overwrite
);
},
/**
* Generates a weak random ID.
*
* @access private
* @return {String} a random ID
*/
guid: function() {
return 'f' + (Math.random() * (1<<30)).toString(16).replace('.', '');
},
/**
* Logs a message for the developer if logging is on.
*
* @access private
* @param args {Object} the thing to log
*/
log: function(args) {
if (FB._logging) {
//TODO what is window.Debug, and should it instead be relying on the
// event fired below?
//#JSCOVERAGE_IF 0
if (window.Debug && window.Debug.writeln) {
window.Debug.writeln(args);
} else if (window.console) {
window.console.log(args);
}
//#JSCOVERAGE_ENDIF
}
// fire an event if the event system is available
if (FB.Event) {
FB.Event.fire('fb.log', args);
}
},
/**
* Shortcut for document.getElementById
* @method $
* @param {string} DOM id
* @return DOMElement
* @access private
*/
$: function(id) {
return document.getElementById(id);
}
};
}
/**
* Copyright Facebook Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @provides fb.array
* @layer basic
* @requires fb.prelude
*/
/**
* Array related helper methods.
*
* @class FB.Array
* @private
* @static
*/
FB.provide('Array', {
/**
* Get index of item inside an array. Return's -1 if element is not found.
*
* @param arr {Array} Array to look through.
* @param item {Object} Item to locate.
* @return {Number} Index of item.
*/
indexOf: function (arr, item) {
if (arr.indexOf) {
return arr.indexOf(item);
}
var length = arr.length;
if (length) {
for (var index = 0; index < length; index++) {
if (arr[index] === item) {
return index;
}
}
}
return -1;
},
/**
* Merge items from source into target, but only if they dont exist. Returns
* the target array back.
*
* @param target {Array} Target array.
* @param source {Array} Source array.
* @return {Array} Merged array.
*/
merge: function(target, source) {
for (var i=0; i < source.length; i++) {
if (FB.Array.indexOf(target, source[i]) < 0) {
target.push(source[i]);
}
}
return target;
},
/**
* Create an new array from the given array and a filter function.
*
* @param arr {Array} Source array.
* @param fn {Function} Filter callback function.
* @return {Array} Filtered array.
*/
filter: function(arr, fn) {
var b = [];
for (var i=0; i < arr.length; i++) {
if (fn(arr[i])) {
b.push(arr[i]);
}
}
return b;
},
/**
* Create an array from the keys in an object.
*
* Example: keys({'x': 2, 'y': 3'}) returns ['x', 'y']
*
* @param obj {Object} Source object.
* @param proto {Boolean} Specify true to include inherited properties.
* @return {Array} The array of keys.
*/
keys: function(obj, proto) {
var arr = [];
for (var key in obj) {
if (proto || obj.hasOwnProperty(key)) {
arr.push(key);
}
}
return arr;
},
/**
* Create an array by performing transformation on the items in a source
* array.
*
* @param arr {Array} Source array.
* @param transform {Function} Transformation function.
* @return {Array} The transformed array.
*/
map: function(arr, transform) {
var ret = [];
for (var i=0; i < arr.length; i++) {
ret.push(transform(arr[i]));
}
return ret;
},
/**
* For looping through Arrays and Objects.
*
* @param {Object} item an Array or an Object
* @param {Function} fn the callback function for iteration.
* The function will be pass (value, [index/key], item) parameters
* @param {Bool} proto indicate if properties from the prototype should
* be included
*
*/
forEach: function(item, fn, proto) {
if (!item) {
return;
}
if (Object.prototype.toString.apply(item) === '[object Array]' ||
(!(item instanceof Function) && typeof item.length == 'number')) {
if (item.forEach) {
item.forEach(fn);
} else {
for (var i=0, l=item.length; i<l; i++) {
fn(item[i], i, item);
}
}
} else {
for (var key in item) {
if (proto || item.hasOwnProperty(key)) {
fn(item[key], key, item);
}
}
}
},
/**
* Turns HTMLCollections or anything array-like (that has a `length`)
* such as function `arguments` into a real array
*
* @param {HTMLCollection} coll Array-like collection
* @return {Array}
*/
toArray: function(coll) {
for (var i = 0, a = [], len = coll.length; i < len; i++) {
a[i] = coll[i];
}
return a;
}
});
/**
* Copyright Facebook Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*
* @provides fb.qs
* @requires fb.prelude fb.array
*/
/**
* Query String encoding & decoding.
*
* @class FB.QS
* @static
* @access private
*/
FB.provide('QS', {
/**
* Encode parameters to a query string.
*
* @access private
* @param params {Object} the parameters to encode
* @param sep {String} the separator string (defaults to '&')
* @param encode {Boolean} indicate if the key/value should be URI encoded
* @return {String} the query string
*/
encode: function(params, sep, encode) {
sep = sep === undefined ? '&' : sep;
encode = encode === false ? function(s) { return s; } : encodeURIComponent;
var pairs = [];
FB.Array.forEach(params, function(val, key) {
if (val !== null && typeof val != 'undefined') {
pairs.push(encode(key) + '=' + encode(val));
}
});
pairs.sort();
return pairs.join(sep);
},
/**
* Decode a query string into a parameters object.
*
* @access private
* @param str {String} the query string
* @return {Object} the parameters to encode
*/
decode: function(str) {
var
decode = decodeURIComponent,
params = {},
parts = str.split('&'),
i,
pair;
for (i=0; i<parts.length; i++) {
pair = parts[i].split('=', 2);
if (pair && pair[0]) {
params[decode(pair[0])] = decode(pair[1] || '');
}
}
return params;
}
});
/**
* Copyright Facebook Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*
* @provides fb.content
* @requires fb.prelude fb.array
*/
/**
* "Content" is a very flexible term. Helpers for things like hidden
* DOM content, iframes and popups.
*
* @class FB.Content
* @static
* @access private
*/
FB.provide('Content', {
_root : null,
_hiddenRoot : null,
_callbacks : {},
/**
* Append some content.
*
* @access private
* @param content {String|Node} a DOM Node or HTML string
* @param root {Node} (optional) a custom root node
* @return {Node} the node that was just appended
*/
append: function(content, root) {
// setup the root node
if (!root) {
if (!FB.Content._root) {
FB.Content._root = root = FB.$('fb-root');
if (!root) {
FB.log('The "fb-root" div has not been created.');
return;
} else {
root.className += ' fb_reset';
}
} else {
root = FB.Content._root;
}
}
if (typeof content == 'string') {
var div = document.createElement('div');
root.appendChild(div).innerHTML = content;
return div;
} else {
return root.appendChild(content);
}
},
/**
* Append some hidden content.
*
* @access private
* @param content {String|Node} a DOM Node or HTML string
* @return {Node} the node that was just appended
*/
appendHidden: function(content) {
if (!FB.Content._hiddenRoot) {
var
hiddenRoot = document.createElement('div'),
style = hiddenRoot.style;
style.position = 'absolute';
style.top = '-10000px';
style.width = style.height = 0;
FB.Content._hiddenRoot = FB.Content.append(hiddenRoot);
}
return FB.Content.append(content, FB.Content._hiddenRoot);
},
/**
* Insert a new iframe. Unfortunately, its tricker than you imagine.
*
* NOTE: These iframes have no border, overflow hidden and no scrollbars.
*
* The opts can contain:
* root DOMElement required root node (must be empty)
* url String required iframe src attribute
* className String optional class attribute
* height Integer optional height in px
* id String optional id attribute
* name String optional name attribute
* onInsert Function optional callback directly after insertion
* onload Function optional onload handler
* width Integer optional width in px
*
* @access private
* @param opts {Object} the options described above
*/
insertIframe: function(opts) {
//
// Browsers evolved. Evolution is messy.
//
opts.id = opts.id || FB.guid();
opts.name = opts.name || FB.guid();
// Dear IE, screw you. Only works with the magical incantations.
// Dear FF, screw you too. Needs src _after_ DOM insertion.
// Dear Webkit, you're okay. Works either way.
var
guid = FB.guid(),
// Since we set the src _after_ inserting the iframe node into the DOM,
// some browsers will fire two onload events, once for the first empty
// iframe insertion and then again when we set the src. Here some
// browsers are Webkit browsers which seem to be trying to do the
// "right thing". So we toggle this boolean right before we expect the
// correct onload handler to get fired.
srcSet = false,
onloadDone = false;
FB.Content._callbacks[guid] = function() {
if (srcSet && !onloadDone) {
onloadDone = true;
opts.onload && opts.onload(opts.root.firstChild);
}
};
//#JSCOVERAGE_IF
if (document.attachEvent) {
// Initial src is set to javascript:false so as to not trigger the
// unsecure content warning.
var html = (
'<iframe' +
' id="' + opts.id + '"' +
' name="' + opts.name + '"' +
(opts.title ? ' title="' + opts.title + '"' : '') +
(opts.className ? ' class="' + opts.className + '"' : '') +
' style="border:none;' +
(opts.width ? 'width:' + opts.width + 'px;' : '') +
(opts.height ? 'height:' + opts.height + 'px;' : '') +
'"' +
' src="javascript:false;"' +
' frameborder="0"' +
' scrolling="no"' +
' allowtransparency="true"' +
' onload="FB.Content._callbacks.' + guid + '()"' +
'></iframe>'
);
// There is an IE bug with iframe caching that we have to work around. We
// need to load a dummy iframe to consume the initial cache stream. The
// setTimeout actually sets the content to the HTML we created above, and
// because its the second load, we no longer suffer from cache sickness.
// It must be javascript:false instead of about:blank, otherwise IE6 will
// complain in https.
// Since javascript:false actually result in an iframe containing the
// string 'false', we set the iframe height to 1px so that it gets loaded
// but stays invisible.
opts.root.innerHTML = '<iframe src="javascript:false"'+
' frameborder="0"'+
' scrolling="no"'+
' style="height:1px"></iframe>';
// Now we'll be setting the real src.
srcSet = true;
// You may wonder why this is a setTimeout. Read the IE source if you can
// somehow get your hands on it, and tell me if you figure it out. This
// is a continuation of the above trick which apparently does not work if
// the innerHTML is changed right away. We need to break apart the two
// with this setTimeout 0 which seems to fix the issue.
window.setTimeout(function() {
opts.root.innerHTML = html;
opts.root.firstChild.src = opts.url;
opts.onInsert && opts.onInsert(opts.root.firstChild);
}, 0);
} else {
// This block works for all non IE browsers. But it's specifically
// designed for FF where we need to set the src after inserting the
// iframe node into the DOM to prevent cache issues.
var node = document.createElement('iframe');
node.id = opts.id;
node.name = opts.name;
node.onload = FB.Content._callbacks[guid];
node.scrolling = 'no';
node.style.border = 'none';
node.style.overflow = 'hidden';
if (opts.title) {
node.title = opts.title;
}
if (opts.className) {
node.className = opts.className;
}
if (opts.height) {
node.style.height = opts.height + 'px';
}
if (opts.width) {
if (opts.width == '100%') {
node.style.width = opts.width;
} else {
node.style.width = opts.width + 'px';
}
}
opts.root.appendChild(node);
// Now we'll be setting the real src.
srcSet = true;
node.src = opts.url;
opts.onInsert && opts.onInsert(node);
}
},
/**
* Dynamically generate a <form> and submits it to the given target.
* Uses POST by default.
*
* The opts MUST contain:
* url String action URL for the form
* target String the target for the form
* params Object the key/values to be used as POST input
*
* @access protected
* @param opts {Object} the options
* @param get Should we use get instead?
*/
submitToTarget: function(opts, get) {
var form = document.createElement('form');
form.action = opts.url;
form.target = opts.target;
form.method = (get) ? 'GET' : 'POST';
FB.Content.appendHidden(form);
FB.Array.forEach(opts.params, function(val, key) {
if (val !== null && val !== undefined) {
var input = document.createElement('input');
input.name = key;
input.value = val;
form.appendChild(input);
}
});
form.submit();
form.parentNode.removeChild(form);
}
});
/**
* Copyright Facebook Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*
* @provides fb.flash
* @requires fb.prelude
* fb.qs
* fb.content
*/
/**
* Flash Support.
*
* @class FB.Flash
* @static
* @access private
*/
FB.provide('Flash', {
//
// DYNAMIC DATA
//
_minVersions: [
[9, 0, 159, 0 ],
[10, 0, 22, 87]
],
_swfPath: 'swf/XdComm.swf',
/**
* The onReady callbacks.
*
* @access private
* @type Array
*/
_callbacks: [],
/**
* Names of embedded swfs. Used for removing on unload.
*
* @access private
* @type Object
*/
_names: {},
/**
* Whether or not unload callback has been registered (used in IE9).
*
* @access private
* @type Boolean
*/
_unloadRegistered: false,
/**
* Initialize the SWF.
*
* @access private
*/
init: function() {
// only initialize once
if (FB.Flash._init) {
return;
}
FB.Flash._init = true;
// the SWF calls this global function to notify that its ready
// FIXME: should allow the SWF to take a flashvar that controls the name
// of this function. we should not have any globals other than FB.
window.FB_OnFlashXdCommReady = function() {
FB.Flash._ready = true;
for (var i=0, l=FB.Flash._callbacks.length; i<l; i++) {
FB.Flash._callbacks[i]();
}
FB.Flash._callbacks = [];
};
FB.Flash.embedSWF('XdComm',
FB.getDomain('cdn_foreign') + FB.Flash._swfPath);
},
/**
* generates the swf <object> tag and drops it in the DOM
*
* @access private
*/
embedSWF: function(name, swf, flashvars) {
// create the swf
var
IE = !!document.attachEvent,
html = (
'<object ' +
'type="application/x-shockwave-flash" ' +
'id="' + name + '" ' +
(flashvars ? 'flashvars="' + flashvars + '" ' : '') +
(IE ? 'name="' + name + '" ' : '') +
(IE ? '' : 'data="' + swf + '" ') +
(IE
? 'classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '
: ''
) +
'allowscriptaccess="always">' +
'<param name="movie" value="' + swf + '"></param>' +
'<param name="allowscriptaccess" value="always"></param>' +
'</object>'
);
FB.Content.appendHidden(html);
if (FB.UA.ie() >= 9) {
if (!FB.Flash._unloadRegistered) {
var unloadcb = function() {
FB.Array.forEach(FB.Flash._names, function(val, key) {
var elem = document.getElementById(key);
if (elem) {
elem.removeNode(true);
}
});
};
window.attachEvent('onunload', unloadcb);
FB.Flash._unloadRegistered = true;
}
FB.Flash._names[name] = true;
}
},
/**
* Check that the minimal version of Flash we need is available.
*
* @access private
* @return {Boolean} true if the minimum version requirements are matched
*/
hasMinVersion: function() {
if (typeof FB.Flash._hasMinVersion === 'undefined') {
var
versionString,
i,
l,
version = [];
try {
versionString = new ActiveXObject('ShockwaveFlash.ShockwaveFlash')
.GetVariable('$version');
} catch(x) {
if (navigator.mimeTypes.length > 0) {
var mimeType = 'application/x-shockwave-flash';
if (navigator.mimeTypes[mimeType].enabledPlugin) {
var name = 'Shockwave Flash';
versionString = (navigator.plugins[name + ' 2.0'] ||
navigator.plugins[name])
.description;
}
}
}
// take the string and come up with an array of integers:
// [10, 0, 22]
if (versionString) {
var parts = versionString
.replace(/\D+/g, ',')
.match(/^,?(.+),?$/)[1]
.split(',');
for (i=0, l=parts.length; i<l; i++) {
version.push(parseInt(parts[i], 10));
}
}
// start by assuming we dont have the min version.
FB.Flash._hasMinVersion = false;
// look through all the allowed version definitions.
majorVersion:
for (i=0, l=FB.Flash._minVersions.length; i<l; i++) {
var spec = FB.Flash._minVersions[i];
// we only accept known major versions, and every supported major
// version has at least one entry in _minVersions. only if the major
// version matches, does the rest of the check make sense.
if (spec[0] != version[0]) {
continue;
}
// the rest of the version components must be equal or higher
for (var m=1, n=spec.length, o=version.length; (m<n && m<o); m++) {
if (version[m] < spec[m]) {
// less means this major version is no good
//#JSCOVERAGE_IF 0
FB.Flash._hasMinVersion = false;
continue majorVersion;
//#JSCOVERAGE_ENDIF
} else {
FB.Flash._hasMinVersion = true;
if (version[m] > spec[m]) {
// better than needed
break majorVersion;
}
}
}
}
}
return FB.Flash._hasMinVersion;
},
/**
* Register a function that needs to ensure Flash is ready.
*
* @access private
* @param cb {Function} the function
*/
onReady: function(cb) {
FB.Flash.init();
if (FB.Flash._ready) {
// this forces the cb to be asynchronous to ensure no one relies on the
// _potential_ synchronous nature.
window.setTimeout(cb, 0);
} else {
FB.Flash._callbacks.push(cb);
}
}
});
/**
* This is the stock JSON2 implementation from www.json.org.
*
* Modifications include:
* 1/ Removal of jslint settings
*
* @provides fb.thirdparty.json2
*/
/*