-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathappController.coffee
More file actions
425 lines (356 loc) · 13.2 KB
/
appController.coffee
File metadata and controls
425 lines (356 loc) · 13.2 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
###
socr.controller is the controller object for the SOCR app.
@author: selvam , ashwini
@return: {object}
SOCR - Statistical Online Computational Resource
###
socr.controller = (model, view) ->
# PRIVATE PROPERTIES
__accordionDOMSelector__ = "#accordion"
__controllerBackBtnDOMSelector__ = ".controller-back"
__runBtnDOMSelector__ = "#runButton"
__stepBtnDOMSelector__ = "#stepButton"
__stopBtnDOMSelector__ = "#stopButton"
__resetBtnDOMSelector__ = "#resetButton"
__inferBtnDOMSelector__ = "#infer"
__inferenceDatasetIndexDOMSelector__ = "#index"
__inferenceVariableDOMSelector__ = "#variable"
__inferenceAnalysisDOMSelector__ = "#analysis"
__inferencePrecisionDOMSelector__ = '#result-precision'
_id = 0 # Stores the id for setInterval in run mode
_runsElapsed = 0 # Keeps count of number of resamples generated
_runCount = 0
_this = this
_noOfSteps = 0
_currentMode = "Experiment" #App starts with dataDriven mode [default value]
MIN_SAMPLE_GENERATION_STEP_COUNT = 100
MIN_SAMPLE_GENERATION_STEP_TIME = 10 # (in ms)
# PRIVATE METHODS
###
@method: [private] _generate()
@description: This function generates 1000 resamples by calling the generateSample() of model.
@dependencies: generateSample()
###
_generate = (count)->
# _temp = (model.get "stopCount")/ MIN_SAMPLE_GENERATION_STEP_COUNT
# _noOfSteps = Math.ceil(_temp)
console.log _runCount
if _runCount < model.get "stopCount"
_leftCount = model.get("stopCount") - _runCount
if _leftCount > MIN_SAMPLE_GENERATION_STEP_COUNT
i = MIN_SAMPLE_GENERATION_STEP_COUNT
else
i = _leftCount
_runCount += i
model.generateSample() while i--
# percent = Math.ceil((_runsElapsed / _noOfSteps) * 100)
view.updateSlider()
# _runsElapsed++
console.log "runCount:" + _runCount
percent = Math.ceil(100 * _runCount / (model.get "stopCount") )
view.updateStatus "update", percent
else
PubSub.publish "randomSampleGenerationComplete", {'sampleCount':model.getRSampleCount()}
_this.stop()
return
# PUBLIC METHODS
currentMode: _currentMode
###
@method: [private] initialize()
@description:Initializes the app..binds all the buttons...create the show slider
###
initialize: ->
_this = this
console.log "initialize() invoked "
#ADDING EVENT LISTENERS STARTS
#--------------------------------
$(".controller-handle").on "click", view.toggleControllerHandle
$(".help").on "change click", (e) ->
e.preventDefault()
socr.tutorial.toggleStatus()
(if (socr.tutorial.getStatus() is "on") then $(".help").css("background-color", "green").html("<a href='#'>Help : ON</a>") else $(".help").css("background-color", "").html("<a href='#'>Help : OFF</a>"))
return
$("#showButton").on "click", ->
#a check to see if the sample count is 0 or not
view.createList $(".show-list-start").val(), $(".show-list-end").val()
return
$("#share-instance-button").on "click", ->
$(".generate-response").html ""
html = "<p>Dataset:<strong>" + model.getDataset() + "</strong></p>"
html += "<p>Count Size:<strong>" + $("#countSize").val() + "</strong></p>"
html += "<p>datapoints:<strong>" + $("#nSize").val() + "</strong></p>"
$("#settings").html html
return
$("#generate-url-button").on "click", ->
unless model.getDataset() is ""
$("#url").val baseUrl + "index.html?" + "type=url&dataset=" + model.getDataset() + "&countSize=" + $("#countSize").val() + "&nSize=" + $("#nSize").val()
else
console.log "Dataset not initialised"
alertblock = "<div class=\"alert alert-block\">Dataset not initialised</div>"
$(".generate-response").html alertblock
return
$("#reset-button").on "click", ->
_this.reset()
return
$(".input-controls").delegate "td", "mousedown", ->
table.startEdit $(this)
return
#console.log('Logging function called')
$(".input-controls").delegate "input#generateMatrix", "click", ->
console.log "Table Generated"
console.log table.getMatrix()
return
#console.log(table.getMatrix)
$(".input-controls").delegate "input#submatrix", "click", ->
start = $(".input-controls").find("input[name=\"start\"]").val()
end = $(".input-controls").find("input[name=\"end\"]").val()
table.generateSub start, end
return
#ADDING EVENT LISTENERS ENDS
# Twitter Feed
# $('#tweetFeed').jTweetsAnywhere({
# searchParams: 'q=%23socrWebapp',
# count: 10,
# showTweetFeed: {
# autorefresh: {
# mode: 'trigger-insert',
# interval: 60
# },
# paging: {
# mode: 'more'
# },
# showTimestamp: {
# refreshInterval: 30
# }
# }
# });
$("#accordion").accordion()
$(".dropdown-toggle").dropdown()
$(".popups").popover
html: true
trigger: "click"
animation: true
$(".tooltips").tooltip()
view.createShowSlider()
return
initController: ->
# set the K - value in model.
model.setK()
$(".tooltips").tooltip()
$(__controllerBackBtnDOMSelector__).on "click", (e) ->
e.preventDefault()
try
model.reset()
view.reset()
console.log "exp_" + socr.exp.current.name
socr.dataTable.simulationDriven.init "exp_" + socr.exp.current.name
socr.exp.current.initialize()
catch err
console.log err.message
return
$(__runBtnDOMSelector__).on "click", (e) ->
e.preventDefault()
console.log "Run Started"
setTimeout socr.controller.run, 500
return
$(__stepBtnDOMSelector__).on "click", (e) ->
e.preventDefault()
console.log "Step pressed "
socr.controller.step()
return
$(__stopBtnDOMSelector__).on "click", (e) ->
e.preventDefault()
console.log "Stop Pressed "
socr.controller.stop()
PubSub.publish "randomSampleGenerationInterrupted", {}
return
$(__resetBtnDOMSelector__).on "click", (e) ->
e.preventDefault()
console.log "Reset pressed"
socr.controller.reset()
return
$(__inferBtnDOMSelector__).on "click", (e) ->
e.preventDefault()
if model.getSample(1) is false
view.handleResponse "<h4 class=\"alert-heading\">No Random samples to infer From!</h4>Please generate some random samples in Step 2. ", "error", "controller-content"
else
view.toggleControllerHandle "hide"
PubSub.publish "toggleLoadingSpinner" ,{action:'show'}
setTimeout socr.controller.setDotplot, 500
return
$(__inferenceVariableDOMSelector__).on "change", ->
if $(this).val() is "Mean" or $(this).val() is "Count"
$(__inferenceDatasetIndexDOMSelector__).attr "disabled", false
else
$(__inferenceDatasetIndexDOMSelector__).attr "disabled", true
return
$(__inferenceAnalysisDOMSelector__).on "change", ->
if socr.analysis[$(this).val()] isnt "undefined"
el = ""
$.each socr.analysis[$(this).val()]["variables"], (key, value) ->
el += "<option value=\"" + value + "\">" + value.replace("-", " ") + "</option>"
return
$(__inferenceVariableDOMSelector__).html el
return
$(".update").on "click", ->
val = []
$.each $(".nValues"), (k, v) ->
val.push $(v).val()
return
socr.model.setN val
return
try
$(".controller-popups").popover html: true
catch e
console.log e.message
return
###
@method: step()
@description: It generates 1 random sample with animation effect showing the generation.
@dependencies: view.animate()
###
step: ->
$(__accordionDOMSelector__).accordion "activate", 1
#socr.view.toggleControllerHandle("hide");
view.disableButtons() #disabling buttons
try
model.generateSample() #generate one sample
$(".removable").remove() #remove the previously generated canvas during animation
PubSub.publish "randomSampleGenerationComplete", {'sampleCount':model.getRSampleCount()}
catch e
console.log e
view.enableButtons()
return
#view.animate({
# stopCount:$('#nSize').val(),
# speed:$('#speed').val(),
# indexes:keys.indexes,
# datasetIndexes:keys.datasetIndexes
# });
#show sample generation animation
###
@method: run()
@description:It generates X random sample with animation effect showing the generation.
###
run: ->
#this should go through model event.
model.set "stopCount" , $("#countSize").val() #save the stopcount provided by user
compute = ->
PubSub.publish "randomSampleGenerationStarted"
# d = Date()
#console.log "start" + _runsElapsed + d
console.log "stop count: " + model.get "stopCount"
_generate()
_id = setInterval(_generate, MIN_SAMPLE_GENERATION_STEP_TIME)
return
#throw warning if datapoints cross threshold.
if model.aboveThreshold()
$("<div></div>").appendTo("body").html("<div><h6>Caution: Your dataset and sample count selection may consume too much memory causing the tab to become unresponsive. Do you wish to continue?</h6></div>").dialog
modal: true
title: "Warning!"
zIndex: 10000
autoOpen: true
width: "auto"
resizable: false
buttons:
Yes: ->
compute()
$(this).dialog "close" #close the confirmation window
return
No: ->
$(this).dialog "close"
return
close: (event, ui) ->
$(this).remove()
return
else
compute()
###
@method: stop()
@description:It resets the setInterval for _generate() ans halts the random sample generation immediately.
###
stop: ->
d = Date()
console.log "end" + _runsElapsed + d
PubSub.publish "randomSampleGenerationStopped", {}
clearInterval _id #stop the setinterval function
_runCount = 0
_runsElapsed = 0 #reset the runelapsed count
return
###
@method: reset()
@description:It resets the application by clearing the appModel and appView.
###
reset: ->
$("<div></div>").appendTo("body").html("<div><h6>Are you sure you want to reset everything? Data will be lost!</h6></div>").dialog
modal: true
title: "Reset Data?"
zIndex: 10000
autoOpen: true
width: "auto"
resizable: false
buttons:
Yes: ->
_this.stop()
model.reset()
socr.exp.current = {} #deleting the current experiment instance
view.toggleControllerHandle "hide"
socr.dataTable.simulationDriven.resetScreen()
$(this).dialog "close" #close the confirmation window
return
No: ->
$(this).dialog "close"
return
close: (event, ui) ->
$(this).remove()
return
return
###
@description : When user clicks "infer" button, setDotplot is triggered.
###
setDotplot: (precision)->
inferenceDatasetIndex = parseInt($(__inferenceDatasetIndexDOMSelector__).val())
inferencePrecision = $(__inferencePrecisionDOMSelector__).val() || 4
console.log "setdotplot started", "variable:" + $(__inferenceVariableDOMSelector__).val()
model.setInferenceSettings
analysis: $(__inferenceAnalysisDOMSelector__).val()
variable: $(__inferenceVariableDOMSelector__).val()
precision: inferencePrecision
index: inferenceDatasetIndex
return
###
@method: loadController()
@description: Single point of contact to load the controller tab and set the dataset
###
loadController: (setting) ->
result = undefined
return false if typeof setting isnt "object"
if setting.to is "dataDriven"
socr.controller.setCurrentMode setting.from if setting.from isnt "undefined"
PubSub.publish "Datadriven controller loaded"
#If experiment , then check for data
_name = ""
if setting.from is "Experiment" and not $.isEmptyObject(socr.exp.current) and socr.exp.current.getDataset() isnt ""
console.log "simulation drive has some data"
result = model.setDataset(
keys: socr.exp.current.getDatasetKeys()
values: socr.exp.current.getDatasetValues()
processed: true
)
else if setting.from is "spreadSheet"
_name = "Data Driven Experiment"
result = model.setDataset(setting.data)
else
return
if result is true
view.updateSimulationInfo _name
view.toggleControllerHandle "show"
model.setN()
view.createControllerView()
return
setCurrentMode: (mode) ->
_currentMode = mode unless mode is `undefined`
true
getCurrentMode: ->
_currentMode
#return