blob: bd20b9c496d7b498182c6ef4862d5757bfaaddb7 [file] [log] [blame]
Bri Prebilic Cole093739a2015-01-23 10:22:50 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Widget -- Table Service
19 */
20(function () {
21 'use strict';
22
23 var $log;
24
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080025 function renderTable(div, config) {
26 var table = div.append('table').attr('fixed-header', true),
27 thead, tr, numTableCols, i;
28 table.append('thead');
29 table.append('tbody');
30
31 thead = table.select('thead');
32 tr = thead.append('tr');
33 numTableCols = config.colIds.length;
34
35 for(i = 0; i < numTableCols; i += 1) {
36 tr.append('th').html(config.colText[i]);
37 }
38
39 return config.colIds;
40 }
41
42 // I can delete these comments later...
43 // loadTableData needs to know which IDs are used to create the table...
44 // Potentially, there will be some rows in the JSON that the server is
45 // sending back that will be unused. We don't want to create unneeded rows.
46 // For example, in device view, we aren't displaying "role" or
47 // "available" properties, but they would be displayed if we took it
48 // directly from the data being sent in.
49 function loadTableData(data, div, colIds) {
50 // let me know if you have suggestions for this function
51
52 var numTableCols = colIds.length,
53 tbody, tr, td, i;
54 tbody = div.select('tbody');
55
56 // get the array associated with the first object, such as "devices"
57 // in fakeData
58 // (for some reason it doesn't like the data.keys() function, saying
59 // "undefined is not a function"
60 // Object.keys(data) works though)
61 // loop through every object in the array, and every property in the object
62 // put their property in the td of the table
63 (data[Object.keys(data)[0]]).forEach(function (item) {
64 tr = tbody.append('tr');
65 for(var key in item) {
66 for(i = 0; i < numTableCols; i += 1) {
67 if(key === colIds[i]) {
68 td = tr.append('td').html(item[key]);
69 }
70 }
71 }
72 });
73 }
74
75 function renderAndLoadTable(div, config, data) {
76 loadTableData(data, div, (renderTable(div, config)));
77 }
78
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080079 angular.module('onosWidget')
80 .factory('TableService', ['$log', function (_$log_) {
81 $log = _$log_;
82
83 return {
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080084 renderTable: renderTable,
85 loadTableData: loadTableData,
86 renderAndLoadTable: renderAndLoadTable
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080087 };
88 }]);
89
90}());