aboutsummaryrefslogtreecommitdiff
path: root/src/js/fiveui/injected/ui.js
blob: d7427209c85098e676c943099bf38e4681994c5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Module     : injected/fiveui-injected-ui.js
 * Copyright  : (c) 2011-2012, Galois, Inc.
 *
 * Maintainer :
 * Stability  : Provisional
 * Portability: Portable
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

(function(){
   /**
    * Storage namespace for in-browser logic
    */
   var core = {};
   core.port = obtainPort();

   core.ui = $('<div></div>');

   core.lockDepth = 0;

   core.lockMask = function() {
     core.lockDepth = core.lockDepth + 1;
     if(core.lockDepth == 1) {
       core.port.emit('MaskRules', null);
     }
   };

   core.unlockMask = function() {
     core.lockDepth = core.lockDepth - 1;
     if(core.lockDepth == 0) {
       core.port.emit('UnmaskRules', null);
     }
   };

   /**
    * Due to the lack of a confirmation continuation with the port api, this
    * function runs a continuation after 10ms of delivering the MaskRules
    * message, then waits another 10ms before delivering the UnmaskRules
    * message.  The two delays seem to give a better chance that the
    * continuation runs within the masked context.
    */
   core.maskRules = function(body) {
     core.lockMask();

     setTimeout(function() {
       body();
       setTimeout(function() {
         core.unlockMask();
       }, 10);
     }, 10);
   };

   core.highlightProblem = function(elt) {
     core.maskRules(function() {
       var oldStyle = elt.attr('style');

       elt.attr('style', 'background-color: rgba(255,0,0,0.3); background-image: none;');
       elt.addClass('uic-problem');

       return oldStyle;
     });
   };

   core.maskProblem = function(elt, oldStyle) {
     core.maskRules(function() {
       if (oldStyle == undefined) {
         elt.removeAttr('style');
       } else {
         elt.attr('style', oldStyle);
       }

       elt.removeClass('uic-problem');
     });
   };

   core.renderStatsTemplate = _.template(
     [ '<table class="fiveui-table">'
     , '  <tr>'
     , '    <td class="fiveui-table-text">rules checked:</td>'
     , '    <td class="fiveui-table-number"><%= numRules %></td>'
     , '  </tr>'
     , '  <tr>'
     , '    <td class="fiveui-table-text">elements checked:</td>'
     , '    <td class="fiveui-table-number"><%= numElts %></td>'
     , '  </tr>'
     , '  <tr>'
     , '    <td class="fiveui-table-text">elapsed time (ms):</td>'
     , '    <td class="fiveui-table-number"><%= time %></td>'
     , '  </tr>'
     , '</table>'
     ].join(''));

   core.renderStats = function (stats) {

     // give stats some sane defaults.
     stats = stats || {};
     _.defaults(stats, {
       numRules: 0,
       numElts: 0,
       start: 0,
       end: 0,
     });

     core.maskRules(function () {

       var statsDiv, statsDetail;
       statsDiv = $('#fiveui-stats');
       statsDiv.children().remove();

       stats.time = stats.end - stats.start;
       statsDiv.html(core.renderStatsTemplate(stats));
     });
   };

   core.renderProblem = function(prob) {
     core.maskRules(function() {
       var probDiv = $('<div class="pr"></div>');


       /** Problem Controls **/
       var prControls = $('<div class="prControls"></div>');
       probDiv.append(prControls);

       var prSeverity = $('<div class="prSeverity"></div>');
       prControls.append(prSeverity);

       if (1 == prob.severity) {
         prSeverity.addClass('prSeverity-err');
       } else {
         prSeverity.addClass('prSeverity-warn');
       }

       var prExpand = $('<div class="prExpand prExpand-right"></div>');
       prControls.append(prExpand);

       /** Problem Content **/
       var prMessage = $('<div class="prMessage"></div>');
       probDiv.append(prMessage);       

       var prTitle = $('<div class="prTitle">'+prob.name+'</div>');
       prMessage.append(prTitle);

       var prDetails = $('<div class="prDetails"></div>');
       prMessage.append(prDetails);


       var prDescr  = $('<p>'+prob.descr+'</p>');
       var prPath   = $('<p>'+prob.xpath+'</p>');
       prDetails.append(prDescr);
       if (prob.msg) {
         var reportMsg = $('<div class="prReportMessage">'+prob.msg+'</div>');
         prDetails.append(reportMsg);
       }
       prDetails.append(prPath);
       prDetails.hide();

       $('#problemList').append(probDiv);

       prExpand.click(
         function() {
           var oldStyle;
           var elt = $(this);
           if(elt.is('.prExpand-down')) {
             elt.removeClass('prExpand-down')
                .addClass('prExpand-right');
             prDetails.hide();
             core.maskProblem(fiveui.query('.' + prob.hash), oldStyle);
           } else {
             elt.addClass('prExpand-down')
                .removeClass('prExpand-right');
             prDetails.show();
             oldStyle = core.highlightProblem(fiveui.query('.' + prob.hash));
           }

           return false;
         });
     });
   };

   var dragStop = function(evt,e) {
     core.port.emit('Position', core.ui.parent().position());
   };

   var resizeStop = function(evt,e) {
     core.port.emit('Size', { width: core.ui.width(), height: core.ui.height() });
   };

   var beforeClose = function(evt,e) {
     core.port.emit('CloseUI');
   };

   var registerBackendListeners = function(port) {

     port.on('ShowUI', function(unused) {
       core.ui.dialog('open');
     });

     port.on('ShowProblem', function(problem) {
       core.renderProblem(problem);
     });

     port.on('ShowStats', function(stats) {
       core.renderStats(stats);
     });

     port.on('RestoreUI', function(state) {
       core.ui.append($('<div id="controls"></div>'));

       core.ui.append($('<div id="problemList"></div>'));

       var newDialog = core.ui.dialog({ title: 'FiveUI',
                        dragStop: dragStop,
                        resizeStop: resizeStop,
                        beforeClose: beforeClose,
                        position: [state.winState.x, state.winState.y],
                        width: state.winState.width,
                        height: state.winState.height,
                        autoOpen: false,
                        zIndex: 50000
                      });
       newDialog.parent().attr('id', 'fiveui-top');

       $('#controls').append($('<div id="clearButton"></div>')
                             .button({ label: 'clear' }));

       $('#clearButton').click(function() {
             $('#problemList').children().remove();
             port.emit('ClearProblems');

             core.renderStats();
             $('prExpand-down').click();

             // Just in case the click event on prExpand-down missde anything:
             core.maskProblem(fiveui.query('.uic-problem'), undefined);
             core.renderStats();
         });

       ///////////////////////////////////////////
       // Add a button that causes a debuger break.
       //
       // handy for playing with Jquery on the dom.
       // Note: This only works in Google Chrome.
       $('#controls').append($('<div id="breakButton"></div>')
                             .button({ label: 'break' }));
       $('#breakButton').click(function() {
                                 debugger;       //
                               });               //
       ////////////////////////////////////////////

       core.ui.append($('<div id="fiveui-stats"></div>'));

       if(!state.winState.closed) {
         core.ui.dialog('open');
       }

       $(state.problems).each(function(ix,prob) {
                                core.renderProblem(prob);
                              });

       core.renderStats(state.stats);
     });
   };

   registerBackendListeners(core.port);
})();