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