a93aff3ffa854f34dfdb0e1a8e557dd16d4c3363
[sdc.git] / catalog-ui / src / app / directives / utils / sdc-keyboard-events / sdc-keyboard-events.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22
23 export interface ISdcKeyboardEventsScope extends ng.IScope {
24     keyEnter:Function;
25     keyShift:Function;
26     keyCtrl:Function;
27     keyEscape:Function;
28     keySpace:Function;
29 }
30
31 export class SdcKeyboardEventsDirective implements ng.IDirective {
32
33     constructor() {
34     }
35
36     scope = {
37         keyEnter: '=',
38         keyShift: '=',
39         keyCtrl: '=',
40         keyEscape: '=',
41         keySpace: '='
42     };
43
44     public replace = false;
45     public restrict = 'A';
46     public transclude = false;
47
48     link = (scope:ISdcKeyboardEventsScope, element:ng.IAugmentedJQuery, attrs:angular.IAttributes) => {
49
50         element.bind("keydown keypress", function (event) {
51             //console.log(event.which);
52             switch (event.which) {
53                 case 13: // enter key
54                     scope.$apply(function () {
55                         if (scope.keyEnter) {
56                             scope.keyEnter();
57                             event.preventDefault();
58                         }
59                     });
60                     break;
61                 case 16: // shift key
62                     scope.$apply(function () {
63                         if (scope.keyShift) {
64                             scope.keyShift();
65                             event.preventDefault();
66                         }
67                     });
68                     break;
69                 case 17: // ctrl key
70                     scope.$apply(function () {
71                         if (scope.keyCtrl) {
72                             scope.keyCtrl();
73                             event.preventDefault();
74                         }
75                     });
76                     break;
77                 case 27: // escape key
78                     scope.$apply(function () {
79                         if (scope.keyEscape) {
80                             scope.keyEscape();
81                             event.preventDefault();
82                         }
83                     });
84                     break;
85                 case 32: // space key
86                     scope.$apply(function () {
87                         if (scope.keySpace) {
88                             scope.keySpace();
89                             event.preventDefault();
90                         }
91                     });
92                     break;
93             }
94         });
95
96     };
97
98     public static factory = ()=> {
99         return new SdcKeyboardEventsDirective();
100     };
101
102 }
103
104 SdcKeyboardEventsDirective.factory.$inject = [];