Total Complexity | 114 |
Complexity/F | 1.48 |
Lines of Code | 906 |
Function Count | 77 |
Duplicated Lines | 906 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 resources/assets/js/app.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 | // ============================================================================= |
||
7 | (function($) { |
||
8 | |||
9 | // Add isValid() |
||
10 | |||
11 | $.fn.isValid = function(){ |
||
12 | return this[0].checkValidity() |
||
13 | } |
||
14 | |||
15 | // Root |
||
16 | |||
17 | var $root = $('html, body'); |
||
18 | |||
19 | // Add has attribute Function |
||
20 | $.fn.hasAttr = function(name) { |
||
21 | var attr = $(this).attr(name); |
||
22 | // For some browsers, `attr` is undefined; for others, |
||
23 | // `attr` is false. Check for both. |
||
24 | return (typeof attr !== typeof undefined && attr !== false); |
||
25 | |||
26 | }; |
||
27 | |||
28 | // User Agent Data Attributes ============================================== |
||
29 | |||
30 | var ua = navigator.userAgent; |
||
31 | ua = ua.toString(); |
||
32 | console.log("hello"); |
||
33 | $('body').attr('id', ua); |
||
34 | |||
35 | $(document).ready(function() { |
||
36 | |||
37 | // Accordion Handlers ================================================== |
||
38 | |||
39 | function accordionTrigger(trigger) { |
||
40 | if ($(trigger).parent(".accordion").hasClass("active")) { |
||
41 | $(trigger).attr("aria-expanded", "false"); |
||
42 | $(trigger).parent(".accordion").removeClass("active"); |
||
43 | $(trigger).parent(".accordion").find(".accordion-content").attr("aria-hidden", "true"); |
||
44 | } |
||
45 | else { |
||
46 | $(trigger).attr("aria-expanded", "true"); |
||
47 | $(trigger).parent(".accordion").addClass("active"); |
||
48 | $(trigger).parent(".accordion").find(".accordion-content").attr("aria-hidden", "false"); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | $(document).on("click", ".accordion-trigger", function(e){ |
||
53 | |||
54 | accordionTrigger(this); |
||
55 | |||
56 | }); |
||
57 | |||
58 | $(document).on("keyup", ".accordion-trigger", function(e){ |
||
59 | |||
60 | if(e.which == 13) { |
||
61 | accordionTrigger(this); |
||
62 | } |
||
63 | |||
64 | }); |
||
65 | |||
66 | // Modal Handlers ====================================================== |
||
67 | |||
68 | function openModal(trigger) { |
||
69 | |||
70 | var modalID = $(trigger).attr("data-modal-id"); |
||
71 | var modal = $(".modal[data-modal-id="+modalID+"]"); |
||
72 | var modalObject = $(trigger).parents(".modal-target-object"); |
||
73 | $(".modal-overlay").addClass("active"); |
||
74 | modal.addClass("active"); |
||
75 | $("body").css("overflow", "hidden"); |
||
76 | |||
77 | // Tab Items |
||
78 | |||
79 | var focusableItems = modal.find(":focusable"); |
||
80 | |||
81 | var firstInput = focusableItems.first(); |
||
82 | var lastInput = focusableItems.last(); |
||
83 | |||
84 | if (modal.find("form").length == 0) { |
||
85 | lastInput.focus(); |
||
86 | } |
||
87 | else { |
||
88 | firstInput.focus(); |
||
89 | } |
||
90 | |||
91 | modalTabHandler(firstInput, lastInput); |
||
92 | modalDeleteTrigger(trigger, modal, modalObject); |
||
93 | escapeModalHandler(); |
||
94 | |||
95 | } |
||
96 | |||
97 | $(document).on("click", ".modal-trigger", function(e){ |
||
98 | |||
99 | openModal(this); |
||
100 | |||
101 | }); |
||
102 | |||
103 | $(document).on("keyup", ".modal-trigger", function(e){ |
||
104 | |||
105 | if(e.which == 13) { |
||
106 | openModal(this); |
||
107 | } |
||
108 | |||
109 | }); |
||
110 | |||
111 | |||
112 | function closeModal(trigger) { |
||
113 | |||
114 | $(".modal-overlay").removeClass("active"); |
||
115 | $(".modal").removeClass("active"); |
||
116 | $("body").css("overflow", "visible"); |
||
117 | |||
118 | } |
||
119 | |||
120 | $(document).on("click", ".modal-cancel-trigger", function(e){ |
||
121 | |||
122 | closeModal(this); |
||
123 | |||
124 | }); |
||
125 | |||
126 | $(document).on("keyup", ".modal-cancel-trigger", function(e){ |
||
127 | |||
128 | if(e.which == 13) { |
||
129 | closeModal(this); |
||
130 | } |
||
131 | |||
132 | }); |
||
133 | |||
134 | // Delete Trigger ================================================== |
||
135 | |||
136 | function modalDeleteTrigger(trigger, modal, object) { |
||
137 | |||
138 | $(document).on("click", ".modal-delete-trigger", function(e){ |
||
139 | |||
140 | //TODO: when items are saved with ajax too, the check |
||
141 | // will become more complicated than checking for a |
||
142 | // delete url |
||
143 | |||
144 | //If object has been saved to server, make an ajax delete |
||
145 | // call to the item url, and only close the modal when it |
||
146 | // succeeds |
||
147 | if ( $(object).attr('data-item-saved') ) { |
||
148 | var itemId = $(object).attr('data-item-id'); |
||
149 | var deleteUrl = $(object).attr('data-item-url').replace(':id', itemId); |
||
150 | $(modal).addClass('working'); |
||
151 | |||
152 | axios.delete(deleteUrl) |
||
153 | .then(function(response) { |
||
154 | closeModal(trigger); |
||
155 | $(object).remove(); |
||
156 | $(modal).removeClass('working'); |
||
157 | }).catch(function(error) { |
||
158 | $(modal).removeClass('working'); |
||
159 | }); |
||
160 | //TODO: catch and present errors |
||
161 | |||
162 | } else { |
||
163 | //If no deletion url provided, simply delete the |
||
164 | // object and close the modal. |
||
165 | |||
166 | closeModal(trigger); |
||
167 | $(object).remove(); |
||
168 | } |
||
169 | }); |
||
170 | |||
171 | } |
||
172 | |||
173 | // Tab Handler ===================================================== |
||
174 | |||
175 | function modalTabHandler(first, last) { |
||
176 | |||
177 | $(document).on("keydown", function(e){ |
||
178 | |||
179 | var keyCode = e.keyCode || e.which; |
||
180 | |||
181 | if (keyCode == 9 && !e.shiftKey) { |
||
182 | |||
183 | if ($(last).is(":focus")) { |
||
184 | e.preventDefault(); |
||
185 | $(first).focus(); |
||
186 | } |
||
187 | |||
188 | } |
||
189 | else if (keyCode == 9 && e.shiftKey) { |
||
190 | |||
191 | if($(first).is(":focus")) { |
||
192 | e.preventDefault(); |
||
193 | $(last).focus(); |
||
194 | } |
||
195 | |||
196 | } |
||
197 | |||
198 | }); |
||
199 | |||
200 | } |
||
201 | |||
202 | // Escape Handler ================================================== |
||
203 | |||
204 | function escapeModalHandler() { |
||
205 | |||
206 | $(document).on("keyup", function(e){ |
||
207 | |||
208 | if((e.key==='Escape'||e.key==='Esc'||e.keyCode===27)){ |
||
209 | |||
210 | $(".modal-overlay").removeClass("active"); |
||
211 | $(".modal").removeClass("active"); |
||
212 | $("body").css("overflow", "visible"); |
||
213 | |||
214 | // FF and compatible |
||
215 | if (e.stopPropagation) { |
||
216 | e.stopPropagation(); |
||
217 | e.preventDefault(); |
||
218 | } |
||
219 | |||
220 | } |
||
221 | |||
222 | }); |
||
223 | |||
224 | } |
||
225 | |||
226 | // Form Handlers ======================================================= |
||
227 | |||
228 | // Required Fields |
||
229 | |||
230 | function requiredFields() { |
||
231 | $("input:required, textarea:required").each(function(e) { |
||
232 | $(this).parent().addClass("required"); |
||
233 | $(this).parent().find("label").append("<span class='form__required'><i class='fa fa-asterisk' aria-label='Asterisk'></i></span>"); |
||
234 | }); |
||
235 | } |
||
236 | |||
237 | requiredFields(); |
||
238 | |||
239 | // Label Handers =================================================== |
||
240 | |||
241 | function labelHandlers() { |
||
242 | |||
243 | $("[class*='form__input-wrapper'] input, [class*='form__input-wrapper'] textarea").focusin(function(e) { |
||
244 | $(this).parent().addClass("active"); |
||
245 | }); |
||
246 | |||
247 | $("[class*='form__input-wrapper'] input, [class*='form__input-wrapper'] textarea").focusout(function(e) { |
||
248 | |||
249 | // Check for existing value. |
||
250 | |||
251 | if ($(this).val() == "") { |
||
252 | $(this).parent().removeClass("active"); |
||
253 | } |
||
254 | |||
255 | // Check Validity |
||
256 | |||
257 | if ($(this).isValid() == true) { |
||
258 | |||
259 | if ($(this).val() == "" || $(this).attr("type") == "password") { |
||
260 | $(this).parent().removeClass("valid"); |
||
261 | $(this).parent().removeClass("invalid"); |
||
262 | } |
||
263 | else { |
||
264 | $(this).parent().addClass("valid"); |
||
265 | $(this).parent().removeClass("invalid"); |
||
266 | } |
||
267 | |||
268 | } |
||
269 | else { |
||
270 | |||
271 | if ($(this).attr("type") == "password") { |
||
272 | return false; |
||
273 | } |
||
274 | else { |
||
275 | $(this).parent().addClass("invalid"); |
||
276 | $(this).parent().removeClass("valid"); |
||
277 | } |
||
278 | |||
279 | } |
||
280 | |||
281 | }); |
||
282 | |||
283 | } |
||
284 | |||
285 | labelHandlers(); |
||
286 | |||
287 | // Individualizing repeater name and id attributes====================== |
||
288 | |||
289 | //Individualize template attributes |
||
290 | function appendToAttributes(parent, attribute, suffix, conditions = null) { |
||
291 | var selector = "*[" + attribute + "]"; |
||
292 | |||
293 | //If conditions is set, only modify attributes that also |
||
294 | //satisfy that selector |
||
295 | if (conditions) { |
||
296 | selector = conditions + selector; |
||
297 | } |
||
298 | |||
299 | parent.find(selector).each(function() { |
||
300 | $(this).attr(attribute, $(this).attr(attribute) + suffix); |
||
301 | }); |
||
302 | } |
||
303 | |||
304 | //Individualize template attributes |
||
305 | function replaceInAttributes(parent, attribute, oldString, newString, conditions = null) { |
||
306 | var selector = "*[" + attribute + "]"; |
||
307 | |||
308 | //If conditions is set, only modify attributes that also |
||
309 | //satisfy that selector |
||
310 | if (conditions) { |
||
311 | selector = conditions + selector; |
||
312 | } |
||
313 | |||
314 | parent.find(selector).each(function() { |
||
315 | //replaces only the first instance of a match in a string |
||
316 | $(this).attr(attribute, $(this).attr(attribute).replace(oldString, newString)); |
||
317 | }); |
||
318 | } |
||
319 | |||
320 | //Return the next unused idAttr value |
||
321 | function getNextItemId(parent, idAttr = "data-item-id") { |
||
322 | var maxId = 0; |
||
323 | parent.find("*[" + idAttr + "]").each(function() { |
||
324 | var id = parseInt( $(this).attr(idAttr) ); |
||
325 | if (id > maxId) { |
||
326 | maxId = id; |
||
327 | } |
||
328 | }); |
||
329 | return maxId + 1; |
||
330 | } |
||
331 | |||
332 | //The all in one function to set proper ids and form names |
||
333 | function individualizeFormIdsAndNames(template, wrapper) { |
||
334 | // Get New ID |
||
335 | var newId = getNextItemId(wrapper); |
||
336 | |||
337 | //Set date-item-id, used to track which newId's are taken |
||
338 | template.attr('data-item-id', newId); |
||
339 | |||
340 | //Differentiate real forms from templates |
||
341 | |||
342 | // filter, if we only want to affect certain results |
||
343 | var filter = ''; |
||
344 | |||
345 | replaceInAttributes(template, 'id', ':template', 'new', filter); |
||
346 | replaceInAttributes(template, 'for', ':template', 'new', filter); |
||
347 | replaceInAttributes(template, 'name', ':template', 'new', filter); |
||
348 | replaceInAttributes(template, 'submit', ':template', 'new', filter); |
||
349 | replaceInAttributes(template, 'value', ':template', 'new', filter+'[name=submit]'); |
||
350 | |||
351 | replaceInAttributes(template, 'id', ':id', newId, filter); |
||
352 | replaceInAttributes(template, 'for', ':id', newId, filter); |
||
353 | replaceInAttributes(template, 'name', ':id', newId, filter); |
||
354 | replaceInAttributes(template, 'submit', ':id', newId, filter); |
||
355 | replaceInAttributes(template, 'value', ':id', newId, filter+'[name=submit]'); |
||
356 | } |
||
357 | |||
358 | // Profile List Handlers =============================================== |
||
359 | |||
360 | // Add Profile Element |
||
361 | function addProfileElement(trigger) { |
||
362 | |||
363 | // Get Parent |
||
364 | var parent = $(trigger).parents(".profile-list"); |
||
365 | |||
366 | // Get List Wrapper |
||
367 | var wrapper = parent.find(".profile-element-list"); |
||
368 | |||
369 | // Set Null to Hidden |
||
370 | parent.find(".profile-null").removeClass("active"); |
||
371 | |||
372 | // Get Template |
||
373 | var template = parent.find(".profile-element.template").clone(); |
||
374 | |||
375 | // Remove Template Class |
||
376 | template.removeClass("template"); |
||
377 | |||
378 | //Set ids and form names to be unique |
||
379 | individualizeFormIdsAndNames(template, wrapper); |
||
380 | |||
381 | // Prepend Clone to the Wrapper |
||
382 | wrapper.prepend(template); |
||
383 | |||
384 | // Reactivate Required Fields |
||
385 | requiredFields(); |
||
386 | |||
387 | // Reactivate Labels |
||
388 | labelHandlers(); |
||
389 | |||
390 | // Reactivate Nested Relatives |
||
391 | loadProfileRelatives(); |
||
392 | |||
393 | } |
||
394 | |||
395 | // Click Trigger |
||
396 | $(".profile-list__add-element-trigger").on("click", function(e) { |
||
397 | |||
398 | // Prevent Default Functions |
||
399 | e.preventDefault(); |
||
400 | |||
401 | // Add Profile Elements |
||
402 | addProfileElement(this); |
||
403 | |||
404 | }); |
||
405 | |||
406 | // Enter Key Trigger |
||
407 | $(".profile-list__add-element-trigger").on("keyup", function(e) { |
||
408 | |||
409 | if(e.which == 13) { |
||
410 | |||
411 | // Prevent Default Functions |
||
412 | e.preventDefault(); |
||
413 | |||
414 | // Add Profile Elements |
||
415 | addProfileElement(this); |
||
416 | |||
417 | } |
||
418 | |||
419 | }); |
||
420 | |||
421 | // Remove Profile Element |
||
422 | |||
423 | // Add Profile Relative |
||
424 | function addProfileRelative(trigger) { |
||
425 | |||
426 | // Get Parent |
||
427 | var parent = $(trigger).parents(".profile-relative-list"); |
||
428 | |||
429 | // Get List Wrapper |
||
430 | var wrapper = parent.find(".profile-relative-list__wrapper"); |
||
431 | |||
432 | // Set Null to Hidden |
||
433 | // parent.find(".profile-null").removeClass("active"); |
||
434 | |||
435 | // Get Template |
||
436 | var template = parent.find(".profile-relative.template").clone(); |
||
437 | |||
438 | // Remove Template Class |
||
439 | template.removeClass("template"); |
||
440 | |||
441 | //Set ids and form names to be unique |
||
442 | individualizeFormIdsAndNames(template, wrapper); |
||
443 | |||
444 | // Append Clone to the Wrapper |
||
445 | wrapper.append(template); |
||
446 | |||
447 | // Reactivate Required Fields |
||
448 | requiredFields(); |
||
449 | |||
450 | // Reactivate Labels |
||
451 | labelHandlers(); |
||
452 | |||
453 | // Reactivate Nested Relatives |
||
454 | loadProfileRelativeDeletion(); |
||
455 | |||
456 | } |
||
457 | |||
458 | // Load Function |
||
459 | function loadProfileRelatives() { |
||
460 | |||
461 | // Click Trigger |
||
462 | $(".profile-relative__add-trigger").off("click"); |
||
463 | |||
464 | $(".profile-relative__add-trigger").on("click", function(e) { |
||
465 | |||
466 | // Prevent Default Functions |
||
467 | e.preventDefault(); |
||
468 | |||
469 | // Add Profile Relative |
||
470 | addProfileRelative(this); |
||
471 | |||
472 | }); |
||
473 | |||
474 | // Enter Key Trigger |
||
475 | $(".profile-relative__add-trigger").off("keyup"); |
||
476 | |||
477 | $(".profile-relative__add-trigger").on("keyup", function(e) { |
||
478 | |||
479 | if(e.which == 13) { |
||
480 | |||
481 | // Prevent Default Functions |
||
482 | e.preventDefault(); |
||
483 | |||
484 | // Add Profile Relative |
||
485 | addProfileRelative(this); |
||
486 | |||
487 | } |
||
488 | |||
489 | }); |
||
490 | |||
491 | } |
||
492 | |||
493 | loadProfileRelatives(); |
||
494 | |||
495 | // Remove Profile Relative |
||
496 | function deleteProfileRelative(trigger) { |
||
497 | |||
498 | $(trigger).parents(".profile-relative").remove(); |
||
499 | |||
500 | } |
||
501 | |||
502 | // Load Function |
||
503 | function loadProfileRelativeDeletion() { |
||
504 | |||
505 | // Click Trigger |
||
506 | $(".profile-relative__remove-trigger").on("click", function(e) { |
||
507 | |||
508 | // Prevent Default Functions |
||
509 | e.preventDefault(); |
||
510 | |||
511 | // Delete Profile Relative |
||
512 | deleteProfileRelative(this); |
||
513 | |||
514 | }); |
||
515 | |||
516 | // Enter Key Trigger |
||
517 | $(".profile-relative__remove-trigger").on("keyup", function(e) { |
||
518 | |||
519 | if(e.which == 13) { |
||
520 | |||
521 | // Prevent Default Functions |
||
522 | e.preventDefault(); |
||
523 | |||
524 | // Delete Profile Relative |
||
525 | deleteProfileRelative(this); |
||
526 | |||
527 | } |
||
528 | |||
529 | }); |
||
530 | |||
531 | } |
||
532 | |||
533 | loadProfileRelativeDeletion(); |
||
534 | |||
535 | // Experience Handlers ================================================= |
||
536 | |||
537 | // Degrees |
||
538 | |||
539 | function addDegree(trigger) { |
||
540 | |||
541 | // Get Wrapper |
||
542 | var wrapper = $(".application-post__experience-wrapper"); |
||
543 | |||
544 | // Get Template |
||
545 | var template = $(".application-post__accordion--degree.template").clone(); |
||
546 | |||
547 | // Remove Template Class |
||
548 | template.removeClass("template"); |
||
549 | |||
550 | //Set ids and form names to be unique |
||
551 | individualizeFormIdsAndNames(template, wrapper); |
||
552 | |||
553 | // Append Clone to the Wrapper |
||
554 | wrapper.append(template); |
||
555 | |||
556 | requiredFields(); |
||
557 | labelHandlers(); |
||
558 | |||
559 | } |
||
560 | |||
561 | $("#addDegreeButton").on("click", function(e) { |
||
562 | |||
563 | e.preventDefault(); |
||
564 | |||
565 | addDegree(this); |
||
566 | |||
567 | }); |
||
568 | |||
569 | $("#addDegreeButton").on("keyup", function(e) { |
||
570 | |||
571 | if(e.which == 13) { |
||
572 | e.preventDefault(); |
||
573 | addDegree(this); |
||
574 | } |
||
575 | |||
576 | }); |
||
577 | |||
578 | // Courses |
||
579 | |||
580 | function addCourse(trigger) { |
||
581 | |||
582 | // Get Wrapper |
||
583 | var wrapper = $(".application-post__experience-wrapper"); |
||
584 | |||
585 | // Get Template |
||
586 | var template = $(".application-post__accordion--course.template").clone(); |
||
587 | |||
588 | // Remove Template Class |
||
589 | template.removeClass("template"); |
||
590 | |||
591 | //Set ids and form names to be unique |
||
592 | individualizeFormIdsAndNames(template, wrapper); |
||
593 | |||
594 | // Append Clone to the Wrapper |
||
595 | wrapper.append(template); |
||
596 | |||
597 | requiredFields(); |
||
598 | labelHandlers(); |
||
599 | |||
600 | } |
||
601 | |||
602 | $("#addCourseButton").on("click", function(e) { |
||
603 | |||
604 | e.preventDefault(); |
||
605 | |||
606 | addCourse(this); |
||
607 | |||
608 | }); |
||
609 | |||
610 | $("#addCourseButton").on("keyup", function(e) { |
||
611 | |||
612 | if(e.which == 13) { |
||
613 | e.preventDefault(); |
||
614 | addCourse(this); |
||
615 | } |
||
616 | |||
617 | }); |
||
618 | |||
619 | // Work |
||
620 | |||
621 | function addWork(trigger) { |
||
622 | |||
623 | // Get Wrapper |
||
624 | var wrapper = $(".application-post__experience-wrapper"); |
||
625 | |||
626 | // Get Template |
||
627 | var template = $(".application-post__accordion--work.template").clone(); |
||
628 | |||
629 | // Remove Template Class |
||
630 | template.removeClass("template"); |
||
631 | |||
632 | //Set ids and form names to be unique |
||
633 | individualizeFormIdsAndNames(template, wrapper); |
||
634 | |||
635 | // Append Clone to the Wrapper |
||
636 | wrapper.append(template); |
||
637 | |||
638 | requiredFields(); |
||
639 | labelHandlers(); |
||
640 | |||
641 | } |
||
642 | |||
643 | $("#addWorkButton").on("click", function(e) { |
||
644 | |||
645 | e.preventDefault(); |
||
646 | |||
647 | addWork(this); |
||
648 | |||
649 | }); |
||
650 | |||
651 | $("#addWorkButton").on("keyup", function(e) { |
||
652 | |||
653 | if(e.which == 13) { |
||
654 | e.preventDefault(); |
||
655 | addWork(this); |
||
656 | } |
||
657 | |||
658 | }); |
||
659 | |||
660 | // Create Job Handlers ================================================= |
||
661 | |||
662 | // Tasks |
||
663 | |||
664 | function addTask(trigger) { |
||
665 | |||
666 | // Get Wrapper |
||
667 | var wrapper = $(".manager-jobs__create-task-wrapper"); |
||
668 | |||
669 | // Get Template |
||
670 | var template = $(".manager-jobs__create-task.template").clone(); |
||
671 | |||
672 | console.log(wrapper.find(".manager-jobs__create-task")); |
||
673 | |||
674 | // Get New ID |
||
675 | if (wrapper.find(".manager-jobs__create-task").length == 0) { |
||
676 | var newID = parseInt(template.attr("data-task-id")) + 1; |
||
677 | } |
||
678 | else { |
||
679 | var newID = parseInt(wrapper.find("[class*='manager-jobs__create-task']").last().attr("data-task-id")) + 1; |
||
680 | } |
||
681 | |||
682 | // Remove Template Class |
||
683 | template.removeClass("template"); |
||
684 | |||
685 | //TODO: replace with call to individualizeFormIdsAndNames(template, wrapper); |
||
686 | //TODO: This requires changes to JobController@create, because the id would change places |
||
687 | |||
688 | // Assign the New ID |
||
689 | template.attr("data-task-id", newID); |
||
690 | |||
691 | // Add newID as suffix to all "id" and "for" attributes |
||
692 | template.find("*[id]").each(function() { $(this).attr("id", this.id + newID)}); |
||
693 | template.find("*[for]").each(function() { $(this).attr("for", $(this).attr("for") + newID)}); |
||
694 | |||
695 | // Replace :id with newID in all form names |
||
696 | template.find("*[name]").each(function() { $(this).attr('name', $(this).attr("name").replace(":id", newID))}); |
||
697 | |||
698 | // Task (English) |
||
699 | //template.find("[data-form-id*='task-english']").find("label").attr("for", "taskEN" + newID); |
||
700 | //template.find("[data-form-id*='task-english']").find("input").attr("id", "taskEN" + newID); |
||
701 | |||
702 | // Task (French) |
||
703 | //template.find("[data-form-id*='task-french']").find("label").attr("for", "taskFR" + newID); |
||
704 | //template.find("[data-form-id*='task-french']").find("input").attr("id", "taskFR" + newID); |
||
705 | |||
706 | // Append Clone to the Wrapper |
||
707 | wrapper.append(template); |
||
708 | |||
709 | requiredFields(); |
||
710 | labelHandlers(); |
||
711 | deleteTaskTrigger(); |
||
712 | |||
713 | } |
||
714 | |||
715 | $("#addTaskButton").on("click", function(e) { |
||
716 | |||
717 | e.preventDefault(); |
||
718 | |||
719 | addTask(this); |
||
720 | |||
721 | }); |
||
722 | |||
723 | $("#addTaskButton").on("keyup", function(e) { |
||
724 | |||
725 | if(e.which == 13) { |
||
726 | e.preventDefault(); |
||
727 | addTask(this); |
||
728 | } |
||
729 | |||
730 | }); |
||
731 | |||
732 | // Task Deletion |
||
733 | |||
734 | function deleteTask(trigger) { |
||
735 | |||
736 | $(trigger).parents(".manager-jobs__create-task").remove(); |
||
737 | |||
738 | } |
||
739 | |||
740 | function deleteTaskTrigger() { |
||
741 | |||
742 | $(".manager-jobs__delete-task-button").on("click", function(e) { |
||
743 | |||
744 | e.preventDefault(); |
||
745 | |||
746 | deleteTask(this); |
||
747 | |||
748 | }); |
||
749 | |||
750 | $(".manager-jobs__delete-task-button").on("keyup", function(e) { |
||
751 | |||
752 | if(e.which == 13) { |
||
753 | e.preventDefault(); |
||
754 | deleteTask(this); |
||
755 | } |
||
756 | |||
757 | }); |
||
758 | |||
759 | } |
||
760 | |||
761 | deleteTaskTrigger(); |
||
762 | |||
763 | // Skills |
||
764 | |||
765 | function addSkill(trigger) { |
||
766 | |||
767 | // Get Parent |
||
768 | var parent = $(trigger).parents(".manager-jobs__skill-wrapper"); |
||
769 | |||
770 | // Get Wrapper |
||
771 | var wrapper = parent.find(".manager-jobs__create-skill-wrapper"); |
||
772 | |||
773 | // Get Template |
||
774 | var template = parent.find(".manager-jobs__create-skill.template").clone(); |
||
775 | |||
776 | console.log(wrapper.find(".manager-jobs__create-skill")); |
||
777 | |||
778 | // Remove Template Class |
||
779 | template.removeClass("template"); |
||
780 | |||
781 | //Set ids and form names to be unique |
||
782 | individualizeFormIdsAndNames(template, wrapper); |
||
783 | |||
784 | // Append Clone to the Wrapper |
||
785 | wrapper.append(template); |
||
786 | |||
787 | requiredFields(); |
||
788 | labelHandlers(); |
||
789 | deleteSkillTrigger(); |
||
790 | |||
791 | } |
||
792 | |||
793 | $(".manager-jobs__add-skill-button").on("click", function(e) { |
||
794 | |||
795 | e.preventDefault(); |
||
796 | |||
797 | addSkill(this); |
||
798 | |||
799 | }); |
||
800 | |||
801 | $(".manager-jobs__add-skill-button").on("keyup", function(e) { |
||
802 | |||
803 | if(e.which == 13) { |
||
804 | e.preventDefault(); |
||
805 | addSkill(this); |
||
806 | } |
||
807 | |||
808 | }); |
||
809 | |||
810 | // Skill Deletion |
||
811 | |||
812 | function deleteSkill(trigger) { |
||
813 | |||
814 | $(trigger).parents(".manager-jobs__create-skill").remove(); |
||
815 | |||
816 | } |
||
817 | |||
818 | function deleteSkillTrigger() { |
||
819 | |||
820 | $(".manager-jobs__delete-skill-button").on("click", function(e) { |
||
821 | |||
822 | e.preventDefault(); |
||
823 | |||
824 | deleteSkill(this); |
||
825 | |||
826 | }); |
||
827 | |||
828 | $(".manager-jobs__delete-skill-button").on("keyup", function(e) { |
||
829 | |||
830 | if(e.which == 13) { |
||
831 | e.preventDefault(); |
||
832 | deleteSkill(this); |
||
833 | } |
||
834 | |||
835 | }); |
||
836 | |||
837 | } |
||
838 | |||
839 | deleteSkillTrigger(); |
||
840 | |||
841 | // Questions |
||
842 | |||
843 | function addQuestion(trigger) { |
||
844 | |||
845 | // Get Wrapper |
||
846 | var wrapper = $(".manager-jobs__create-question-wrapper"); |
||
847 | |||
848 | // Get Template |
||
849 | var template = $(".manager-jobs__create-question.template").clone(); |
||
850 | |||
851 | console.log(wrapper.find(".manager-jobs__create-question")); |
||
852 | |||
853 | // Get New ID |
||
854 | if (wrapper.find(".manager-jobs__create-question").length == 0) { |
||
855 | var newID = parseInt(template.attr("data-question-id")) + 1; |
||
856 | } |
||
857 | else { |
||
858 | var newID = parseInt(wrapper.find("[class*='manager-jobs__create-question']").last().attr("data-question-id")) + 1; |
||
859 | } |
||
860 | |||
861 | // Remove Template Class |
||
862 | template.removeClass("template"); |
||
863 | |||
864 | //TODO: replace with call to individualizeFormIdsAndNames(template, wrapper); |
||
865 | //TODO: This requires changes to JobController@create, because the id would change places |
||
866 | |||
867 | // Assign the New ID |
||
868 | template.attr("data-question-id", newID); |
||
869 | |||
870 | // Add newID as suffix to all "id" and "for" attributes |
||
871 | template.find("*[id]").each(function() { $(this).attr("id", this.id + newID)}); |
||
872 | template.find("*[for]").each(function() { $(this).attr("for", $(this).attr("for") + newID)}); |
||
873 | |||
874 | // Replace :id with newID in all form names |
||
875 | template.find("*[name]").each(function() { $(this).attr('name', $(this).attr("name").replace(":id", newID))}); |
||
876 | |||
877 | // Edit Form IDs |
||
878 | // |
||
879 | // // Queestion (English) |
||
880 | // template.find("[data-form-id*='question-english']").find("label").attr("for", "questionEN" + newID); |
||
881 | // template.find("[data-form-id*='question-english']").find("input").attr("id", "questionEN" + newID); |
||
882 | // |
||
883 | // // Queestion (French) |
||
884 | // template.find("[data-form-id*='question-french']").find("label").attr("for", "questionFR" + newID); |
||
885 | // template.find("[data-form-id*='question-french']").find("input").attr("id", "questionFR" + newID); |
||
886 | |||
887 | // Append Clone to the Wrapper |
||
888 | wrapper.append(template); |
||
889 | |||
890 | requiredFields(); |
||
891 | labelHandlers(); |
||
892 | deleteQuestionTrigger(); |
||
893 | |||
894 | } |
||
895 | |||
896 | $("#addQuestionButton").on("click", function(e) { |
||
897 | |||
898 | e.preventDefault(); |
||
899 | |||
900 | addQuestion(this); |
||
901 | |||
902 | }); |
||
903 | |||
904 | $("#addQuestionButton").on("keyup", function(e) { |
||
905 | |||
906 | if(e.which == 13) { |
||
907 | e.preventDefault(); |
||
908 | addQuestion(this); |
||
909 | } |
||
910 | |||
911 | }); |
||
912 | |||
913 | // Question Deletion |
||
947 |