0
|
1 /*
|
|
2 * tablesaw: A set of plugins for responsive tables
|
|
3 * Button component
|
|
4 * Copyright (c) 2013 Filament Group, Inc.
|
|
5 * MIT License
|
|
6 */
|
|
7
|
|
8 (function() {
|
|
9 var pluginName = "tablesawbtn",
|
|
10 methods = {
|
|
11 _create: function() {
|
|
12 return $(this).each(function() {
|
|
13 $(this)
|
|
14 .trigger("beforecreate." + pluginName)
|
|
15 [pluginName]("_init")
|
|
16 .trigger("create." + pluginName);
|
|
17 });
|
|
18 },
|
|
19 _init: function() {
|
|
20 var oEl = $(this),
|
|
21 sel = this.getElementsByTagName("select")[0];
|
|
22
|
|
23 if (sel) {
|
|
24 // TODO next major version: remove .btn-select
|
|
25 $(this)
|
|
26 .addClass("btn-select tablesaw-btn-select")
|
|
27 [pluginName]("_select", sel);
|
|
28 }
|
|
29 return oEl;
|
|
30 },
|
|
31 _select: function(sel) {
|
|
32 var update = function(oEl, sel) {
|
|
33 var opts = $(sel).find("option");
|
|
34 var label = document.createElement("span");
|
|
35 var el;
|
|
36 var children;
|
|
37 var found = false;
|
|
38
|
|
39 label.setAttribute("aria-hidden", "true");
|
|
40 label.innerHTML = " ";
|
|
41
|
|
42 opts.each(function() {
|
|
43 var opt = this;
|
|
44 if (opt.selected) {
|
|
45 label.innerHTML = opt.text;
|
|
46 }
|
|
47 });
|
|
48
|
|
49 children = oEl.childNodes;
|
|
50 if (opts.length > 0) {
|
|
51 for (var i = 0, l = children.length; i < l; i++) {
|
|
52 el = children[i];
|
|
53
|
|
54 if (el && el.nodeName.toUpperCase() === "SPAN") {
|
|
55 oEl.replaceChild(label, el);
|
|
56 found = true;
|
|
57 }
|
|
58 }
|
|
59
|
|
60 if (!found) {
|
|
61 oEl.insertBefore(label, oEl.firstChild);
|
|
62 }
|
|
63 }
|
|
64 };
|
|
65
|
|
66 update(this, sel);
|
|
67 // todo should this be tablesawrefresh?
|
|
68 $(this).on("change refresh", function() {
|
|
69 update(this, sel);
|
|
70 });
|
|
71 }
|
|
72 };
|
|
73
|
|
74 // Collection method.
|
|
75 $.fn[pluginName] = function(arrg, a, b, c) {
|
|
76 return this.each(function() {
|
|
77 // if it's a method
|
|
78 if (arrg && typeof arrg === "string") {
|
|
79 return $.fn[pluginName].prototype[arrg].call(this, a, b, c);
|
|
80 }
|
|
81
|
|
82 // don't re-init
|
|
83 if ($(this).data(pluginName + "active")) {
|
|
84 return $(this);
|
|
85 }
|
|
86
|
|
87 $(this).data(pluginName + "active", true);
|
|
88
|
|
89 $.fn[pluginName].prototype._create.call(this);
|
|
90 });
|
|
91 };
|
|
92
|
|
93 // add methods
|
|
94 $.extend($.fn[pluginName].prototype, methods);
|
|
95
|
|
96 // TODO OOP this and add to Tablesaw object
|
|
97 })();
|