-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdemo.js
10507 lines (8757 loc) · 299 KB
/
demo.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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
* bitcoinaddress.js
*
* Bitcoin address and payment helper.
*
* Copyright 2013 Mikko Ohtamaa http://opensourcehacker.com
*
* Licensed under MIT license.
*/
// Please note that script this depends on jQuery,
// but I did not find a solution for having UMD loading for the script,
// so that jQuery would be available through browserify bundling
// OR CDN. Include jQuery externally before including this script.
/* global module, require */
var qrcode = require("./qrcode.js");
// jQuery reference
var $;
module.exports = {
config : null,
/**
* Create URL for bitcoin URI scheme payments.
*
* https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki#Examples
*
* http://bitcoin.stackexchange.com/questions/4987/bitcoin-url-scheme
*
* @param {String} address Receiving address
* @param {String} amount Amount as big decimal
* @param {String} label [description]
* @param {[type]} message [description]
* @return {[type]} [description]
*/
buildBitcoinURI : function(address, amount, label, message) {
var tmpl = ["bitcoin:", address, "?"];
if(amount) {
tmpl = tmpl.concat(["amount=", encodeURIComponent(amount), "&"]);
}
if(label) {
tmpl = tmpl.concat(["label=", encodeURIComponent(label), "&"]);
}
if(message) {
tmpl = tmpl.concat(["message=", encodeURIComponent(message), "&"]);
}
// Remove prefixing extra
var lastc = tmpl[tmpl.length-1];
if(lastc == "&" || lastc == "?") {
tmpl = tmpl.splice(0, tmpl.length-1);
}
return tmpl.join("");
},
/**
* Build special HTML for bitcoin address manipulation.
* @param {DOM} elem Templatized target
* @param {DOM} source Original source tree element with data attributes
*/
buildControls : function(elem, source) {
// Replace .bitcoin-address in the template
var addr = elem.find(".bitcoin-address");
// Add a maker class so that we don't reapply template
// on the subsequent scans
addr.addClass("bitcoin-address-controls");
addr.text(source.attr("data-bc-address"));
// Copy orignal attributes;
$.each(["address", "amount", "label", "message"], function() {
var attrName = "data-bc-" + this;
elem.attr(attrName, source.attr(attrName));
});
// Build BTC URL
var url = this.buildBitcoinURI(source.attr("data-bc-address"),
source.attr("data-bc-amount"),
source.attr("data-bc-label"),
source.attr("data-bc-message"));
elem.find(".bitcoin-address-action-send").attr("href", url);
},
/**
* Get the template element defined in the options.
* @return {[type]} [description]
*/
getTemplate : function() {
var template = document.getElementById(this.config.template);
if(!template) {
throw new Error("Bitcoin address template element missing:" + this.config.template);
}
template = $(template);
if(template.size() != 1) {
throw new Error("Bitcoin address template DOM does not contain a single element");
}
return template;
},
/**
* Applies bitcoinaddress DOM template to a certain element.
*
* The `target` element must contain necessary data-attributes
* from where we scoop the info.
*
* Also builds bitcoin: URI.
*
* @param {jQuery} elem jQuery selection of target bitcoin address
* @param {jQuery} template (optional) Template element to be applied
*/
applyTemplate : function(target, template) {
if(!template) {
template = this.getTemplate();
}
// Make a deep copy, so we don't accidentally modify
// template elements in-place
var elem = template.clone(false, true);
this.buildControls(elem, target);
// Make sure we are visible (HTML5 way, CSS way)
// and clean up the template id if we managed to copy it around
elem.removeAttr("hidden id");
elem.show();
target.replaceWith(elem);
},
/**
* Scan the page for bitcoin addresses.
*
* Create user interface for all bitcoin address elements on the page-.
* You can call this function multiple times if new bitcoin addresses become available.
*/
scan: function() {
var self = this;
var template = this.getTemplate();
// Optionally bail out if the default selection
// is not given (user calls applyTemplate() manually)
if(!this.config.selector) {
return;
}
$(this.config.selector).each(function() {
var $this = $(this);
// Template already applied
if($this.hasClass("bitcoin-address-controls")) {
return;
}
// Make sure we don't apply the template on the template itself
if($this.parents("#" + self.config.template).size() > 0) {
return;
}
// Don't reapply templates on subsequent scans
self.applyTemplate($this, template);
});
},
/**
* Prepare selection in .bitcoin-address-container for copy paste
*/
prepareCopySelection : function(elem) {
var addy = elem.find(".bitcoin-address");
window.getSelection().selectAllChildren(addy.get(0));
elem.find(".bitcoin-action-hint").hide();
elem.find(".bitcoin-action-hint-copy").slideDown();
},
/**
* Send payment action handler
*/
onActionSend : function(e) {
var elem = $(e.target).parents(".bitcoin-address-container");
// We never know if the click action was succesfully complete
elem.find(".bitcoin-action-hint").hide();
elem.find(".bitcoin-action-hint-send").slideDown();
},
/**
* Copy action handler.
*/
onActionCopy : function(e) {
e.preventDefault();
var elem = $(e.target).parents(".bitcoin-address-container");
this.prepareCopySelection(elem);
return false;
},
/**
* Generates QR code inside the target element.
*/
generateQR : function(qrContainer) {
var elem = qrContainer.parents(".bitcoin-address-container");
//var addr = elem.attr("data-bc-address");
var url = this.buildBitcoinURI(elem.attr("data-bc-address"),
elem.attr("data-bc-amount"),
elem.attr("data-bc-label"));
console.log("QR address URL is ", url);
var options = $.extend({}, this.config.qr, {
text: url
});
var qrCode = new qrcode.QRCode(qrContainer.get(0), options);
},
/**
* QR code generation action.
*/
onActionQR : function(e) {
e.preventDefault();
var elem = $(e.target).parents(".bitcoin-address-container");
var addr = elem.attr("data-bc-address");
var qrContainer = elem.find(".bitcoin-address-qr-container");
// Lazily generate the QR code
if(qrContainer.children().size() === 0) {
this.generateQR(qrContainer);
}
elem.find(".bitcoin-action-hint").hide();
elem.find(".bitcoin-action-hint-qr").slideDown();
return false;
},
onClick : function(e) {
var elem = $(e.target).parents(".bitcoin-address-container");
this.prepareCopySelection(elem);
},
initUX : function() {
var self = this;
$(document.body).on("click", ".bitcoin-address-action-copy", $.proxy(this.onActionCopy, this));
$(document.body).on("click", ".bitcoin-address-action-send", $.proxy(this.onActionSend, this));
$(document.body).on("click", ".bitcoin-address-action-qr", $.proxy(this.onActionQR, this));
$(document.body).on("click", ".bitcoin-address", $.proxy(this.onClick, this));
// Hide any copy hints when user presses CTRL+C
// on any part of the page
$(document.body).on("copy", function() {
$(".bitcoin-action-hint-copy").slideUp();
});
if(this.config.generateQREagerly) {
$(".bitcoin-address-container").each(function() {
var elem = $(this);
var addr = elem.attr("data-bc-address");
var qrContainer = elem.find(".bitcoin-address-qr-container");
self.generateQR(qrContainer);
});
}
},
/**
* Call to initialize the detault bitcoinprices UI.
*/
init : function(_config) {
var self = this;
if(!_config) {
throw new Error("You must give bitcoinaddress config object");
}
this.config = _config;
$ = this.config.jQuery || jQuery;
this.scan();
this.initUX();
}
};
},{"./qrcode.js":5}],2:[function(require,module,exports){
/* jshint globalstrict:true */
/* globals require */
"use strict";
var $ = require("jquery/dist/jquery")(window);
var bitcoinprices = require("bitcoinprices");
var bitcoinaddress = require("./bitcoinaddress");
$(document).ready(function() {
// Basic initialization
bitcoinaddress.init({
// jQuery selector defining bitcon addresses on the page
// needing the boost
selector: ".bitcoin-address",
// Id of the DOM template element we use to decorate the addresses.
// This must contain placefolder .bitcoin-address
template: "bitcoin-address-template",
// Passed directly to QRCode.js
// https://github.com/davidshimjs/qrcodejs
qr : {
width: 128,
height: 128,
colorDark : "#000000",
colorLight : "#ffffff"
},
jQuery: $
});
// Construct USD nominated donation button
$(document).on("marketdataavailable", function() {
// Scrape source for the amount and convert the USD nominated amount ot BTC
var usdDonationElem = $("#donation-usd");
var amount = usdDonationElem.attr("data-usd-amount");
var btcAmount = bitcoinprices.convert(parseFloat(amount), "USD", "BTC");
// Round 7 decimals as
// BC wallets may not be able to deal with too many
// decimals in the price
btcAmount = Math.round(btcAmount*10000000)/10000000;
// The address is marked with a special CSS class,
// so that it doesn't get initialized with bitcoin addresses
// on page load, as we don't have market data available by then
var addr = usdDonationElem.find(".bitcoin-address-usd");
// Apply bitcoin address template
addr.attr("data-bc-amount", btcAmount);
bitcoinaddress.applyTemplate(addr);
// Show the donation amount in the selected currency
var currency = bitcoinprices.getActiveCurrency();
var amountInActiveCurrency = bitcoinprices.convert(parseFloat(amount), "USD", currency);
var donationPrice = usdDonationElem.find(".clickable-price");
donationPrice.html(bitcoinprices.formatPrice(amountInActiveCurrency, currency, true));
donationPrice.attr("data-btc-price", btcAmount);
usdDonationElem.show();
});
// Initialize bitcoinprices helper library needed
// for USD nominated prices
bitcoinprices.init({
url: "https://api.bitcoinaverage.com/ticker/all",
marketRateVariable: "24h_avg",
currencies: ["BTC", "USD", "EUR", "CNY"],
symbols: {
"BTC": "<i class='fa fa-btc'></i>"
},
defaultCurrency: "BTC",
ux : {
clickPrices : true,
menu : true,
},
// Pass our explicit jQuery object
jQuery: $
});
// For the test purpose, trigger extra scan to see it doesn't do anything evil
// with the layout
bitcoinaddress.scan();
});
},{"./bitcoinaddress":1,"bitcoinprices":3,"jquery/dist/jquery":4}],3:[function(require,module,exports){
/**
* bitcoinprices.js
*
* Display human-friendly bitcoin prices, both desktop and mobile.
*
* Copyright 2013 Mikko Ohtamaa http://opensourcehacker.com
*
* Licensed under MIT license.
*/
/* global define */
// UMD boilerplate
// https://github.com/umdjs/umd/blob/master/returnExports.js
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jQuery'], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like enviroments that support module.exports,
// like Node.
// jQuery(window) is jQuery 2.1+
module.exports = factory(require('jquery/dist/jquery')(window));
} else {
// Browser globals (root is window)
root.bitcoinprices = factory(root.jQuery);
}
}(this, function (jQuery) {
"use strict";
// Store jQuery locally, so we can override it
// with an external option
var $ = jQuery;
return {
/** Store exchange rate data as returned by bitcoinaverages.com */
data : null,
/** Our configuration options */
config : null,
/**
* Update market rate data from the server using JSON AJAX request.
*
* Assumes the server sets proper cache headers, so we are not bombing
* the server.
*/
loadData : function () {
var self = this;
$.getJSON(self.config.url, function(resp) {
self.data = resp;
$(document).trigger("marketdataavailable");
}).error(function() {
throw new Error("Could not load exchage rate data from:" + self.config.url);
});
},
/**
* Convert between BTC and fiat currecy.
*
* @param {Number} amount Currency amount to convert
* @param {String} source Three-letter currency code
* @param {String} target Three-letter currency code
* @return {Number} Amount in other currency
*/
convert : function(amount, source, target) {
var inverse;
if(!$.isNumeric(amount)) {
throw new Error("Amount must be numeric");
}
if(!source || !target) {
throw new Error("You need to give both source and target currency:" + source + " " + target);
}
// No conversion
if(source == "BTC" && target == "BTC") {
return amount;
}
if(!(source == "BTC" || target == "BTC")) {
// Convert through BTC
return this.convert(this.convert(amount, source, "BTC"), "BTC", target);
}
if(source == "BTC") {
inverse = true;
// http://stackoverflow.com/a/16201730/315168
target = [source, source = target][0];
} else {
inverse = false;
}
if(!this.data) {
throw new Error("Exchange rate data not available");
}
var currencyData = this.data[source];
if(!currencyData) {
throw new Error("We do not have market data for currency: " + source);
}
var rate = currencyData[this.config.marketRateVariable];
if(!rate) {
throw new Error("Cannot parse bitcoinaverage data for " + source + " " + this.config.url);
}
if(inverse) {
return amount*rate;
} else {
return amount/rate;
}
},
/**
* Format a price for a currency.
*
* Fills in currency symbols we have configured.
*
* @param {Number} amount
* @param {String} currency Three letter currency code
* @param {Boolean} symbol Add currency symbol
* @return {String} HTML snippet
*/
formatPrice : function (amount, currency, symbol) {
var decimals;
if(currency == "BTC") {
decimals = 8;
} else {
decimals = 2;
}
var formatted = amount.toFixed(decimals);
if(symbol) {
formatted += " " + this.getCurrencySymbol(currency);
}
return formatted;
},
/**
* Get HTML for a currency symbol
* @param {String} currency Three-letter currency code
*/
getCurrencySymbol : function(currency) {
var symbols = this.config.symbols || {};
var symbol = this.config.symbols[currency] || currency;
return symbol;
},
/**
* Assume we have market data available.
*
* Update the prices to reflect the current state of selected
* currency and market price.
*/
updatePrices : function() {
var self = this;
var currentCurrency = this.getActiveCurrency();
// Find all elements which declare themselves to present BTC prices
$("[data-btc-price]").each(function() {
var elem = $(this);
var btcPrice = elem.attr("data-btc-price");
try {
btcPrice = parseFloat(btcPrice, 10);
} catch(e) {
// On invalid price keep going forward
// silently ignoring this
return;
}
var priceSymbol = elem.attr("data-price-symbol") != "off";
var inCurrentCurrency = self.convert(btcPrice, "BTC", currentCurrency);
elem.html(self.formatPrice(inCurrentCurrency, currentCurrency, priceSymbol));
});
},
/**
* Update currency symbols on the page which are not directly associated with a price.
*/
updateCurrencySymbols : function() {
$(".current-currency-symbol").html(this.getCurrencySymbol(this.getActiveCurrency()));
},
/**
* Get the currency selected by the user.
*/
getActiveCurrency : function() {
return window.localStorage["bitcoinprices.currency"] || this.config.defaultCurrency || "BTC";
},
/**
* If we have an active currency which is not provided by current data return to BTC;
*/
resetCurrency : function() {
var currency = this.getActiveCurrency();
var idx = $.inArray(currency, this.config.currencies);
if(idx < 0) {
window.localStorage["bitcoinprices.currency"] = "BTC";
}
},
/**
* Loop available currencies, select next one.
*
* @return {String} user-selected next three-letter currency code
*/
toggleNextActiveCurrency : function() {
var currency = this.getActiveCurrency();
var idx = $.inArray(currency, this.config.currencies);
if(idx < 0) {
idx = 0;
}
idx = (++idx) % this.config.currencies.length;
currency = window.localStorage["bitcoinprices.currency"] = this.config.currencies[idx];
return currency;
},
/**
* User changes the default currency through clicking a price.
*/
installClicker : function() {
var self = this;
function onclick(e) {
e.preventDefault();
self.toggleNextActiveCurrency();
$(document).trigger("activecurrencychange");
}
// We have now market data available,
// decoreate elements so the user knows there is interaction
$("[data-btc-price]").addClass("clickable-price");
$(".current-currency-symbol").addClass("clickable-price");
$(".clickable-price").click(onclick);
},
/**
* Populate Bootstrap dropdown menu "currency-dropdown" with available currency choices.
*
* Automatically toggle the currently activated currency.
*/
installCurrencyMenu : function() {
var self = this;
var menu = $(".currency-dropdown");
function updateCurrencyInMenu(currency) {
var symbol = self.getCurrencySymbol(currency);
menu.find(".currency-symbol").html(symbol);
menu.find("li[data-currency]").removeClass("active");
menu.find("li[data-currency=" + currency + "]").addClass("active");
}
function buildMenu() {
$.each(self.config.currencies, function() {
var symbol = self.getCurrencySymbol(this);
var template = [
"<li class='currency-menu-entry' data-currency='",
this,
"'><a role='menuitem'>",
symbol,
"</a></li>"
];
var html = template.join("");
menu.find("ul").append(html);
});
}
buildMenu();
$(document).on("activecurrencychange", function() {
var active = self.getActiveCurrency();
updateCurrencyInMenu(active);
});
menu.on("click", ".currency-menu-entry", function(e) {
var currency = $(this).attr("data-currency");
window.localStorage["bitcoinprices.currency"] = currency;
$(document).trigger("activecurrencychange");
});
// Initialize the currency from what the user had on the last page load
var active = this.getActiveCurrency();
updateCurrencyInMenu(active);
},
/**
* Make prices clickable and tooltippable.
*
* Assume we have market data available.
*/
installUX : function() {
var self = this;
if(self.config.ux.clickPrices) {
this.installClicker();
}
if(self.config.ux.menu) {
this.installCurrencyMenu();
}
// Whenever some UX element updates the active currency then refresh the page
$(document).bind("activecurrencychange", function() {
self.updatePrices();
self.updateCurrencySymbols();
});
},
/**
* Call to initialize the detault bitcoinprices UI.
*/
init : function(_config) {
if(!_config) {
throw new Error("You must give config object");
}
var self = this;
this.config = _config;
// Allow jQuery override
// (solves many problems with require() jQuery includes)
if(this.config.jQuery) {
$ = this.config.jQuery;
}
if(this.config.url) {
// Chec we are not running headless testing mode
$(document).bind("marketdataavailable", function() {
self.updatePrices();
self.updateCurrencySymbols();
self.installUX();
});
this.loadData();
}
}
};
}));
},{"jquery/dist/jquery":4}],4:[function(require,module,exports){
/*!
* jQuery JavaScript Library v2.1.0-beta3
* http://jquery.com/
*
* Includes Sizzle.js
* http://sizzlejs.com/
*
* Copyright 2005, 2013 jQuery Foundation, Inc. and other contributors
* Released under the MIT license
* http://jquery.org/license
*
* Date: 2013-12-20T22:31Z
*/
(function( global, factory ) {
if ( typeof module === "object" && typeof module.exports === "object" ) {
// For CommonJS and CommonJS-like environments where a proper window is present,
// execute the factory and get jQuery
// For environments that do not inherently posses a window with a document
// (such as Node.js), expose a jQuery-making factory as module.exports
// This accentuates the need for the creation of a real window
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info
module.exports = global.document ?
factory( global ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
// Pass this, window may not be defined yet
}(this, function( window ) {
// Can't do this because several apps including ASP.NET trace
// the stack via arguments.caller.callee and Firefox dies if
// you try to trace through "use strict" call chains. (#13335)
// Support: Firefox 18+
//
var arr = [];
var slice = arr.slice;
var concat = arr.concat;
var push = arr.push;
var indexOf = arr.indexOf;
var class2type = {};
var toString = class2type.toString;
var hasOwn = class2type.hasOwnProperty;
var trim = "".trim;
var support = {};
var
// Use the correct document accordingly with window argument (sandbox)
document = window.document,
version = "2.1.0-beta3",
// Define a local copy of jQuery
jQuery = function( selector, context ) {
// The jQuery object is actually just the init constructor 'enhanced'
// Need init if jQuery is called (just allow error to be thrown if not included)
return new jQuery.fn.init( selector, context );
},
// Matches dashed string for camelizing
rmsPrefix = /^-ms-/,
rdashAlpha = /-([\da-z])/gi,
// Used by jQuery.camelCase as callback to replace()
fcamelCase = function( all, letter ) {
return letter.toUpperCase();
};
jQuery.fn = jQuery.prototype = {
// The current version of jQuery being used
jquery: version,
constructor: jQuery,
// Start with an empty selector
selector: "",
// The default length of a jQuery object is 0
length: 0,
toArray: function() {
return slice.call( this );
},
// Get the Nth element in the matched element set OR
// Get the whole matched element set as a clean array
get: function( num ) {
return num != null ?
// Return a 'clean' array
( num < 0 ? this[ num + this.length ] : this[ num ] ) :
// Return just the object
slice.call( this );
},
// Take an array of elements and push it onto the stack
// (returning the new matched element set)
pushStack: function( elems ) {
// Build a new jQuery matched element set
var ret = jQuery.merge( this.constructor(), elems );
// Add the old object onto the stack (as a reference)
ret.prevObject = this;
ret.context = this.context;
// Return the newly-formed element set
return ret;
},
// Execute a callback for every element in the matched set.
// (You can seed the arguments with an array of args, but this is
// only used internally.)
each: function( callback, args ) {
return jQuery.each( this, callback, args );
},
map: function( callback ) {
return this.pushStack( jQuery.map(this, function( elem, i ) {
return callback.call( elem, i, elem );
}));
},
slice: function() {
return this.pushStack( slice.apply( this, arguments ) );
},
first: function() {
return this.eq( 0 );
},
last: function() {
return this.eq( -1 );
},
eq: function( i ) {
var len = this.length,
j = +i + ( i < 0 ? len : 0 );
return this.pushStack( j >= 0 && j < len ? [ this[j] ] : [] );
},
end: function() {
return this.prevObject || this.constructor(null);
},
// For internal use only.
// Behaves like an Array's method, not like a jQuery method.
push: push,
sort: arr.sort,
splice: arr.splice
};
jQuery.extend = jQuery.fn.extend = function() {
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if ( typeof target === "boolean" ) {
deep = target;
// skip the boolean and the target
target = arguments[ i ] || {};
i++;
}
// Handle case when target is a string or something (possible in deep copy)
if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
target = {};
}
// extend jQuery itself if only one argument is passed
if ( i === length ) {
target = this;
i--;
}
for ( ; i < length; i++ ) {
// Only deal with non-null/undefined values
if ( (options = arguments[ i ]) != null ) {
// Extend the base object
for ( name in options ) {
src = target[ name ];
copy = options[ name ];
// Prevent never-ending loop
if ( target === copy ) {
continue;
}
// Recurse if we're merging plain objects or arrays
if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
if ( copyIsArray ) {
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[ name ] = jQuery.extend( deep, clone, copy );
// Don't bring in undefined values
} else if ( copy !== undefined ) {
target[ name ] = copy;
}
}
}
}
// Return the modified object
return target;
};
jQuery.extend({
// Unique for each copy of jQuery on the page