Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 1 | // js for UI Reference App table view |
| 2 | (function () { |
| 3 | 'use strict'; |
| 4 | |
| 5 | // injected refs |
| 6 | var $log, $scope, fs, wss; |
| 7 | |
| 8 | // constants |
| 9 | var detailsReq = 'uiRefTableDetailsRequest', |
| 10 | detailsResp = 'uiRefTableDetailsResponse', |
| 11 | pName = 'ov-ui-ref-table-item-details-panel', |
| 12 | |
| 13 | propOrder = ['id', 'label', 'code'], |
| 14 | friendlyProps = ['Item ID', 'Item Label', 'Special Code']; |
| 15 | |
| 16 | |
| 17 | function addProp(tbody, index, value) { |
| 18 | var tr = tbody.append('tr'); |
| 19 | |
| 20 | function addCell(cls, txt) { |
| 21 | tr.append('td').attr('class', cls).html(txt); |
| 22 | } |
| 23 | addCell('label', friendlyProps[index] + ' :'); |
| 24 | addCell('value', value); |
| 25 | } |
| 26 | |
| 27 | function populatePanel(panel) { |
| 28 | var title = panel.append('h3'), |
| 29 | tbody = panel.append('table').append('tbody'); |
| 30 | |
| 31 | title.text('Item Details'); |
| 32 | |
| 33 | propOrder.forEach(function (prop, i) { |
| 34 | addProp(tbody, i, $scope.panelDetails[prop]); |
| 35 | }); |
| 36 | |
| 37 | panel.append('hr'); |
| 38 | panel.append('h4').text('Comments'); |
| 39 | panel.append('p').text($scope.panelDetails.comment); |
| 40 | } |
| 41 | |
| 42 | function respDetailsCb(data) { |
| 43 | $scope.panelDetails = data.details; |
| 44 | $scope.$apply(); |
| 45 | } |
| 46 | |
| 47 | angular.module('ovUiRefTable', []) |
Simon Hunt | df8a640 | 2016-02-04 10:58:54 -0800 | [diff] [blame] | 48 | .run(['IconService', function (is) { |
| 49 | // we want to be able to re-use our custom glyph (defined in |
| 50 | // uiRefTopovOverlay.js) as our nav icon. So create an |
| 51 | // icon-to-glyph binding here: |
| 52 | is.registerIconMapping('nav_uiref', 'ui-ref-overlay-smiley'); |
| 53 | }]) |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 54 | .controller('OvUiRefTableCtrl', |
| 55 | ['$log', '$scope', 'TableBuilderService', |
| 56 | 'FnService', 'WebSocketService', |
| 57 | |
| 58 | function (_$log_, _$scope_, tbs, _fs_, _wss_) { |
| 59 | $log = _$log_; |
| 60 | $scope = _$scope_; |
| 61 | fs = _fs_; |
| 62 | wss = _wss_; |
| 63 | |
| 64 | var handlers = {}; |
| 65 | $scope.panelDetails = {}; |
| 66 | |
| 67 | // details response handler |
| 68 | handlers[detailsResp] = respDetailsCb; |
| 69 | wss.bindHandlers(handlers); |
| 70 | |
| 71 | // custom selection callback |
| 72 | function selCb($event, row) { |
| 73 | if ($scope.selId) { |
| 74 | wss.sendEvent(detailsReq, { id: row.id }); |
| 75 | } else { |
| 76 | $scope.hidePanel(); |
| 77 | } |
| 78 | $log.debug('Got a click on:', row); |
| 79 | } |
| 80 | |
| 81 | // TableBuilderService creating a table for us |
| 82 | tbs.buildTable({ |
| 83 | scope: $scope, |
| 84 | tag: 'uiRefTable', |
| 85 | selCb: selCb |
| 86 | }); |
| 87 | |
| 88 | // cleanup |
| 89 | $scope.$on('$destroy', function () { |
| 90 | wss.unbindHandlers(handlers); |
| 91 | $log.log('OvUiRefTableCtrl has been destroyed'); |
| 92 | }); |
| 93 | |
| 94 | $log.log('OvUiRefTableCtrl has been created'); |
| 95 | }]) |
| 96 | |
| 97 | .directive('ovUiRefTableItemDetailsPanel', ['PanelService', 'KeyService', |
| 98 | function (ps, ks) { |
| 99 | return { |
| 100 | restrict: 'E', |
| 101 | link: function (scope, element, attrs) { |
| 102 | // insert details panel with PanelService |
| 103 | // create the panel |
| 104 | var panel = ps.createPanel(pName, { |
| 105 | width: 200, |
| 106 | margin: 20, |
| 107 | hideMargin: 0 |
| 108 | }); |
| 109 | panel.hide(); |
| 110 | scope.hidePanel = function () { panel.hide(); }; |
| 111 | |
| 112 | function closePanel() { |
| 113 | if (panel.isVisible()) { |
| 114 | $scope.selId = null; |
| 115 | panel.hide(); |
| 116 | return true; |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | // create key bindings to handle panel |
| 122 | ks.keyBindings({ |
| 123 | esc: [closePanel, 'Close the details panel'], |
| 124 | _helpFormat: ['esc'] |
| 125 | }); |
| 126 | ks.gestureNotes([ |
| 127 | ['click', 'Select a row to show item details'] |
| 128 | ]); |
| 129 | |
| 130 | // update the panel's contents when the data is changed |
| 131 | scope.$watch('panelDetails', function () { |
| 132 | if (!fs.isEmptyObject(scope.panelDetails)) { |
| 133 | panel.empty(); |
| 134 | populatePanel(panel); |
| 135 | panel.show(); |
| 136 | } |
| 137 | }); |
| 138 | |
| 139 | // cleanup on destroyed scope |
| 140 | scope.$on('$destroy', function () { |
| 141 | ks.unbindKeys(); |
| 142 | ps.destroyPanel(pName); |
| 143 | }); |
| 144 | } |
| 145 | }; |
| 146 | }]); |
| 147 | }()); |