Yangman - changed event strings to constants
[dlux.git] / modules / yangman-resources / src / main / resources / yangman / controllers / request-data.controller.js
1 define([], function () {
2     'use strict';
3
4     angular.module('app.yangman').controller('RequestDataCtrl', RequestDataCtrl);
5
6     RequestDataCtrl.$inject = ['$filter', '$mdToast', '$scope', 'RequestsService', 'constants'];
7
8     function RequestDataCtrl($filter, $mdToast, $scope, RequestsService, constants) {
9         var requestData = this,
10             cmData = {
11                 cmInstance: null,
12                 cmFontSize: 14,
13             };
14
15         requestData.paramsArray = [];
16         requestData.data = '';
17         requestData.type = null;
18
19
20         // todo: move all cm staff to directive
21         requestData.dataEditorOptions = {
22             mode: 'javascript',
23             lineNumbers: true,
24             lineWrapping: true,
25             matchBrackets: true,
26             extraKeys: {
27                 'Ctrl-Space': 'autocomplete',
28             },
29             onLoad: function (cmInstance) {
30
31                 cmData.cmInstance = cmInstance;
32
33                 cmInstance.data = {
34                     parameterListObj: $scope.parametersList,
35                 };
36
37                 angular.element(cmInstance.display.wrapper).css('fontSize', cmData.cmFontSize + 'px');
38
39                 cmInstance.on('changes', function () {
40                     if (angular.isFunction(cmInstance.showHint)) {
41                         cmInstance.showHint();
42                     }
43                 });
44
45                 cmInstance.on('cursorActivity', function () {
46                     var lineString = cmInstance.getLine(cmInstance.getCursor().line);
47                     requestData.paramsArray = RequestsService.scanDataParams($scope.parametersList, lineString);
48
49                     if (!$scope.$$phase) {
50                         $scope.$apply();
51                     }
52                 });
53
54                 cmInstance.on('keydown', function (codemirror, event) {
55                     if (event.altKey) {
56                         switch (event.key){
57                             case '+':
58                                 incCMFontSize();
59                                 angular.element(cmInstance.display.wrapper).css(
60                                     'fontSize',
61                                     cmData.cmFontSize + 'px'
62                                 );
63                                 break;
64                             case '-':
65                                 decCMFontSize();
66                                 angular.element(cmInstance.display.wrapper).css(
67                                     'fontSize',
68                                     cmData.cmFontSize + 'px'
69                                 );
70                                 break;
71                         }
72
73                     }
74                 });
75
76
77             },
78         };
79
80         // methods
81         requestData.init = init;
82         requestData.enlargeCMFont = enlargeCMFont;
83         requestData.reduceCMFont = reduceCMFont;
84
85         function incCMFontSize() {
86             if (cmData.cmFontSize < 30) {
87                 cmData.cmFontSize++;
88             }
89         }
90
91         function decCMFontSize() {
92             if (cmData.cmFontSize > 5) {
93                 cmData.cmFontSize--;
94             }
95         }
96
97         function enlargeCMFont() {
98             incCMFontSize();
99             angular.element(cmData.cmInstance.display.wrapper).css(
100                 'fontSize',
101                 cmData.cmFontSize + 'px'
102             );
103         }
104
105         function reduceCMFont() {
106             decCMFontSize();
107             angular.element(cmData.cmInstance.display.wrapper).css(
108                 'fontSize',
109                 cmData.cmFontSize + 'px'
110             );
111         }
112
113         /**
114          * Set code mirror theme and readonly property considering requestData.type
115          */
116         function initEditorOptions() {
117             requestData.dataEditorOptions.theme = requestData.type === constants.REQUEST_DATA_TYPE_RECEIVED ? 'eclipse-disabled' : 'eclipse';
118             requestData.dataEditorOptions.readOnly = requestData.type === constants.REQUEST_DATA_TYPE_RECEIVED;
119         }
120
121
122         /**
123          * Initialization
124          * @param type
125          */
126         function init(type){
127             requestData.type = type;
128             initEditorOptions();
129
130             $scope.$on(constants.YANGMAN_SET_CODEMIRROR_DATA + type, function (event, args){
131                 requestData.data = args.params.data;
132             });
133
134             $scope.$on(constants.YANGMAN_GET_CODEMIRROR_DATA + type, function (event, args){
135                 args.params.reqData = requestData.data;
136             });
137         }
138     }
139
140 });