Merge "Yangman - request progress, error msg"
[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'];
7
8     function RequestDataCtrl($filter, $mdToast, $scope, RequestsService) {
9         var requestData = this;
10
11         requestData.paramsArray = [];
12         requestData.data = '';
13         requestData.type = null;
14
15         requestData.dataEditorOptions = {
16             mode: 'javascript',
17             lineNumbers: true,
18             lineWrapping: true,
19             matchBrackets: true,
20             extraKeys: {
21                 'Ctrl-Space': 'autocomplete',
22             },
23             onLoad: function (cmInstance) {
24
25                 cmInstance.data = {
26                     parameterListObj: $scope.parametersList,
27                     codeFontSize: 14,
28                 };
29
30                 angular.element(cmInstance.display.wrapper).css('fontSize', cmInstance.data.codeFontSize + 'px');
31
32                 cmInstance.on('changes', function () {
33                     if (angular.isFunction(cmInstance.showHint)) {
34                         cmInstance.showHint();
35                     }
36                 });
37
38                 cmInstance.on('cursorActivity', function () {
39                     var lineString = cmInstance.getLine(cmInstance.getCursor().line);
40                     requestData.paramsArray = RequestsService.scanDataParams($scope.parametersList, lineString);
41
42                     if (!$scope.$$phase) {
43                         $scope.$apply();
44                     }
45                 });
46
47                 cmInstance.on('keydown', function (codemirror, event) {
48                     if (event.altKey) {
49                         switch (event.key){
50                             case '+':
51                                 if (cmInstance.data.codeFontSize < 30) {
52                                     cmInstance.data.codeFontSize++;
53                                 }
54                                 angular.element(cmInstance.display.wrapper).css(
55                                     'fontSize',
56                                     cmInstance.data.codeFontSize + 'px'
57                                 );
58                                 break;
59                             case '-':
60                                 if (cmInstance.data.codeFontSize > 5) {
61                                     cmInstance.data.codeFontSize--;
62                                 }
63                                 angular.element(cmInstance.display.wrapper).css(
64                                     'fontSize',
65                                     cmInstance.data.codeFontSize + 'px'
66                                 );
67                                 break;
68                         }
69
70                     }
71                 });
72
73
74             },
75         };
76
77         // methods
78         requestData.init = init;
79         requestData.showCMHint = showCMHint;
80
81         /**
82          * Set code mirror theme and readonly property considering requestData.type
83          */
84         function initEditorOptions() {
85             requestData.dataEditorOptions.theme = requestData.type === 'RECEIVED' ? 'eclipse-disabled' : 'eclipse';
86             requestData.dataEditorOptions.readOnly = requestData.type === 'RECEIVED';
87         }
88
89
90         /**
91          * Show hints for first codemirror instancesk
92          */
93         function showCMHint(type) {
94
95             if (!localStorage.getItem('yangman_cm_hint_got_it')){
96
97                 $mdToast.show(
98                     $mdToast.simple()
99                         .textContent($filter('translate')('YANGMAN_CM_FONT_SIZE_HINT'))
100                         .action($filter('translate')('YANGMAN_CM_HINT_DONT_SHOW'))
101                         .position('top right')
102                         .parent(angular.element('.yangmanModule__right-panel__req-data__cm-' + type))
103                         .hideDelay(10000)
104                 ).then(function (response){
105                     if (response === 'ok') {
106                         localStorage.setItem('yangman_cm_hint_got_it', 1);
107                     }
108                 });
109             }
110         }
111
112         /**
113          * Initialization
114          * @param type
115          */
116         function init(type){
117             requestData.type = type;
118             initEditorOptions();
119
120             showCMHint(type);
121
122             $scope.$on('YANGMAN_SET_CODEMIRROR_DATA_' + type, function (event, args){
123                 requestData.data = args.params.data;
124                 showCMHint(type);
125             });
126
127             $scope.$on('YANGMAN_GET_CODEMIRROR_DATA_' + type, function (event, args){
128                 args.params.reqData = requestData.data;
129             });
130
131
132         }
133
134
135     }
136
137 });
138