Completed
Push — master ( c38c9f...6d2c0d )
by Josh
06:54
created

EvidenceAPI.instantiateApplicationEvidencePanel   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
1
var EvidenceAPI = {};
2
3
EvidenceAPI.evidenceTriggerName = function (criteriaId, criteriaType) {
4
    return criteriaType + "-" + criteriaId;
5
};
6
7
EvidenceAPI.evidencePreviewTriggerName = function (criteriaId, criteriaType) {
8
    return "preview-" + criteriaType + "-" + criteriaId;
9
};
10
11
EvidenceAPI.menuItemId = function (criteriaId, criteriaType) {
12
    var triggerName = EvidenceAPI.evidenceTriggerName(criteriaId, criteriaType);
13
    return "applicationEvidenceMenuItem_" + triggerName;
14
};
15
16
EvidenceAPI.previewMenuItemId = function (criteriaId, criteriaType) {
17
    var triggerName = EvidenceAPI.evidencePreviewTriggerName(criteriaId, criteriaType);
18
    return "applicationPreviewEvidenceMenuItem_" + triggerName;
19
};
20
21
EvidenceAPI.instantiateApplicationEvidenceMenuItem = function (criteriaId, criteriaType, criteriaName) {
22
    var menuItem = document.getElementById("applicantEvidenceMenuItemTemplate").firstElementChild.cloneNode(true);
23
24
    menuItem.classList.remove("template");
25
26
    var triggerName = EvidenceAPI.evidenceTriggerName(criteriaId, criteriaType);
27
28
    var menuItemId = EvidenceAPI.menuItemId(criteriaId, criteriaType);
29
30
    menuItem.id = menuItemId;
31
32
    menuItem.setAttribute("onclick", "EvidenceAPI.scrollToThisSkill(event, this)");
33
34
    //Set data attributes
35
    menuItem.setAttribute("data-evidence-trigger", triggerName);
36
    menuItem.setAttribute("data-criteria-type", criteriaType);
37
    menuItem.setAttribute("data-criteria-id", criteriaId);
38
39
    var title = menuItem.querySelector(".applicant-evidence__desktop-item-title");
40
41
    title.innerHTML = criteriaName;
42
43
    return menuItem;
44
};
45
46
EvidenceAPI.instantiateApplicationPreviewEvidenceMenuItem = function (criteriaId, criteriaType, criteriaName) {
47
    var menuItem = document.getElementById("applicationPrevierwEvidenceMenuItemTemplate").firstElementChild.cloneNode(true);
48
49
    menuItem.classList.remove("template");
50
51
    var triggerName = EvidenceAPI.evidencePreviewTriggerName(criteriaId, criteriaType);
52
    var menuItemId = EvidenceAPI.previewMenuItemId(criteriaId, criteriaType);
53
    menuItem.id = menuItemId;
54
    menuItem.setAttribute("data-evidence-trigger", triggerName);
55
56
    //Set data attributes
57
    menuItem.setAttribute("data-criteria-type", criteriaType);
58
    menuItem.setAttribute("data-criteria-id", criteriaId);
59
60
    menuItem.innerHTML = criteriaName;
61
62
    return menuItem;
63
};
64
65
EvidenceAPI.instantiateApplicationEvidencePanel = function (criteriaId, criteriaType, criteriaName, criteriaDescription) {
66
    var evidencePanel = document.getElementById("applicantEvidencePanelTemplate").firstElementChild.cloneNode(true);
67
68
    evidencePanel.classList.remove("template");
69
70
    var triggerName = EvidenceAPI.evidenceTriggerName(criteriaId, criteriaType);
71
    var menuItemId = EvidenceAPI.menuItemId(criteriaId, criteriaType);
72
    evidencePanel.setAttribute("data-evidence-target", triggerName);
73
    evidencePanel.setAttribute("aria-labelledby", menuItemId);
74
75
    //SET DATA ATTRIBUTES
76
    evidencePanel.setAttribute("data-criteria-type", criteriaType);
77
    evidencePanel.setAttribute("data-criteria-id", criteriaId);
78
79
    //SET SKILL TITLE AND DESCRIPTION
80
    evidencePanel.querySelector(".applicant-evidence__skill-title").innerHTML = criteriaName;
81
    if (criteriaDescription) {
82
        evidencePanel.querySelector(".applicant-evidence__skill-description").innerHTML = criteriaDescription;
83
    }
84
85
    //MODIFY IDs FOR UNIQUENESS
86
    var idSuffix = "_" + triggerName;
87
88
    //SKILL DELCARATION IDS
89
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceDeclarationText", idSuffix);
0 ignored issues
show
Bug introduced by
The variable Utilities seems to be never declared. If this is a global, consider adding a /** global: Utilities */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
90
91
    // MICRO-REFERENCE IDs
92
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceReferenceName", idSuffix);
93
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceReferenceEmail", idSuffix);
94
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceReferenceRelationship", idSuffix);
95
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceReferenceFrom", idSuffix);
96
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceReferenceUntil", idSuffix);
97
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceReferenceExpLevel", idSuffix);
98
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceReferenceStory", idSuffix);
99
100
    // SKILL SAMPLE IDs
101
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceSampleName", idSuffix);
102
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceSampleType", idSuffix);
103
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceSampleDateCreated", idSuffix);
104
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceSampleHttpLink", idSuffix);
105
    Utilities.addSuffixToElementId(evidencePanel, "applicationEvidenceSampleStory", idSuffix);
106
107
    // POPULATE "SLIDERS"
108
    // DEV-NOTE: Beware copy-paste errors, and confusing EXPERTISE with EXPERIENCE
109
110
    var expertiseSelector = evidencePanel.querySelector(".applicant-evidence__expertise-wrapper");
111
    var expertiseRadioGroup = evidencePanel.querySelector(".applicant-evidence__expertise-radiogroup");
112
    var expertiseRadioGroupTitle = evidencePanel.querySelector(".applicant-evidence__expertise-radiogroup-title");
113
    expertiseRadioGroupTitle.setAttribute("id", "expertiseRadioGroupTitle"+idSuffix);
114
    expertiseRadioGroup.setAttribute("aria-labelledby", "expertiseRadioGroupTitle"+idSuffix);
115
    expertiseSelector.innerHTML = ""; //clear template items
116
    LookupAPI.getLookupResponse("skill_level", function (items) {
0 ignored issues
show
Bug introduced by
The variable LookupAPI seems to be never declared. If this is a global, consider adding a /** global: LookupAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
117
        var numberOfItems = items.length;
118
        var fragment = document.createDocumentFragment();
119
        for (var i = 0; i < numberOfItems; i++) {
120
            var item = items[i];
121
            var outputValue = item.id; //TODO: change to value
122
            var itemElement = EvidenceAPI.instantiateApplicationEvidenceExpertiseItem(criteriaId, criteriaType, item.id, item.value, outputValue, numberOfItems);
123
            fragment.appendChild(itemElement);
124
        }
125
        expertiseSelector.appendChild(fragment);
126
    });
127
128
    var experienceSelector = evidencePanel.querySelector(".applicant-evidence__experience-wrapper");
129
    var experienceRadioGroup = evidencePanel.querySelector(".applicant-evidence__experience-radiogroup");
0 ignored issues
show
Unused Code introduced by
The variable experienceRadioGroup seems to be never used. Consider removing it.
Loading history...
130
    var experienceRadioGroupTitle = evidencePanel.querySelector(".applicant-evidence__experience-radiogroup-title");
0 ignored issues
show
Unused Code introduced by
The variable experienceRadioGroupTitle seems to be never used. Consider removing it.
Loading history...
131
    expertiseRadioGroupTitle.setAttribute("id", "expertiseRadioGroupTitle"+idSuffix);
132
    expertiseRadioGroup.setAttribute("aria-labelledby", "expertiseRadioGroupTitle"+idSuffix);
133
    experienceSelector.innerHTML = ""; //clear template items
134
    LookupAPI.getLookupResponse("experience_level", function (items) {
135
        var numberOfItems = items.length;
136
        var fragment = document.createDocumentFragment();
137
        for (var i = 0; i < numberOfItems; i++) {
138
            var item = items[i];
139
            var outputValue = item.id; //TODO: change to value
140
            var itemElement = EvidenceAPI.instantiateApplicationEvidenceExperienceItem(criteriaId, criteriaType, item.id, item.value, outputValue, numberOfItems);
141
            fragment.appendChild(itemElement);
142
        }
143
        experienceSelector.appendChild(fragment);
144
    });
145
146
    //POPULATE SELECT INPUTS
147
    var relationshipSelect = evidencePanel.querySelector("#applicationEvidenceReferenceRelationship" + idSuffix);
148
    LookupAPI.populateDropdownElement("relationship", relationshipSelect, true);
149
    var refExperienceLevel = evidencePanel.querySelector("#applicationEvidenceReferenceExpLevel" + idSuffix);
150
    LookupAPI.populateDropdownElement("experience_level", refExperienceLevel, true);
151
152
    var sampleFileTypeSelect = evidencePanel.querySelector("select[name=\"sample_type\"]");
153
    LookupAPI.populateDropdownElement("file_type", sampleFileTypeSelect, true);
154
155
    //ADD EVENT HANDLERS
156
157
    //adding save button handlers
158
    var saveDeclarationBtn = evidencePanel.querySelector("." +SkillDeclarationAPI.wrapperClass + " .applicant-evidence__save-button");
0 ignored issues
show
Bug introduced by
The variable SkillDeclarationAPI seems to be never declared. If this is a global, consider adding a /** global: SkillDeclarationAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
159
    saveDeclarationBtn.addEventListener("click", function declarationSaveListener() {
160
        SkillDeclarationAPI.saveSingleSkillDeclaration(criteriaId, function onDeclarationSaveSuccess() {
0 ignored issues
show
Bug introduced by
The variable SkillDeclarationAPI seems to be never declared. If this is a global, consider adding a /** global: SkillDeclarationAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
161
            EvidenceAPI.setUiSaved(criteriaId, SkillDeclarationAPI, true);
0 ignored issues
show
Bug introduced by
The variable SkillDeclarationAPI seems to be never declared. If this is a global, consider adding a /** global: SkillDeclarationAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
162
        }, null);
163
    });
164
    var saveReferenceBtn = evidencePanel.querySelector("." + MicroReferenceAPI.wrapperClass + " .applicant-evidence__save-button");
0 ignored issues
show
Bug introduced by
The variable MicroReferenceAPI seems to be never declared. If this is a global, consider adding a /** global: MicroReferenceAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
165
    saveReferenceBtn.addEventListener("click", function referenceSaveListener() {
166
        MicroReferenceAPI.saveSingleMicroReference(criteriaId, function onReferenceSaveSuccess() {
0 ignored issues
show
Bug introduced by
The variable MicroReferenceAPI seems to be never declared. If this is a global, consider adding a /** global: MicroReferenceAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
167
            EvidenceAPI.setUiSaved(criteriaId, MicroReferenceAPI, true);
0 ignored issues
show
Bug introduced by
The variable MicroReferenceAPI seems to be never declared. If this is a global, consider adding a /** global: MicroReferenceAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
168
        }, null);
169
    });
170
    var saveSampleBtn = evidencePanel.querySelector("." + SkillSampleAPI.wrapperClass + " .applicant-evidence__save-button");
0 ignored issues
show
Bug introduced by
The variable SkillSampleAPI seems to be never declared. If this is a global, consider adding a /** global: SkillSampleAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
171
    saveSampleBtn.addEventListener("click", function sampleSaveListener() {
172
        SkillSampleAPI.saveSingleSkillSample(criteriaId, function onSampleSaveSuccess() {
0 ignored issues
show
Bug introduced by
The variable SkillSampleAPI seems to be never declared. If this is a global, consider adding a /** global: SkillSampleAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
173
            EvidenceAPI.setUiSaved(criteriaId, SkillSampleAPI, true);
0 ignored issues
show
Bug introduced by
The variable SkillSampleAPI seems to be never declared. If this is a global, consider adding a /** global: SkillSampleAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
174
        }, null);
175
    });
176
177
    //define a function to check skill declaration status
178
    function declarationOnChange() {
179
        SkillDeclarationAPI.onStatusChange(criteriaId);
0 ignored issues
show
Bug introduced by
The variable SkillDeclarationAPI seems to be never declared. If this is a global, consider adding a /** global: SkillDeclarationAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
180
    }
181
    //Add handler to experienence inputs
182
    var experienceInputs = evidencePanel.querySelectorAll("input[name=experience]");
183
    for(var i=0; i<experienceInputs.length; i++) {
184
        experienceInputs[i].onclick = declarationOnChange;
185
    }
186
    //Add handler to expertise inputs
187
    var expertiseInputs = evidencePanel.querySelectorAll("input[name=expertise]");
188
    for(var i=0; i<expertiseInputs.length; i++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 183. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Bug introduced by
It seems like i was already defined.
Loading history...
189
        expertiseInputs[i].onclick = declarationOnChange;
190
    }
191
    //Add handler to Skill Declaration story text
192
    var declarationText = evidencePanel.querySelector(".applicant-evidence__skill-declaration-text");
193
    declarationText.onchange = declarationOnChange;
194
195
    //define a function to check micro-reference status
196
    function referenceOnChange() {
197
        MicroReferenceAPI.onStatusChange(criteriaId);
0 ignored issues
show
Bug introduced by
The variable MicroReferenceAPI seems to be never declared. If this is a global, consider adding a /** global: MicroReferenceAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
198
    }
199
    //Add onChange handler to all micro-reference inputs
200
    evidencePanel.querySelector("input[name=\"reference_name\"]").onchange = referenceOnChange;
201
    evidencePanel.querySelector("input[name=\"reference_email\"]").onchange = referenceOnChange;
202
    evidencePanel.querySelector("select[name=\"reference_relationship\"]").onchange = referenceOnChange;
203
    evidencePanel.querySelector("input[name=\"reference_from_date\"]").onchange = referenceOnChange;
204
    evidencePanel.querySelector("input[name=\"reference_until_date\"]").onchange = referenceOnChange;
205
    evidencePanel.querySelector("select[name=\"reference_exp_level\"]").onchange = referenceOnChange;
206
    evidencePanel.querySelector("textarea[name=\"reference_story\"]").onchange = referenceOnChange;
207
208
    //define a function to check skill sample status
209
    function sampleOnChange() {
210
        SkillSampleAPI.onStatusChange(criteriaId);
0 ignored issues
show
Bug introduced by
The variable SkillSampleAPI seems to be never declared. If this is a global, consider adding a /** global: SkillSampleAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
211
    }
212
    //Add onChange handler to all skill sample inputs
213
    evidencePanel.querySelector("input[name=\"sample_name\"]").onchange = sampleOnChange;
214
    evidencePanel.querySelector("select[name=\"sample_type\"]").onchange = sampleOnChange;
215
    evidencePanel.querySelector("input[name=\"sample_date_created\"]").onchange = sampleOnChange;
216
    evidencePanel.querySelector("input[name=\"sample_http_link\"]").onchange = sampleOnChange;
217
    evidencePanel.querySelector("textarea[name=\"sample_story\"]").onchange = sampleOnChange;
218
219
220
    //SET TEXTAREA FIELDS TO EMPTY
221
    //This is to override IE11's habit of setting textarea values with their placeholder text
222
    evidencePanel.querySelector(".applicant-evidence__skill-declaration-text").value = "";
223
    evidencePanel.querySelector("textarea[name=\"reference_story\"]").value = "";
224
    evidencePanel.querySelector("textarea[name=\"sample_story\"]").value = "";
225
226
    return evidencePanel;
227
};
228
229
EvidenceAPI.instantiateApplicationPreviewEvidencePanel = function (criteriaId, criteriaType, criteriaName) {
230
    var evidencePanel = document.getElementById("applicationPreviewEvidencePanelTemplate").firstElementChild.cloneNode(true);
231
232
    evidencePanel.classList.remove("template");
233
234
    var triggerName = EvidenceAPI.evidencePreviewTriggerName(criteriaId, criteriaType);
235
    var menuItemId = EvidenceAPI.previewMenuItemId(criteriaId, criteriaType);
236
    evidencePanel.setAttribute("data-evidence-target", triggerName);
237
    evidencePanel.setAttribute("aria-labelledby", menuItemId);
238
239
    //SET DATA ATTRIBUTES
240
    evidencePanel.setAttribute("data-criteria-type", criteriaType);
241
    evidencePanel.setAttribute("data-criteria-id", criteriaId);
242
243
    //SET SKILL TITLE
244
    evidencePanel.querySelector(".applicant-evidence-preview__criteria-name").innerHTML = criteriaName;
245
246
    return evidencePanel;
247
};
248
249
EvidenceAPI.instantiateApplicationEvidenceExpertiseItem = function (criteriaId, criteriaType, expertiseId, expertiseValue, outputValue, numberOfItems) {
250
    var templateId = "applicationEvidenceExpertiseItemTemplate";
251
    var inputId = "expertise__" + EvidenceAPI.evidenceTriggerName(criteriaId, criteriaType) + "__" + expertiseId;
252
    return EvidenceAPI.instantiateApplicationEvidenceRadioItem(templateId, inputId, expertiseValue, outputValue, numberOfItems);
253
};
254
255
EvidenceAPI.instantiateApplicationEvidenceExperienceItem = function (criteriaId, criteriaType, experienceId, experienceValue, outputValue, numberOfItems) {
256
    var templateId = "applicationEvidenceExperienceItemTemplate";
257
    var inputId = "experience__" + EvidenceAPI.evidenceTriggerName(criteriaId, criteriaType) + "__" + experienceId;
258
    return EvidenceAPI.instantiateApplicationEvidenceRadioItem(templateId, inputId, experienceValue, outputValue, numberOfItems);
259
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
260
261
EvidenceAPI.instantiateApplicationEvidenceRadioItem = function (templateId, inputId, textValue, outputValue, numberOfItems) {
262
    var itemLabel = document.getElementById(templateId).firstElementChild.cloneNode(true);
263
264
    itemLabel.classList.remove("template");
265
266
    var itemInput = itemLabel.querySelector("input");
267
    var itemText = itemLabel.querySelector(".form__radio-group-span");
268
269
    var sizeClass = "small-1of" + numberOfItems;
270
    itemLabel.classList.add(sizeClass);
271
272
    itemLabel.setAttribute("for", inputId);
273
    itemInput.id = inputId;
274
    itemInput.value = outputValue;
275
276
    itemText.innerHTML = textValue;
277
    return itemLabel;
278
};
279
280
/**
281
 *
282
 * @param {int} criteriaId
283
 * @param {string} iconClass - Should be "fa-check", "fa-user", or "fa-file"
284
 * @param {boolean} isActive
285
 * @return {undefined}
286
 */
287
EvidenceAPI.setEvidenceIconStatus = function(criteriaId, iconClass, isActive) {
288
    var panel = document.querySelector(".applicant-evidence__skill[data-criteria-id=\"" + criteriaId + "\"]:not(.template)");
289
    if (isActive) {
290
        //Activate icon in accordion trigger
291
        var check = panel.querySelector(".applicant-evidence__accordion-trigger-icon-wrapper ." + iconClass);
292
        check.classList.add("active");
293
294
        //Activate icon in menu item
295
        var menuItem = document.querySelector(".applicant-evidence__desktop-menu-item[data-criteria-id=\"" + criteriaId + "\"]");
296
        var menuCheck = menuItem.querySelector(".applicant-evidence__desktop-icon-wrapper ." + iconClass);
297
        menuCheck.classList.add("active");
298
    } else {
299
        //Deactivate icon in accordion trigger
300
        var check = panel.querySelector(".applicant-evidence__accordion-trigger-icon-wrapper ." + iconClass);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable check already seems to be declared on line 291. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Bug introduced by
It seems like check was already defined.
Loading history...
301
        check.classList.remove("active");
302
303
        //Deactivate icon in menu item
304
        var menuItem = document.querySelector(".applicant-evidence__desktop-menu-item[data-criteria-id=\"" + criteriaId + "\"]");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable menuItem already seems to be declared on line 295. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Bug introduced by
It seems like menuItem was already defined.
Loading history...
305
        var menuCheck = menuItem.querySelector(".applicant-evidence__desktop-icon-wrapper ." + iconClass);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable menuCheck already seems to be declared on line 296. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Bug introduced by
It seems like menuCheck was already defined.
Loading history...
306
        menuCheck.classList.remove("active");
307
    }
308
};
309
310
/**
311
 * Saves all completed evidence peices, and deletes incomplete ones from the saved applciation.
312
 *
313
 * If criteriaType is defined, it saves/deletes evidence of the matching criteriaType.
314
 * If criteriaType is undefined, it saves/deletes ALL completed skill declarations.
315
 *
316
 * Calls onSuccess if all evidence pieces are saved/deleted successfully.
317
 *
318
 * @param {string} criteriaType
319
 * @param {function} onSuccess
320
 * @return {undefined}
321
 */
322
EvidenceAPI.saveEvidence = function(criteriaType, onSuccess) {
323
    var pendingRequests = 2;
324
    var everythingSuccessful = true;
325
326
    function saveSuccessful() {
327
        pendingRequests = pendingRequests - 1;
328
        if (pendingRequests == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing pendingRequests to 0 using the == operator is not safe. Consider using === instead.
Loading history...
Coding Style introduced by
It is recommended to use === to compare with 0.

Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.

Read more about comparison operations.

Loading history...
329
            if (everythingSuccessful)
330
                onSuccess();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
331
            else {
332
                window.alert("Something went wrong saving evidence!");
333
                onSuccess();
334
            }
335
        }
336
    }
337
338
    function saveFailed() {
339
        everythingSuccessful = false;
340
        pendingRequests = pendingRequests - 1;
341
        if (pendingRequests == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing pendingRequests to 0 using the == operator is not safe. Consider using === instead.
Loading history...
Coding Style introduced by
It is recommended to use === to compare with 0.

Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.

Read more about comparison operations.

Loading history...
342
            if (everythingSuccessful)
343
                onSuccess();
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
344
            else {
345
                window.alert("Something went wrong while saving evidence!");
346
                onSuccess();
347
            }
348
        }
349
    }
350
351
    SkillDeclarationAPI.saveSkillDeclarations(criteriaType, saveSuccessful, saveFailed);
0 ignored issues
show
Bug introduced by
The variable SkillDeclarationAPI seems to be never declared. If this is a global, consider adding a /** global: SkillDeclarationAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
352
    MicroReferenceAPI.saveMicroReferences(criteriaType, saveSuccessful, saveFailed);
0 ignored issues
show
Bug introduced by
The variable MicroReferenceAPI seems to be never declared. If this is a global, consider adding a /** global: MicroReferenceAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
353
    SkillSampleAPI.saveSkillSamples(criteriaType, saveSuccessful, saveFailed);
0 ignored issues
show
Bug introduced by
The variable SkillSampleAPI seems to be never declared. If this is a global, consider adding a /** global: SkillSampleAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
354
};
355
356
// New Evidence UI Handlers ====================================================
357
358
    // Scroll to Skills
359
360
        EvidenceAPI.scrollToSkills = function(e, button) {
361
362
            e.preventDefault();
363
364
            var skills = button.parentElement.parentElement.parentElement.querySelector(".applicant-evidence__anchor");
365
366
            var xPosition = 0;
367
            var yPosition = 0;
368
369
            while(skills) {
370
                xPosition += (skills.offsetLeft - skills.scrollLeft + skills.clientLeft);
371
                yPosition += (skills.offsetTop - skills.scrollTop + skills.clientTop);
372
                skills = skills.offsetParent;
373
            }
374
375
            window.scroll(0, yPosition);
376
377
        }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
378
379
    // Scroll to Individual Skill
380
381
        EvidenceAPI.scrollToThisSkill = function(e, button) {
382
383
            e.preventDefault();
384
385
            var skillID = button.getAttribute("data-evidence-trigger");
386
387
            var skills = document.querySelectorAll(".applicant-evidence__skill");
388
389
            for (var i=0; i<skills.length; i++) {
390
391
                if (skills[i].getAttribute("data-evidence-target") == skillID) {
392
                    var thisSkill = skills[i];
393
                }
394
395
            }
396
397
            var xPosition = 0;
398
            var yPosition = 0;
399
400
            while(thisSkill) {
0 ignored issues
show
Bug introduced by
The variable thisSkill seems to not be initialized for all possible execution paths.
Loading history...
Bug introduced by
The variable thisSkill seems to be used out of scope.

This error can usually be fixed by declaring the variable in the scope where it is used:

function someFunction() {
    (function() {
        var i = 0;
    })();

    // i is not defined.
    alert(i);
}

// This can be fixed by moving the var statement to the outer scope.

function someFunction() {
    var i;
    (function() {
        i = 1;
    })();

    alert(i);
};
Loading history...
401
                xPosition += (thisSkill.offsetLeft - thisSkill.scrollLeft + thisSkill.clientLeft);
0 ignored issues
show
Bug introduced by
The variable thisSkill seems to be used out of scope.

This error can usually be fixed by declaring the variable in the scope where it is used:

function someFunction() {
    (function() {
        var i = 0;
    })();

    // i is not defined.
    alert(i);
}

// This can be fixed by moving the var statement to the outer scope.

function someFunction() {
    var i;
    (function() {
        i = 1;
    })();

    alert(i);
};
Loading history...
402
                yPosition += (thisSkill.offsetTop - thisSkill.scrollTop + thisSkill.clientTop);
0 ignored issues
show
Bug introduced by
The variable thisSkill seems to be used out of scope.

This error can usually be fixed by declaring the variable in the scope where it is used:

function someFunction() {
    (function() {
        var i = 0;
    })();

    // i is not defined.
    alert(i);
}

// This can be fixed by moving the var statement to the outer scope.

function someFunction() {
    var i;
    (function() {
        i = 1;
    })();

    alert(i);
};
Loading history...
403
                thisSkill = thisSkill.offsetParent;
0 ignored issues
show
Bug introduced by
The variable thisSkill seems to be used out of scope.

This error can usually be fixed by declaring the variable in the scope where it is used:

function someFunction() {
    (function() {
        var i = 0;
    })();

    // i is not defined.
    alert(i);
}

// This can be fixed by moving the var statement to the outer scope.

function someFunction() {
    var i;
    (function() {
        i = 1;
    })();

    alert(i);
};
Loading history...
404
            }
405
406
            var location = yPosition - 40;
407
408
            // console.log(location);
409
410
            window.scroll(0, location);
411
412
        }
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
413
414
    // Micro References
415
416
        EvidenceAPI.addMicroReference = function(button) {
417
            var panel = button.closest(".applicant-evidence__skill");
418
419
            panel.querySelector(".applicant-evidence__skill-attribute--reference").classList.add("active");
420
421
            panel.querySelector(".applicant-evidence__skill-attribute--reference").querySelector(".accordion__trigger").focus();
422
423
            panel.querySelector(".applicant-evidence__optional-button--reference").classList.add("hidden");
424
        };
425
426
        EvidenceAPI.removeMicroReference = function(button) {
427
            var panel = button.closest(".applicant-evidence__skill");
428
            panel.querySelector(".applicant-evidence__skill-attribute--reference").classList.remove("active");
429
430
            panel.querySelector(".applicant-evidence__optional-button--reference").classList.remove("hidden");
431
            
432
            var criteriaId = panel.getAttribute("data-criteria-id");
433
            MicroReferenceAPI.deleteMicroReference(criteriaId);
0 ignored issues
show
Bug introduced by
The variable MicroReferenceAPI seems to be never declared. If this is a global, consider adding a /** global: MicroReferenceAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
434
        };
435
436
    // Work Sample
437
438
        EvidenceAPI.addWorkSample = function(button) {
439
            var panel = button.closest(".applicant-evidence__skill");
440
441
            panel.querySelector(".applicant-evidence__skill-attribute--sample").classList.add("active");
442
443
            panel.querySelector(".applicant-evidence__skill-attribute--sample").querySelector(".accordion__trigger").focus();
444
445
            panel.querySelector(".applicant-evidence__optional-button--sample").classList.add("hidden");
446
447
        };
448
449
        EvidenceAPI.removeWorkSample = function(button) {
450
            var panel = button.closest(".applicant-evidence__skill");
451
452
            panel.querySelector(".applicant-evidence__skill-attribute--sample").classList.remove("active");
453
454
            panel.querySelector(".applicant-evidence__optional-button--sample").classList.remove("hidden");
455
456
            var criteriaId = panel.getAttribute("data-criteria-id");
457
            SkillSampleAPI.deleteSkillSample(criteriaId);
0 ignored issues
show
Bug introduced by
The variable SkillSampleAPI seems to be never declared. If this is a global, consider adding a /** global: SkillSampleAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
458
        };
459
460
    // Saving
461
462
    EvidenceAPI.setUiSaved = function(criteriaId, subEvidenceAPI, isSaved) {
463
        var panel = document.querySelector(".applicant-evidence__skill[data-criteria-id=\"" + criteriaId + "\"]:not(.template)");
464
        var evidenceItem = panel.querySelector("." + subEvidenceAPI.wrapperClass);
465
        var saveButton = evidenceItem.querySelector(".applicant-evidence__save-button");
466
        if (isSaved) {
467
            //EvidenceAPI.setUiComplete(criteriaId, subEvidenceAPI, true);
468
            saveButton.classList.add("button-green");
469
            saveButton.classList.remove("button-blue");
470
            saveButton.innerHTML = "Saved";
471
        } else {
472
            saveButton.classList.remove("button-green");
473
            saveButton.classList.add("button-blue");
474
            saveButton.innerHTML = "Save";
475
        }
476
    };
477
478
    EvidenceAPI.setUiComplete = function(criteriaId, subEvidenceAPI, isComplete) {
479
        var panel = document.querySelector(".applicant-evidence__skill[data-criteria-id=\"" + criteriaId + "\"]:not(.template)");
480
        var evidenceItem = panel.querySelector("." + subEvidenceAPI.wrapperClass);
481
        var accordionTrigger = evidenceItem.querySelector(".accordion__trigger");
482
        var menuItem = document.querySelector(".applicant-evidence__desktop-menu-item[data-criteria-id=\"" + criteriaId + "\"]:not(.template)");
483
        if (isComplete) {
484
            accordionTrigger.classList.add("complete");
485
            if (subEvidenceAPI === SkillDeclarationAPI) {
0 ignored issues
show
Bug introduced by
The variable SkillDeclarationAPI seems to be never declared. If this is a global, consider adding a /** global: SkillDeclarationAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
486
                //Skill Declaration completeness determines whole section completeness
487
                menuItem.classList.add("complete");
488
            }
489
        } else {
490
            accordionTrigger.classList.remove("complete");
491
            if (subEvidenceAPI === SkillDeclarationAPI) {
492
                //Skill Declaration completeness determines whole section completeness
493
                menuItem.classList.remove("complete");
494
            }
495
        }
496
    };
497
    
498
    EvidenceAPI.onStatusUpdate = function() {
499
        var essentialsComplete = true;
500
        var essentialEvidencePanels = document.querySelectorAll(".applicant-evidence__skill[data-criteria-type=\"essential\"]:not(.template)");
501
        for (var i = 0; i < essentialEvidencePanels.length; i++) {
502
            var panel = essentialEvidencePanels[i];
503
            var requiredAccordion = panel.querySelector(".applicant-evidence__skill-attribute--required .accordion__trigger");
504
            if (!requiredAccordion.classList.contains("complete")) {
505
                essentialsComplete = false;
506
                break;
507
            }
508
        }
509
        if (essentialsComplete) {
510
            var continueBtns = document.querySelectorAll(".applicant-evidence__save-and-continue--essential");
511
            for (var i = 0; i < continueBtns.length; i++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 501. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Bug introduced by
It seems like i was already defined.
Loading history...
512
                continueBtns[i].removeAttribute("disabled");
513
            }
514
        } else {
515
            var continueBtns = document.querySelectorAll(".applicant-evidence__save-and-continue--essential");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable continueBtns already seems to be declared on line 510. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Bug introduced by
It seems like continueBtns was already defined.
Loading history...
516
            for (var i = 0; i < continueBtns.length; i++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 501. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Bug introduced by
It seems like i was already defined.
Loading history...
517
                continueBtns[i].setAttribute("disabled", "");
518
            }
519
        }
520
    };
521