blob: 52ee86fe28cfc0d5048a89719eac54fcd44686c7 [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) {
Bri Prebilic Cole5c2cab92015-01-23 16:53:29 -080026 var table = div.append('table').attr('fixed-header', ''),
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080027 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,
Bri Prebilic Cole5c2cab92015-01-23 16:53:29 -080053 tbody, tr, i;
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080054 tbody = div.select('tbody');
55
56 // get the array associated with the first object, such as "devices"
Bri Prebilic Cole5c2cab92015-01-23 16:53:29 -080057 // loop through every object in the array, and every colId in config
58 // put the appropriate property in the td of the table
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080059 (data[Object.keys(data)[0]]).forEach(function (item) {
60 tr = tbody.append('tr');
Bri Prebilic Cole5c2cab92015-01-23 16:53:29 -080061 for(i = 0; i < numTableCols; i += 1) {
62 if(item.hasOwnProperty(colIds[i])) {
63 tr.append('td').html(item[colIds[i]]);
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080064 }
65 }
66 });
67 }
68
69 function renderAndLoadTable(div, config, data) {
70 loadTableData(data, div, (renderTable(div, config)));
71 }
72
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080073 angular.module('onosWidget')
74 .factory('TableService', ['$log', function (_$log_) {
75 $log = _$log_;
76
77 return {
Bri Prebilic Coleaa8d2ed2015-01-23 16:53:29 -080078 renderTable: renderTable,
79 loadTableData: loadTableData,
80 renderAndLoadTable: renderAndLoadTable
Bri Prebilic Cole093739a2015-01-23 10:22:50 -080081 };
82 }]);
83
84}());