Debian Codegraph @

js/widgets/widget-view.js

raw symbols SCIP
1 /* Widget: view (beta) - updated 7/11/2016 (v2.26.6) */
2 /* By Justin F. Hallett (https://github.com/TheSin-)
3 * Requires tablesorter v2.8+ and jQuery 1.7+
4 */
5 /*jshint browser:true, jquery:true, unused:false */
6 /*global jQuery: false */
7 ;(function($) {
8 'use strict';
9
10 var ts = $.tablesorter,
11 is_hidden = false,
12 tpos, ttop, tleft,
13
14 view = ts.view = {
15 copyCaption: function(c, wo) {
16 view.removeCaption(c, wo);
17
18 if (c.$table.find('caption').length > 0) {
19 $(wo.view_caption).text(c.$table.find('caption').text());
20 }
21 },
22
23 removeCaption: function(c, wo) {
24 $(wo.view_caption).empty();
25 },
26
27 buildToolBar: function(c, wo) {
28 view.removeToolBar(c, wo);
29 view.copyCaption(c, wo);
30 var $toolbar = $(wo.view_toolbar);
31
32 $.each(wo.view_layouts, function(k, v) {
33 var classes = wo.view_switcher_class;
34 if (k === wo.view_layout) {
35 classes += ' active';
36 }
37
38 var $switcher = $('<a>', {
39 'href': '#',
40 'class': classes,
41 'data-view-type': k,
42 'title': v.title
43 });
44
45 $switcher.append($('<i>', {
46 'class': v.icon
47 }));
48
49 $toolbar.append($switcher);
50 });
51
52 $toolbar.find('.' + wo.view_switcher_class).on('click', function(e) {
53 e.preventDefault();
54 if ($(this).hasClass('active')) {
55 // if currently clicked button has the active class
56 // then we do nothing!
57 return false;
58 } else {
59 // otherwise we are clicking on the inactive button
60 // and in the process of switching views!
61 $toolbar.find('.' + wo.view_switcher_class).removeClass('active');
62 $(this).addClass('active');
63 wo.view_layout = $(this).attr('data-view-type');
64
65 if (wo.view_layouts[wo.view_layout].raw === true) {
66 view.remove(c, wo);
67 view.buildToolBar(c, wo);
68 } else {
69 if (is_hidden === false) {
70 view.hideTable(c, wo);
71 }
72 view.buildView(c, wo);
73 }
74 }
75 });
76 },
77
78 removeToolBar: function(c, wo) {
79 $(wo.view_toolbar).empty();
80 view.removeCaption(c, wo);
81 },
82
83 buildView: function(c, wo) {
84 view.removeView(c, wo);
85
86 var myview = wo.view_layouts[wo.view_layout];
87 var $container = $(myview.container, {
88 'class': wo.view_layout
89 });
90
91 ts.getColumnText(c.$table, 0, function(data) {
92 var tmpl = myview.tmpl;
93
94 $.each($(data.$row).find('td'), function(k, v) {
95 var attrs = {};
96 var reg = '{col' + k + '}';
97 $.each(v.attributes, function(idx, attr) {
98 attrs[attr.nodeName] = attr.nodeValue;
99 });
100 var content = $(v).html();
101 // Add 2 spans, one is dropped when using .html()
102 var span = $('<span />').append($('<span/>', attrs).append(content));
103 tmpl = tmpl.replace(new RegExp(reg, 'g'), span.html());
104
105 reg = '{col' + k + ':raw}';
106 tmpl = tmpl.replace(new RegExp(reg, 'g'), $(v).text());
107 });
108
109 var $tmpl = $(tmpl);
110 $.each(data.$row[0].attributes, function(idx, attr) {
111 if (attr.nodeName === 'class') {
112 $tmpl.attr(attr.nodeName, $tmpl.attr(attr.nodeName) + ' ' + attr.nodeValue);
113 } else {
114 $tmpl.attr(attr.nodeName, attr.nodeValue);
115 }
116 });
117 $container.append($tmpl);
118 });
119
120 $(wo.view_container).append($container);
121 c.$table.triggerHandler('viewComplete');
122 },
123
124 removeView: function(c, wo) {
125 $(wo.view_container).empty();
126 },
127
128 hideTable: function(c) {
129 tpos = c.$table.css('position');
130 ttop = c.$table.css('bottom');
131 tleft = c.$table.css('left');
132
133 c.$table.css({
134 'position': 'absolute',
135 'top': '-10000px',
136 'left': '-10000px'
137 });
138
139 is_hidden = true;
140 },
141
142 init: function(c, wo) {
143 if (wo.view_layout === false) {
144 return;
145 }
146
147 if (typeof wo.view_layouts[wo.view_layout] === 'undefined') {
148 return;
149 }
150
151 if (is_hidden === false) {
152 view.hideTable(c, wo);
153 }
154
155 c.$table.on('tablesorter-ready', function() {
156 view.buildToolBar(c, wo);
157 view.buildView(c, wo);
158 });
159 },
160
161 remove: function(c, wo) {
162 view.removeToolBar(c, wo);
163 view.removeView(c, wo);
164
165 c.$table.css({
166 'position': tpos,
167 'top': ttop,
168 'left': tleft
169 });
170
171 is_hidden = false;
172 }
173 };
174
175 ts.addWidget({
176 id: 'view',
177 options: {
178 view_toolbar: '#ts-view-toolbar',
179 view_container: '#ts-view',
180 view_caption: '#ts-view-caption',
181 view_switcher_class: 'ts-view-switcher',
182 view_layout: false,
183 view_layouts: {}
184 },
185
186 init: function(table, thisWidget, c, wo) {
187 view.init(c, wo);
188 },
189
190 remove: function(table, c, wo) {
191 view.remove(c, wo);
192 }
193 });
194
195 })(jQuery);