UNPKG

subtotal

Version:

Subtotal.js is a JavaScript plugin for PivotTable.js. It renders subtotals of rows and columns with the ability to expand and collapse rows.

3 lines (2 loc) 17.6 kB
(function(){var t,e;t=[["name","gender","colour","birthday","trials","successes"],["Nick","male","blue","1982-11-07",103,12],["Jane","female","red","1982-11-08",95,25],["John","male","blue","1982-12-08",112,30],["Carol","female","yellow","1983-11-11",102,14],["Raj","male","blue","1982-11-07",103,12],["Rani","female","red","1982-11-08",95,25],["Joshi","male","blue","1982-12-09",112,12],["Vel","male","yellow","1982-12-01",112,25],["Sai","male","red","1982-11-08",112,30],["Geeth","female","blue","1982-12-03",112,14],["Malar","male","red","1982-11-05",112,12],["Nila","male","blue","1982-12-07",112,25],["Yaazhi","male","yellow","1982-12-06",112,30],["Mukhi","male","yellow","1982-11-07",112,14]],e=[["name","gender","colour","birthday","trials","successes"],["Nick","male","blue","1982-11-07",103,12],["Jane","female","red","1982-11-08",95,25],["John","male","blue","1982-12-08",112,30],["Carol","female","yellow","1983-12-08",102,14]],describe("$.pivotUI()",function(){return describe("with no rows/cols, default count aggregator, subtotal renderer",function(){var e;return e=null,beforeEach(function(o){return e=$("<div>").pivotUI(t,{dataClass:$.pivotUtilities.SubtotalPivotData,renderers:$.pivotUtilities.subtotal_renderers,onRefresh:o})}),it("has all the basic UI elements",function(t){return expect(e.find("td.pvtAxisContainer").length).toBe(3),expect(e.find("td.pvtRendererArea").length).toBe(1),expect(e.find("td.pvtVals").length).toBe(1),expect(e.find("select.pvtRenderer").length).toBe(1),expect(e.find("select.pvtAggregator").length).toBe(1),expect(e.find("span.pvtAttr").length).toBe(6),expect(e.find("th.pvtTotalLabel").length).toBe(1),expect(e.find("td.pvtGrandTotal").length).toBe(1),t()}),it("reflects its inputs",function(t){return expect(e.find("td.pvtUnused span.pvtAttr").length).toBe(6),expect(e.find("select.pvtRenderer").val()).toBe("Table With Subtotal"),expect(e.find("select.pvtAggregator").val()).toBe("Count"),t()}),it("renders a table",function(t){return expect(e.find("table.pvtTable").length).toBe(1),t()}),describe("its renderer output",function(){return it("has the correct type and number of cells",function(t){return expect(e.find("th.pvtTotalLabel").length).toBe(1),expect(e.find("td.pvtGrandTotal").length).toBe(1),t()}),it("has the correct textual representation",function(t){return expect(e.find("table.pvtTable").text()).toBe(["Totals","14"].join("")),t()}),it("has a correct grand total with data value",function(t){return expect(e.find("td.pvtGrandTotal").text()).toBe("14"),expect(e.find("td.pvtGrandTotal").data("value")).toBe(14),t()})})}),describe("with collapsed rows and cols, subtotal_aggregators",function(){var e;return e=null,beforeEach(function(o){return e=$("<div>").pivotUI(t,{dataClass:$.pivotUtilities.SubtotalPivotData,rows:["gender","colour"],cols:["birthday","trials"],aggregators:$.pivotUtilities.subtotal_aggregators,vals:["successes"],renderers:$.pivotUtilities.subtotal_renderers,rendererOptions:{collapseColsAt:0,collapseRowsAt:0},onRefresh:o})}),it("has all the basic UI elements",function(t){return expect(e.find("td.pvtAxisContainer").length).toBe(3),expect(e.find("td.pvtRendererArea").length).toBe(1),expect(e.find("td.pvtVals").length).toBe(1),expect(e.find("select.pvtRenderer").length).toBe(1),expect(e.find("select.pvtAggregator").length).toBe(1),expect(e.find("span.pvtAttr").length).toBe(6),t()}),it("reflects its inputs",function(t){return expect(e.find("td.pvtUnused span.pvtAttr").length).toBe(2),expect(e.find("td.pvtRows span.pvtAttr").length).toBe(2),expect(e.find("td.pvtCols span.pvtAttr").length).toBe(2),expect(e.find("select.pvtRenderer").val()).toBe("Table With Subtotal"),expect(e.find("select.pvtAggregator").val()).toBe("Sum As Fraction Of Parent Row"),t()}),it("renders a table",function(t){return expect(e.find("table.pvtTable").length).toBe(1),t()}),describe("its renderer output",function(){return it("has the correct type and number of cells",function(t){return expect(e.find("th.pvtAxisLabel").length).toBe(4),expect(e.find("th.pvtAxisLabel.collapsed").length).toBe(4),expect(e.find("th.pvtRowLabel.rowcollapsed").length).toBe(2),expect(e.find("th.pvtColLabel.colcollapsed").length).toBe(10),expect(e.find("th.pvtTotalLabel.rowTotal").length).toBe(1),expect(e.find("th.pvtTotalLabel.colTotal").length).toBe(1),expect(e.find("td.pvtVal.pvtColSubtotal.pvtRowSubtotal").length).toBe(20),expect(e.find("td.pvtTotal.rowTotal.pvtRowSubtotal").length).toBe(2),expect(e.find("td.pvtTotal.colTotal.pvtColSubtotal").length).toBe(10),expect(e.find("td.pvtGrandTotal").length).toBe(1),t()}),it("has the correct textual representation",function(t){return expect(e.find("th.pvtColLabel").text()).toBe(" ▶ 1982-11-05 ▶ 1982-11-07 ▶ 1982-11-08 ▶ 1982-12-01 ▶ 1982-12-03 ▶ 1982-12-06 ▶ 1982-12-07 ▶ 1982-12-08 ▶ 1982-12-09 ▶ 1983-11-1111210311295112112112112112112112102"),expect(e.find("th.pvtRowLabel").text()).toBe(" ▶ femaleblueredyellow ▶ maleblueredyellow"),t()}),it("has a correct spot-checked cell with data value",function(t){return expect(e.find("td.pvtVal.pvtRowSubtotal.row0.col3.rowcol0.colcol1").text()).toBe("17.9%"),expect(e.find("td.pvtVal.pvtTotal.colTotal.pvtColSubtotal.col3.colcol0").data("value")).toBe(80/280),t()})})}),describe("with row and col subtotal hidden on expand",function(){var e;return e=null,beforeEach(function(o){return e=$("<div>").pivotUI(t,{dataClass:$.pivotUtilities.SubtotalPivotData,rows:["colour","birthday","name"],cols:["trials","gender","successes"],aggregators:$.pivotUtilities.subtotal_aggregators,vals:["successes"],renderers:$.pivotUtilities.subtotal_renderers,rendererOptions:{rowSubtotalDisplay:{hideOnExpand:!0},colSubtotalDisplay:{hideOnExpand:!0}},onRefresh:o})}),it("has all the basic UI elements",function(t){return expect(e.find("td.pvtAxisContainer").length).toBe(3),expect(e.find("td.pvtRendererArea").length).toBe(1),expect(e.find("td.pvtVals").length).toBe(1),expect(e.find("select.pvtRenderer").length).toBe(1),expect(e.find("select.pvtAggregator").length).toBe(1),expect(e.find("span.pvtAttr").length).toBe(6),t()}),it("reflects its inputs",function(t){return expect(e.find("td.pvtUnused span.pvtAttr").length).toBe(0),expect(e.find("td.pvtRows span.pvtAttr").length).toBe(3),expect(e.find("td.pvtCols span.pvtAttr").length).toBe(3),expect(e.find("select.pvtRenderer").val()).toBe("Table With Subtotal"),expect(e.find("select.pvtAggregator").val()).toBe("Sum As Fraction Of Parent Row"),t()}),it("renders a table",function(t){return expect(e.find("table.pvtTable").length).toBe(1),t()}),describe("its renderer output",function(){return it("has the correct type and number of cells",function(t){return expect(e.find("th.pvtAxisLabel").length).toBe(6),expect(e.find("th.pvtAxisLabel.collapsed").length).toBe(0),expect(e.find("th.pvtRowLabel.rowshow.rowcollapsed").length).toBe(0),expect(e.find("th.pvtColLabel.colshow.colcollapsed").length).toBe(0),expect(e.find("th.pvtRowLabel.rowshow.rowexpanded").length).toBe(28),expect(e.find("th.pvtColLabel.colshow.colexpanded").length).toBe(17),expect(e.find("th.pvtRowLabelFiller.rowhide.rowexpanded").length).toBe(14),expect(e.find("th.pvtColLabelFiller.colhide.colexpanded").length).toBe(9),expect(e.find("td.pvtColSubtotal.pvtRowSubtotal.colhide.colexpanded.rowhide.rowexpanded").length).toBe(126),expect(e.find("th.pvtTotalLabel.rowTotal").length).toBe(1),expect(e.find("th.pvtTotalLabel.colTotal").length).toBe(1),expect(e.find("td.pvtTotal.rowTotal.pvtRowSubtotal").length).toBe(14),expect(e.find("td.pvtTotal.colTotal.pvtColSubtotal").length).toBe(9),expect(e.find("td.pvtGrandTotal").length).toBe(1),t()}),it("has the correct textual representation",function(t){return expect(e.find("th.pvtColLabel").text()).toBe(" ◢ 95 ◢ 102 ◢ 103 ◢ 112 ◢ female ◢ female ◢ male ◢ female ◢ male2514121412142530"),expect(e.find("th.pvtRowLabel").text()).toBe(" ◢ blue ◢ 1982-11-07NickRaj ◢ 1982-12-03Geeth ◢ 1982-12-07Nila ◢ 1982-12-08John ◢ 1982-12-09Joshi ◢ red ◢ 1982-11-05Malar ◢ 1982-11-08JaneRaniSai ◢ yellow ◢ 1982-11-07Mukhi ◢ 1982-12-01Vel ◢ 1982-12-06Yaazhi ◢ 1983-11-11Carol"),t()}),it("has a correct spot-checked cell with data value",function(t){return expect(e.find("td.pvtVal.rowshow.colshow.row10.col5.rowcol2.colcol2").text()).toBe("100.0%"),expect(e.find('td.pvtVal[data-rownode="1"][data-colnode="6"]').text()).toBe("50.0%"),t()})})}),describe("with row subtotal displayed at bottom",function(){var e;return e=null,beforeEach(function(o){return e=$("<div>").pivotUI(t,{dataClass:$.pivotUtilities.SubtotalPivotData,rows:["colour","birthday"],cols:["gender","successes"],vals:["successes"],renderers:$.pivotUtilities.subtotal_renderers,rendererOptions:{rowSubtotalDisplay:{displayOnTop:!1}},onRefresh:o})}),it("has all the basic UI elements",function(t){return expect(e.find("td.pvtAxisContainer").length).toBe(3),expect(e.find("td.pvtRendererArea").length).toBe(1),expect(e.find("td.pvtVals").length).toBe(1),expect(e.find("select.pvtRenderer").length).toBe(1),expect(e.find("select.pvtAggregator").length).toBe(1),expect(e.find("span.pvtAttr").length).toBe(6),t()}),it("reflects its inputs",function(t){return expect(e.find("td.pvtUnused span.pvtAttr").length).toBe(2),expect(e.find("td.pvtRows span.pvtAttr").length).toBe(2),expect(e.find("td.pvtCols span.pvtAttr").length).toBe(2),expect(e.find("select.pvtRenderer").val()).toBe("Table With Subtotal"),t()}),it("renders a table",function(t){return expect(e.find("table.pvtTable").length).toBe(1),t()}),describe("its renderer output",function(){return it("has the correct type and number of cells",function(t){return expect(e.find(".pvtTable tbody tr:first-child th").length).toBe(1),expect(e.find(".pvtTable tbody tr:first-child th").attr("rowspan")).toBe("7"),expect(e.find("tr:nth-child(7) th.pvtRowLabelFiller").length).toBe(1),expect(e.find("th.pvtTotalLabel.rowTotal").length).toBe(1),expect(e.find("th.pvtTotalLabel.colTotal").length).toBe(1),expect(e.find("td.pvtTotal.rowTotal.pvtRowSubtotal").length).toBe(3),expect(e.find("td.pvtTotal.colTotal.pvtColSubtotal").length).toBe(2),expect(e.find("td.pvtGrandTotal").length).toBe(1),t()}),it("has the correct textual representation",function(t){return expect(e.find("th.pvtColLabel").text()).toBe(" ◢ female ◢ male142512142530"),expect(e.find("th.pvtRowLabel").text()).toBe(" ◢ blue1982-11-071982-12-031982-12-071982-12-081982-12-09 ◢ red1982-11-051982-11-08 ◢ yellow1982-11-071982-12-011982-12-061983-11-11"),t()}),it("has a correct spot-checked cell with data value",function(t){return expect(e.find("tr:nth-child(7) td.pvtTotal").text()).toBe("6"),expect(e.find("td.pvtVal.row5.rowcol0.pvtRowSubtotal.col0.colcol0.pvtColSubtotal").text()).toBe("2"),expect(e.find('td.pvtVal[data-rownode="13"][data-colnode="7"]').text()).toBe("3"),t()})})})}),describe("$.pivot()",function(){return describe("with no rows/cols, default count aggregator, subtotal renderer",function(){var e;return e=$("<div>").pivot(t,{dataClass:$.pivotUtilities.SubtotalPivotData,renderer:$.pivotUtilities.subtotal_renderers["Table With Subtotal"]}),it("has all the basic UI elements",function(){return expect(e.find("th.pvtTotalLabel").length).toBe(1),expect(e.find("td.pvtGrandTotal").length).toBe(1)}),it("renders a table",function(){return expect(e.find("table.pvtTable").length).toBe(1)}),describe("its renderer output",function(){return it("has the correct type and number of cells",function(){return expect(e.find("th.pvtTotalLabel").length).toBe(1),expect(e.find("td.pvtGrandTotal").length).toBe(1)}),it("has the correct textual representation",function(){return expect(e.find("table.pvtTable").text()).toBe(["Totals","14"].join(""))}),it("has a correct grand total with data value",function(){return expect(e.find("td.pvtGrandTotal").text()).toBe("14"),expect(e.find("td.pvtGrandTotal").data("value")).toBe(14)})})}),describe("with rows/cols, subtotal_aggregator",function(){var e;return e=$("<div>").pivot(t,{dataClass:$.pivotUtilities.SubtotalPivotData,rows:["gender","colour"],cols:["birthday","trials"],aggregator:$.pivotUtilities.subtotal_aggregators["Sum As Fraction Of Parent Column"](["successes"]),renderer:$.pivotUtilities.subtotal_renderers["Table With Subtotal"]}),it("renders a table",function(){return expect(e.find("table.pvtTable").length).toBe(1)}),describe("its renderer output",function(){return it("has the correct type and number of cells",function(){return expect(e.find("th.pvtAxisLabel").length).toBe(4),expect(e.find("th.collapsed").length).toBe(0),expect(e.find("th.expanded, th.rowexpanded, th.colexpanded").length).toBe(46),expect(e.find("th.pvtAxisLabel.expanded").length).toBe(4),expect(e.find("th.pvtRowLabel.rowexpanded").length).toBe(8),expect(e.find("th.pvtColLabel.colexpanded").length).toBe(22),expect(e.find("th.pvtTotalLabel.rowTotal").length).toBe(1),expect(e.find("th.pvtTotalLabel.colTotal").length).toBe(1),expect(e.find("td.pvtVal.pvtColSubtotal.pvtRowSubtotal").length).toBe(20),expect(e.find("td.pvtTotal.rowTotal.pvtRowSubtotal").length).toBe(2),expect(e.find("td.pvtTotal.colTotal.pvtColSubtotal").length).toBe(10),expect(e.find("td.pvtGrandTotal").length).toBe(1)}),it("has the correct textual representation",function(){return expect(e.find("th.pvtColLabel").text()).toBe(" ◢ 1982-11-05 ◢ 1982-11-07 ◢ 1982-11-08 ◢ 1982-12-01 ◢ 1982-12-03 ◢ 1982-12-06 ◢ 1982-12-07 ◢ 1982-12-08 ◢ 1982-12-09 ◢ 1983-11-1111210311295112112112112112112112102"),expect(e.find("th.pvtRowLabel").text()).toBe(" ◢ femaleblueredyellow ◢ maleblueredyellow")}),it("has a correct spot-checked cell with data value",function(){return expect(e.find("td.pvtVal.pvtTotal.colTotal.col3.colcol1").text()).toBe("62.5%"),expect(e.find("td.pvtVal.pvtTotal.colTotal.col4.colcol1").data("value")).toBe(.375)})})})}),describe("$.pivotUtilities",function(){return describe(".SubtotalPivotData()",function(){var t;return t={rows:[],cols:[],aggregator:$.pivotUtilities.aggregators["Sum over Sum"](["a","b"]),filter:function(){return!0},sorters:function(){}},describe("with array-of-array input",function(){var e,o;return e=[["a","b"],[1,2],[3,4]],o=new $.pivotUtilities.SubtotalPivotData(e,t),it("has the correct grand total value",function(){return expect(o.getAggregator([],[]).value()).toBe(4/6)})}),describe("with array-of-object input",function(){var e,o;return e=[{a:1,b:2},{a:3,b:4}],o=new $.pivotUtilities.SubtotalPivotData(e,t),it("has the correct grand total value",function(){return expect(o.getAggregator([],[]).value()).toBe(4/6)})}),describe("with function input",function(){var e,o;return e=function(t){return t({a:1,b:2}),t({a:3,b:4})},o=new $.pivotUtilities.SubtotalPivotData(e,t),it("has the correct grand total value",function(){return expect(o.getAggregator([],[]).value()).toBe(4/6)})}),describe("with jQuery table element input",function(){var e,o;return o=$("<table>\n <thead> \n <tr> <th>a</th><th>b</th> </tr>\n </thead> \n <tbody>\n <tr> <td>1</td> <td>2</td> </tr>\n <tr> <td>3</td> <td>4</td> </tr>\n </tbody>\n</table>"),e=new $.pivotUtilities.SubtotalPivotData(o,t),it("has the correct grand total value",function(){return expect(e.getAggregator([],[]).value()).toBe(4/6)})}),describe("with rows/cols, no filters/sorters, count aggregator",function(){var t;return t=new $.pivotUtilities.SubtotalPivotData(e,{rows:["name","colour"],cols:["trials","successes"],aggregator:$.pivotUtilities.aggregators.Count(),filter:function(){return!0},sorters:function(){}}),it("has correctly-ordered row keys",function(){return expect(t.getRowKeys()).toEqual([["Carol","yellow"],["Jane","red"],["John","blue"],["Nick","blue"]])}),it("has correctly-ordered col keys",function(){return expect(t.getColKeys()).toEqual([[95,25],[102,14],[103,12],[112,30]])}),it("can be iterated over",function(){var e,o,l,n,a,r,i,c,p,d;for(r=0,i=0,p=t.getRowKeys(),o=0,n=p.length;o<n;o++)for(c=p[o],d=t.getColKeys(),l=0,a=d.length;l<a;l++)e=d[l],null!=t.getAggregator(c,e).value()?r++:i++;return expect(r).toBe(4),expect(i).toBe(12)}),it("has a correct spot-checked aggregator",function(){var e,o,l,n,a,r,i;for(r=[{spot:[["Carol","yellow"],[102,14]],val:1},{spot:[["Jane","red"],[95,25]],val:1},{spot:[["John","blue"],[112,30]],val:1},{spot:[["Nick","blue"],[103,12]],val:1}],n=[],o=0,l=r.length;o<l;o++)a=r[o],e=t.getAggregator(a.spot[0],a.spot[1]),i=e.value(),expect(i).toBe(1),n.push(expect(e.format(i)).toBe(""+a.val));return n}),it("has correct spot-checked aggregators for subtotal-rows and subtotal-columns",function(){var e,o,l,n,a,r,i;for(r=[{spot:[["Carol"],[102]],val:1},{spot:[["Jane"],[95]],val:1},{spot:[["John"],[112]],val:1},{spot:[["Nick"],[103]],val:1}],n=[],o=0,l=r.length;o<l;o++)a=r[o],e=t.getAggregator(a.spot[0],a.spot[1]),i=e.value(),expect(i).toBe(a.val),n.push(expect(e.format(i)).toBe(""+a.val));return n}),it("has correct row-total for subtotal-rows",function(){var e,o,l,n,a,r,i;for(a=["Carol","Jane","John","Nick"],r=[],l=0,n=a.length;l<n;l++)o=a[l],e=t.getAggregator([o],[]),i=e.value(),expect(i).toBe(1),r.push(expect(e.format(i)).toBe("1"));return r}),it("has correct column-total for subtotal-columns",function(){var e,o,l,n,a,r,i;for(a=[95,102,103,112],r=[],l=0,n=a.length;l<n;l++)o=a[l],e=t.getAggregator([],[o]),i=e.value(),expect(i).toBe(1),r.push(expect(e.format(i)).toBe("1"));return r}),it("has a correct grand total aggregator",function(){var e,o;return e=t.getAggregator([],[]),o=e.value(),expect(o).toBe(4),expect(e.format(o)).toBe("4")})})})})}).call(this); //# sourceMappingURL=pivot_spec.min.js.map