blob: 7f37c5dc7167b417956c9d4571ef0783c28c85f6 [file] [log] [blame]
Simon Hunt84f4c2a2015-09-23 17:52:45 -07001// js for sample app custom view
Thomas Vachuskaa2ae4222015-04-29 18:42:09 -07002(function () {
3 'use strict';
4
Simon Hunte11ce5a2015-07-21 12:11:04 -07005 // injected refs
Simon Hunt84f4c2a2015-09-23 17:52:45 -07006 var $log, $scope, fs, wss;
Simon Hunte11ce5a2015-07-21 12:11:04 -07007
8 // constants
Simon Hunt84f4c2a2015-09-23 17:52:45 -07009 var detailsReq = 'sampleCustomDetailsRequest',
10 detailsResp = 'sampleCustomDetailsResponse',
11 pName = 'ov-sample-custom-item-details-panel',
Simon Hunte11ce5a2015-07-21 12:11:04 -070012
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070013 propOrder = ['id', 'label', 'code'],
14 friendlyProps = ['Item ID', 'Item Label', 'Special Code'];
Simon Hunte11ce5a2015-07-21 12:11:04 -070015
16
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070017 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
Simon Hunte11ce5a2015-07-21 12:11:04 -070042 function respDetailsCb(data) {
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070043 $scope.panelDetails = data.details;
Simon Hunte11ce5a2015-07-21 12:11:04 -070044 $scope.$apply();
45 }
46
Simon Hunt84f4c2a2015-09-23 17:52:45 -070047 angular.module('ovSampleCustom', [])
48 .controller('OvSampleCustomCtrl',
Simon Hunte11ce5a2015-07-21 12:11:04 -070049 ['$log', '$scope', 'TableBuilderService',
50 'FnService', 'WebSocketService',
Thomas Vachuskaa2ae4222015-04-29 18:42:09 -070051
Simon Hunte11ce5a2015-07-21 12:11:04 -070052 function (_$log_, _$scope_, tbs, _fs_, _wss_) {
53 $log = _$log_;
54 $scope = _$scope_;
55 fs = _fs_;
56 wss = _wss_;
Thomas Vachuskaa2ae4222015-04-29 18:42:09 -070057
Simon Hunte11ce5a2015-07-21 12:11:04 -070058 var handlers = {};
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070059 $scope.panelDetails = {};
Simon Hunte11ce5a2015-07-21 12:11:04 -070060
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070061 // details response handler
62 handlers[detailsResp] = respDetailsCb;
63 wss.bindHandlers(handlers);
Simon Hunte11ce5a2015-07-21 12:11:04 -070064
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070065 // custom selection callback
Simon Hunte11ce5a2015-07-21 12:11:04 -070066 function selCb($event, row) {
Simon Hunte11ce5a2015-07-21 12:11:04 -070067 if ($scope.selId) {
68 wss.sendEvent(detailsReq, { id: row.id });
69 } else {
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070070 $scope.hidePanel();
Simon Hunte11ce5a2015-07-21 12:11:04 -070071 }
72 $log.debug('Got a click on:', row);
73 }
74
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070075 // TableBuilderService creating a table for us
Simon Hunte11ce5a2015-07-21 12:11:04 -070076 tbs.buildTable({
77 scope: $scope,
Simon Hunt84f4c2a2015-09-23 17:52:45 -070078 tag: 'sampleCustom',
Simon Hunte11ce5a2015-07-21 12:11:04 -070079 selCb: selCb
80 });
81
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070082 // cleanup
Simon Hunte11ce5a2015-07-21 12:11:04 -070083 $scope.$on('$destroy', function () {
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070084 wss.unbindHandlers(handlers);
Simon Hunt84f4c2a2015-09-23 17:52:45 -070085 $log.log('OvSampleCustomCtrl has been destroyed');
Simon Hunte11ce5a2015-07-21 12:11:04 -070086 });
Thomas Vachuskaa2ae4222015-04-29 18:42:09 -070087
Simon Hunt84f4c2a2015-09-23 17:52:45 -070088 $log.log('OvSampleCustomCtrl has been created');
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070089 }])
90
Simon Hunt84f4c2a2015-09-23 17:52:45 -070091 .directive('ovSampleCustomItemDetailsPanel', ['PanelService', 'KeyService',
92 function (ps, ks) {
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070093 return {
94 restrict: 'E',
95 link: function (scope, element, attrs) {
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -070096 // insert details panel with PanelService
97 // create the panel
98 var panel = ps.createPanel(pName, {
99 width: 200,
100 margin: 20,
101 hideMargin: 0
102 });
103 panel.hide();
104 scope.hidePanel = function () { panel.hide(); };
105
106 function closePanel() {
107 if (panel.isVisible()) {
108 $scope.selId = null;
109 panel.hide();
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700110 return true;
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -0700111 }
Simon Hunt84f4c2a2015-09-23 17:52:45 -0700112 return false;
Bri Prebilic Cole68844ba2015-07-22 15:41:37 -0700113 }
114
115 // create key bindings to handle panel
116 ks.keyBindings({
117 esc: [closePanel, 'Close the details panel'],
118 _helpFormat: ['esc']
119 });
120 ks.gestureNotes([
121 ['click', 'Select a row to show item details']
122 ]);
123
124 // update the panel's contents when the data is changed
125 scope.$watch('panelDetails', function () {
126 if (!fs.isEmptyObject(scope.panelDetails)) {
127 panel.empty();
128 populatePanel(panel);
129 panel.show();
130 }
131 });
132
133 // cleanup on destroyed scope
134 scope.$on('$destroy', function () {
135 ks.unbindKeys();
136 ps.destroyPanel(pName);
137 });
138 }
139 };
140 }]);
Thomas Vachuskaa2ae4222015-04-29 18:42:09 -0700141}());