Total Complexity | 84 |
Complexity/F | 2.27 |
Lines of Code | 457 |
Function Count | 37 |
Duplicated Lines | 48 |
Ratio | 10.5 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like main.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | (() => { |
||
2 | var W = 2; |
||
3 | var N = 2; |
||
4 | var state = []; |
||
|
|||
5 | var sort_show_flag = false; |
||
6 | var num_of_state = 4; |
||
7 | const Operator_name = [ |
||
8 | ["I", "Measure", "X", "Z", "H", "S", "Sd", "T", "Td", "Rx", "Ry", "Rz"], |
||
9 | ["CI", "SCI", "CX", "CZ", "CH", "CS", "CSd", "CT", "CTd", "CRx", "CRy", "CRz"], |
||
10 | ["I", "Fs", "Fe"] |
||
11 | ]; |
||
12 | |||
13 | let Sim = new Simulate(); |
||
14 | |||
15 | const X_SHIFT = 0; |
||
16 | const Y_SHIFT = 50; |
||
17 | |||
18 | function make_button(name, left, top, id_name){ |
||
19 | if(name==="sort_show" || name==="simulate"){ |
||
20 | let button = document.createElement("button"); |
||
21 | button.setAttribute("id", id_name); |
||
22 | button.setAttribute("class", name); |
||
23 | button.setAttribute("type", "button"); |
||
24 | button.setAttribute("style", "cursor:pointer"); |
||
25 | if(name==="sort_show"){ |
||
26 | button.innerHTML = "SORT"; |
||
27 | }else if(name==="simulate"){ |
||
28 | button.innerHTML = "Simulate"; |
||
29 | } |
||
30 | document.body.appendChild(button); |
||
31 | } |
||
32 | console.log(name); |
||
33 | let button = document.getElementById(id_name); |
||
34 | button.style.left = left + X_SHIFT + "px"; |
||
35 | button.style.top = top + Y_SHIFT + "px"; |
||
36 | } |
||
37 | |||
38 | function make_zero(y){ |
||
39 | var canvas = document.createElement("canvas"); |
||
40 | canvas.setAttribute("class", "zero"); |
||
41 | canvas.setAttribute("id", "z"+y); |
||
42 | canvas.style.position = "absolute"; |
||
43 | canvas.style.left = X_SHIFT + "px"; |
||
44 | canvas.style.top = 100*(y+1) + Y_SHIFT + "px"; |
||
45 | var width = canvas.width; |
||
46 | var height = canvas.height; |
||
47 | var ctx = canvas.getContext('2d'); |
||
48 | var img = new Image(); |
||
49 | img.src = "./Figures/Zero.png" |
||
50 | img.onload = function(){ |
||
51 | ctx.drawImage(img, 0, 0, width, height); |
||
52 | } |
||
53 | document.body.appendChild(canvas); |
||
54 | } |
||
55 | |||
56 | function delete_zero(y){ |
||
57 | var canvas = document.getElementById("z"+y); |
||
58 | canvas.remove(); |
||
59 | } |
||
60 | |||
61 | function draw_operator_text(canvas){ |
||
62 | var width = canvas.width; |
||
63 | var height = canvas.height; |
||
64 | var ctx = canvas.getContext('2d'); |
||
65 | var img = new Image(); |
||
66 | var id = canvas.object.Operator_id; |
||
67 | var flag = canvas.object.line_flag; |
||
68 | console.log(flag, id); |
||
69 | img.src = "./Figures/" + Operator_name[flag][id] + ".png"; |
||
70 | ctx.clearRect(0, 0, width, height); |
||
71 | img.onload = function(){ |
||
72 | ctx.drawImage(img, 0, 0, width, height); |
||
73 | } |
||
74 | }; |
||
75 | |||
76 | function make_operator(x, y){ |
||
77 | var canvas = document.createElement("canvas"); |
||
78 | canvas.setAttribute("class", "operator"); |
||
79 | canvas.setAttribute("id", "c"+x+"-"+y); |
||
80 | canvas.style.position = "absolute"; |
||
81 | canvas.style.left = 100*(x+1) + X_SHIFT + "px"; |
||
82 | canvas.style.top = 100*(y+1) + Y_SHIFT + "px"; |
||
83 | document.body.appendChild(canvas); |
||
84 | |||
85 | canvas.object = new Operator_Cell(x,y); |
||
86 | |||
87 | draw_operator_text(canvas); |
||
88 | } |
||
89 | |||
90 | function delete_operator(x, y){ |
||
91 | console.log("delete x:"+x+",y:"+y); |
||
92 | var canvas = document.getElementById("c"+x+"-"+y); |
||
93 | canvas.remove(); |
||
94 | delete_input_theta(x,y); |
||
95 | } |
||
96 | |||
97 | function check_switch(){ |
||
98 | let checkbox = document.getElementById("controll"); |
||
99 | checkbox.checked = false; |
||
100 | checkbox = document.getElementById("for_check"); |
||
101 | checkbox.checked = false; |
||
102 | } |
||
103 | |||
104 | function delete_results(){ |
||
105 | let canvases = document.querySelectorAll(".result_left_top"); |
||
106 | for(let i=0; i<canvases.length; ++i) if(canvases[i]!=null) canvases[i].remove(); |
||
107 | canvases = document.querySelectorAll(".result-bar"); |
||
108 | for(let i=0; i<canvases.length; ++i) if(canvases[i]!=null) canvases[i].remove(); |
||
109 | canvases = document.querySelectorAll(".result-index"); |
||
110 | for(let i=0; i<canvases.length; ++i) if(canvases[i]!=null) canvases[i].remove(); |
||
111 | canvases = document.querySelectorAll(".sort_show"); |
||
112 | for(let i=0; i<canvases.length; ++i) if(canvases[i]!=null) canvases[i].remove(); |
||
113 | |||
114 | /* |
||
115 | var canvas = document.getElementById("result_left_top"); |
||
116 | if(canvas!=null) canvas.remove(); |
||
117 | for(var i=0; i<num_of_state; i++){ |
||
118 | canvas = document.getElementById("result-bar-"+i); |
||
119 | if(canvas!=null) canvas.remove(); |
||
120 | canvas = document.getElementById("result-index-"+i); |
||
121 | if(canvas!=null) canvas.remove(); |
||
122 | } |
||
123 | var button = document.getElementById("sort_show"); |
||
124 | if(button!=null) button.remove(); |
||
125 | */ |
||
126 | } |
||
127 | |||
128 | function make_result_left_top(shift){ |
||
129 | var canvas = document.createElement("canvas"); |
||
130 | canvas.setAttribute("class", "result_left_top"); |
||
131 | canvas.setAttribute("id", "result_left_top"); |
||
132 | canvas.style.position = "absolute"; |
||
133 | canvas.style.left = 50 + X_SHIFT + "px"; |
||
134 | canvas.style.top = 100*(N+2) + Y_SHIFT + 300*shift + "px"; |
||
135 | document.body.appendChild(canvas); |
||
136 | } |
||
137 | |||
138 | function make_result_bar(index, pos, val, width, shift){ |
||
139 | var canvas = document.createElement("canvas"); |
||
140 | canvas.setAttribute("class", "result-bar"); |
||
141 | canvas.setAttribute("id", "result-bar-"+index); |
||
142 | canvas.style.position = "absolute"; |
||
143 | canvas.style.left = 100+pos*width+X_SHIFT+"px"; |
||
144 | canvas.style.top = 100*(N+2)+Y_SHIFT+300*shift+"px"; |
||
145 | canvas.width = width; |
||
146 | var height = canvas.height; |
||
147 | var ctx = canvas.getContext('2d'); |
||
148 | ctx.fillStyle = 'blue'; |
||
149 | ctx.fillRect(width/4, height*(1-val), width/2, height*val); |
||
150 | document.body.appendChild(canvas); |
||
151 | } |
||
152 | |||
153 | function index_converter_to_string(index, show_elements_length){ |
||
154 | var str=""; |
||
155 | var index_copy = index; |
||
156 | for(var i=0; i<show_elements_length; i++){ |
||
157 | str = String(index_copy%2) + str; |
||
158 | index_copy=Math.floor(index_copy/2); |
||
159 | } |
||
160 | return str; |
||
161 | } |
||
162 | |||
163 | function make_result_index(index, pos, name, width, shift){ |
||
164 | var canvas = document.createElement("canvas"); |
||
165 | canvas.setAttribute("class", "result-index"); |
||
166 | canvas.setAttribute("id", "result-index-"+index); |
||
167 | canvas.style.position = "absolute"; |
||
168 | canvas.style.left = 100+pos*width+X_SHIFT+"px"; |
||
169 | canvas.style.top = 100*(N+2)+200+Y_SHIFT+300*shift+"px"; |
||
170 | var ctx = canvas.getContext('2d'); |
||
171 | ctx.font = '64px serif'; |
||
172 | ctx.fillStyle = '#404040'; |
||
173 | ctx.textBaseline = 'middle'; |
||
174 | ctx.textAlign = 'center'; |
||
175 | ctx.fillText(name, canvas.width/2, canvas.height/2); |
||
176 | document.body.appendChild(canvas); |
||
177 | } |
||
178 | |||
179 | function make_result(index, pos, val, show_elements_length, shift){ |
||
180 | //var width = (N+2)*25; canvasの幅を動的に変更しようとしてできなかった |
||
181 | var width = 100; |
||
182 | make_result_bar(index, pos, val, width, shift); |
||
183 | var index_string = index_converter_to_string(index, show_elements_length); |
||
184 | make_result_index(index, pos, index_string, width, shift); |
||
185 | } |
||
186 | |||
187 | function show_results(measure_List){ |
||
188 | delete_results(); |
||
189 | |||
190 | for(let j=0; j<measure_List.length; ++j){ |
||
191 | let state_all = []; |
||
192 | make_result_left_top(j); |
||
193 | let List = measure_List[j]; |
||
194 | List.forEach((value, key)=>{ |
||
195 | state_all.push([key, value]); |
||
196 | }); |
||
197 | if(sort_show_flag){ |
||
198 | state_all.sort(function(a, b){return b[1]-a[1]}); |
||
199 | }else{ |
||
200 | state_all.sort(function(a, b){return a[0]-b[0]}); |
||
201 | } |
||
202 | let measure_indices_length = 0; |
||
203 | for(let i=0; (1<<i)<state_all.length; ++i){ |
||
204 | measure_indices_length++; |
||
205 | } |
||
206 | for(var i=0; i<state_all.length; i++){ |
||
207 | make_result(state_all[i][0], i, state_all[i][1], measure_indices_length, j); |
||
208 | } |
||
209 | make_button("sort_show", 20, 100*(N+4)+300*j+10, "sort_show"+j); |
||
210 | document.querySelector('#sort_show'+j).addEventListener('click', () => { |
||
211 | sort_show_flag = !sort_show_flag; |
||
212 | show_results(measure_List); |
||
213 | }) |
||
214 | } |
||
215 | } |
||
216 | |||
217 | function init(){ |
||
218 | make_button("wbutton1", 25, 25, "wbutton1"); |
||
219 | document.querySelector('.wbutton1').addEventListener('click', () => { |
||
220 | for(let y=0; y<N; ++y) make_operator(W,y); |
||
221 | W++; |
||
222 | }) |
||
223 | |||
224 | make_button("wbutton2", 125, 25, "wbutton2"); |
||
225 | document.querySelector('.wbutton2').addEventListener('click', () => { |
||
226 | W--; |
||
227 | if(W==0){ |
||
228 | W=1; |
||
229 | }else{ |
||
230 | for(let y=0; y<N; ++y){ |
||
231 | delete_operator(W,y); |
||
232 | delete_input_theta(W,y); |
||
233 | } |
||
234 | delete_input_fornum(W); |
||
235 | } |
||
236 | }) |
||
237 | |||
238 | make_button("nbutton1", 225, 25, "nbutton1"); |
||
239 | document.querySelector('.nbutton1').addEventListener('click', () => { |
||
240 | delete_results(); |
||
241 | make_zero(N); |
||
242 | for(let x=0; x<W; ++x) make_operator(x,N); |
||
243 | N++; |
||
244 | num_of_state *= 2; |
||
245 | }) |
||
246 | |||
247 | make_button("nbutton2", 325, 25, "nbutton2"); |
||
248 | document.querySelector('.nbutton2').addEventListener('click', () => { |
||
249 | delete_results(); |
||
250 | N--; |
||
251 | if(N==0){ |
||
252 | N=1; |
||
253 | }else{ |
||
254 | num_of_state/=2; |
||
255 | delete_zero(N); |
||
256 | for(let x=0; x<W; ++x){ |
||
257 | delete_operator(x,N); |
||
258 | delete_input_theta(x,N); |
||
259 | } |
||
260 | } |
||
261 | }) |
||
262 | |||
263 | make_button("simulate", 425, 25, "simulate"); |
||
264 | document.querySelector('.simulate').addEventListener('click', () => { |
||
265 | sort_show_flag = false; |
||
266 | let results = Sim.simulate(W,N,num_of_state); |
||
267 | state = results[0]; |
||
268 | measure_List = results[1]; |
||
269 | show_results(measure_List); |
||
270 | scroll(); |
||
271 | }) |
||
272 | check_switch(); |
||
273 | |||
274 | for(var y=0; y<N; y++){ |
||
275 | make_zero(y); |
||
276 | |||
277 | for(var x=0; x<W; x++){ |
||
278 | make_operator(x,y); |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | |||
283 | function make_input_theta(iIdx,iIdy,canvas){ |
||
284 | let input = document.createElement("input"); |
||
285 | input.setAttribute("type", "text"); |
||
286 | input.setAttribute("name", "input"); |
||
287 | input.setAttribute("class", "theta"); |
||
288 | input.setAttribute("id", "theta-"+iIdx+"-"+iIdy); |
||
289 | input.setAttribute("value", "0"); |
||
290 | |||
291 | input.style.width = 50 + "px"; |
||
292 | input.style.height = 10 + "px"; |
||
293 | input.style.position = "absolute"; |
||
294 | input.style.left = 100*(iIdx+1) + X_SHIFT + 25 + "px"; |
||
295 | input.style.top = 100*(iIdy+1) + Y_SHIFT + 80 + "px"; |
||
296 | |||
297 | input.addEventListener('keyup', () => { |
||
298 | canvas.object.theta = input.value; |
||
299 | }) |
||
300 | |||
301 | document.body.appendChild(input); |
||
302 | } |
||
303 | |||
304 | function delete_input_theta(iIdx,iIdy){ |
||
305 | let canvas = document.getElementById("theta-"+iIdx+"-"+iIdy); |
||
306 | if(canvas!=null) canvas.remove(); |
||
307 | } |
||
308 | |||
309 | function make_input_fornum(iIdx){ |
||
310 | let input = document.createElement("input"); |
||
311 | input.setAttribute("type", "text"); |
||
312 | input.setAttribute("name", "input"); |
||
313 | input.setAttribute("class", "fornum"); |
||
314 | input.setAttribute("id", "fornum-"+iIdx); |
||
315 | input.setAttribute("value", "0"); |
||
316 | |||
317 | input.style.width = 50 + "px"; |
||
318 | input.style.height = 10 + "px"; |
||
319 | input.style.position = "absolute"; |
||
320 | input.style.left = 100*(iIdx+1) + X_SHIFT + 25 + "px"; |
||
321 | input.style.top = Y_SHIFT + 80 + "px"; |
||
322 | |||
323 | input.addEventListener('keyup', () => { |
||
324 | let ob = document.getElementById("c"+iIdx+"-0").object; |
||
325 | ob.for_num = input.value; |
||
326 | }) |
||
327 | |||
328 | document.body.appendChild(input); |
||
329 | } |
||
330 | |||
331 | function delete_input_fornum(iIdx){ |
||
332 | let canvas = document.getElementById("fornum-"+iIdx); |
||
333 | if(canvas!=null) canvas.remove(); |
||
334 | } |
||
335 | |||
336 | function Operator_Cell(iIdx,iIdy){ |
||
337 | var self = this; |
||
338 | this.mIdx = iIdx; |
||
339 | this.mIdy = iIdy; |
||
340 | this.Operator_id = 0; |
||
341 | this.line_flag = 0; |
||
342 | this.theta = 0.0; |
||
343 | this.for_num = 0; |
||
344 | this.for_count = 0; |
||
345 | if(iIdy>0){ |
||
346 | this.line_flag = document.getElementById("c"+iIdx+"-"+0).object.line_flag; |
||
347 | if(this.line_flag===2){ |
||
348 | this.Operator_id = document.getElementById("c"+iIdx+"-"+0).object.Operator_id; |
||
349 | } |
||
350 | } |
||
351 | // line_flag = 0 (nothing) 1 (controll) // 2 (for start) 3 (for end) |
||
352 | this.mCanvas = document.getElementById("c"+iIdx+"-"+iIdy); |
||
353 | |||
354 | this.Click = function(e){ |
||
355 | console.log(""+self.mIdx+"-"+self.mIdy+" is clicked "); |
||
356 | let controll_button = document.getElementById("controll"); |
||
357 | let for_button = document.getElementById("for_check"); |
||
358 | |||
359 | if(for_button.checked){ |
||
360 | if(self.line_flag===2){ |
||
361 | self.Operator_id += 1; |
||
362 | self.Operator_id %= Operator_name[self.line_flag].length; |
||
363 | for(let y=0; y<N; ++y){ |
||
364 | let another_canvas = document.getElementById("c"+self.mIdx+"-"+y); |
||
365 | another_canvas.object.Operator_id = self.Operator_id; |
||
366 | draw_operator_text(another_canvas); |
||
367 | } |
||
368 | if(self.Operator_id===2) make_input_fornum(self.mIdx); |
||
369 | else delete_input_fornum(self.mIdx); |
||
370 | }else{ |
||
371 | for(let y=0; y<N; ++y){ |
||
372 | let another_canvas = document.getElementById("c"+self.mIdx+"-"+y); |
||
373 | if(another_canvas.object.line_flag!==2){ |
||
374 | another_canvas.object.line_flag = 2; |
||
375 | another_canvas.object.Operator_id = 0; |
||
376 | } |
||
377 | delete_input_theta(self.mIdx,y); |
||
378 | draw_operator_text(another_canvas); |
||
379 | } |
||
380 | } |
||
381 | }else if(controll_button.checked){ |
||
382 | View Code Duplication | if(self.line_flag===1){ |
|
383 | self.Operator_id += 1; |
||
384 | self.Operator_id %= Operator_name[self.line_flag].length; |
||
385 | delete_input_theta(self.mIdx,self.mIdy); |
||
386 | if(self.Operator_id-1 >= 8 && self.Operator_id-1 <= 10){ |
||
387 | make_input_theta(self.mIdx,self.mIdy,this); |
||
388 | } |
||
389 | draw_operator_text(self.mCanvas); |
||
390 | }else{ |
||
391 | for(var y=0; y<N; y++){ |
||
392 | var another_canvas = document.getElementById("c"+self.mIdx+"-"+y); |
||
393 | if(another_canvas.object.line_flag!==1){ |
||
394 | if(another_canvas.object.line_flag===2){ |
||
395 | delete_input_fornum(self.mIdx); |
||
396 | another_canvas.object.Operator_id=0; |
||
397 | } |
||
398 | another_canvas.object.line_flag = 1; |
||
399 | if(another_canvas.object.Operator_id==1){ |
||
400 | another_canvas.object.Operator_id=0; |
||
401 | } |
||
402 | } |
||
403 | draw_operator_text(another_canvas); |
||
404 | } |
||
405 | } |
||
406 | }else{ |
||
407 | View Code Duplication | if(self.line_flag!==0){ |
|
408 | for(var y=0; y<N; y++){ |
||
409 | var another_canvas = document.getElementById("c"+self.mIdx+"-"+y); |
||
410 | if(another_canvas.object.line_flag!==0){ |
||
411 | if(another_canvas.object.line_flag===2){ |
||
412 | delete_input_fornum(self.mIdx); |
||
413 | another_canvas.object.Operator_id=0; |
||
414 | } |
||
415 | another_canvas.object.line_flag=0; |
||
416 | if(another_canvas.object.Operator_id==1){ |
||
417 | another_canvas.object.Operator_id=0; |
||
418 | } |
||
419 | } |
||
420 | draw_operator_text(another_canvas); |
||
421 | } |
||
422 | }else{ |
||
423 | self.Operator_id += 1; |
||
424 | self.Operator_id %= Operator_name[self.line_flag].length; |
||
425 | delete_input_theta(self.mIdx,self.mIdy); |
||
426 | if(self.Operator_id-1 >= 8 && self.Operator_id-1 <= 10){ |
||
427 | make_input_theta(self.mIdx,self.mIdy,this); |
||
428 | } |
||
429 | draw_operator_text(self.mCanvas); |
||
430 | } |
||
431 | } |
||
432 | } |
||
433 | |||
434 | this.mCanvas.onclick = this.Click; |
||
435 | }; |
||
436 | |||
437 | function scroll(){ |
||
438 | /* |
||
439 | var element = document.documentElement; |
||
440 | var Target = element.scrollHeight - element.clientHeight; |
||
441 | var Pos = window.pageYOffset; |
||
442 | var move = Target/20; |
||
443 | var interval = 1000/100; |
||
444 | window.scrollBy(0, move); |
||
445 | var rep = setTimeout(scroll, interval); |
||
446 | if(Pos === Target){ |
||
447 | clearTimeout(rep); |
||
448 | } |
||
449 | //window.scrollTo(0, Target); |
||
450 | */ |
||
451 | } |
||
452 | |||
453 | window.onload = function(){ |
||
454 | init(); |
||
455 | }; |
||
456 | |||
457 | })(); |
||
458 |