Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 1 | /* |
| 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 Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 25 | function renderTable(div, config) { |
Bri Prebilic Cole | 5c2cab9 | 2015-01-23 16:53:29 -0800 | [diff] [blame^] | 26 | var table = div.append('table').attr('fixed-header', ''), |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 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, |
Bri Prebilic Cole | 5c2cab9 | 2015-01-23 16:53:29 -0800 | [diff] [blame^] | 53 | tbody, tr, i; |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 54 | tbody = div.select('tbody'); |
| 55 | |
| 56 | // get the array associated with the first object, such as "devices" |
Bri Prebilic Cole | 5c2cab9 | 2015-01-23 16:53:29 -0800 | [diff] [blame^] | 57 | // 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 Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 59 | (data[Object.keys(data)[0]]).forEach(function (item) { |
| 60 | tr = tbody.append('tr'); |
Bri Prebilic Cole | 5c2cab9 | 2015-01-23 16:53:29 -0800 | [diff] [blame^] | 61 | for(i = 0; i < numTableCols; i += 1) { |
| 62 | if(item.hasOwnProperty(colIds[i])) { |
| 63 | tr.append('td').html(item[colIds[i]]); |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | function renderAndLoadTable(div, config, data) { |
| 70 | loadTableData(data, div, (renderTable(div, config))); |
| 71 | } |
| 72 | |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 73 | angular.module('onosWidget') |
| 74 | .factory('TableService', ['$log', function (_$log_) { |
| 75 | $log = _$log_; |
| 76 | |
| 77 | return { |
Bri Prebilic Cole | aa8d2ed | 2015-01-23 16:53:29 -0800 | [diff] [blame] | 78 | renderTable: renderTable, |
| 79 | loadTableData: loadTableData, |
| 80 | renderAndLoadTable: renderAndLoadTable |
Bri Prebilic Cole | 093739a | 2015-01-23 10:22:50 -0800 | [diff] [blame] | 81 | }; |
| 82 | }]); |
| 83 | |
| 84 | }()); |