PageSourceSearch

https://heatherstorta.com/wp-content/plugins/knit-calc-2/smkc625a-esid-func.js?ver=7.1.2

js heatherstorta.com collected 2026-10-05 02:40:20 UTC 14,501 bytes, 332 lines download raw bytes

1
2
3// Initialize code after the document is fully loaded
4document.addEventListener("DOMContentLoaded", initializeESID());
5
6// Capture elements and setup event listeners
7function initializeESID() {
8
9    // Stitch direction block
10    increaseRadio = document.getElementById("smkc625a-esid-increase-radio");
11    decreaseRadio = document.getElementById("smkc625a-esid-decrease-radio");
12    increaseRadio.addEventListener("click", function() {processESIDDirectionUpdate();});
13    decreaseRadio.addEventListener("click", function() {processESIDDirectionUpdate();});
14
15    // Increase method block
16    increaseKFBRadio = document.getElementById("smkc625a-esid-kfb");
17    increaseOtherRadio = document.getElementById("smkc625a-esid-other");
18    increaseKFBRadio.addEventListener("click", function() {processESIDIncMethodUpdate();});
19    increaseOtherRadio.addEventListener("click", function() {processESIDIncMethodUpdate();});
20
21    // Knit style block
22    flatRadio = document.getElementById("smkc625a-esid-flat-radio");
23    itrButton = document.getElementById("smkc625a-esid-itr-radio");
24    flatRadio.addEventListener("click", function() {processESIDKnitStyleUpdate();});
25    itrButton.addEventListener("click", function() {processESIDKnitStyleUpdate();});
26
27    // Selvedge stitch capture
28    selvedgeInput1 = document.getElementById("smkc625a-esid-sel1");
29    selvedgeInput2 = document.getElementById("smkc625a-esid-sel2");
30    selvedgeInput1.addEventListener("change", function() {processESIDSelvedgeUpdate();});
31    selvedgeInput2.addEventListener("change", function() {processESIDSelvedgeUpdate();});
32
33    // Calculate total stitches when input values change
34    currentStitchesInput = document.getElementById("smkc625a-esid-current-stitches");
35    totalIncDecInput = document.getElementById("smkc625a-esid-total-incdec");
36    currentStitchesInput.addEventListener("change", function() {processESIDStitchCountUpdate();});
37    totalIncDecInput.addEventListener("change", function() {processESIDStitchCountUpdate();});
38
39    
40    // Perform final calculation
41    calculateButton = document.getElementById("smkc625a-esid-calculate-button");
42    calculateButton.addEventListener("click", function() {calculateFinalESIDValues();});
43
44    // Copy output to clipboard
45    copyButton = document.getElementById("copy-esid-output-button");
46    copyButton.addEventListener("click", function() {copyESIDOutputToClipboard();});
47}
48
49function processESIDDirectionUpdate() {
50    // Capture the current selection
51    collectESIDFormValues();
52
53    // Hide the error message box since selecting this box clears any errors
54    changeValue('smkc625a-esid-incdec-notes', "");
55
56    if (direction == "increasing") {
57        // If increasing, update the form labels accordingly and display the increasing method box
58        changeValue("incdecLabel", "Number of Increases ");
59        setBlockDisplay('smkc625a-esid-incdec-method-block', "flex");
60    }
61    else if (direction == "decreasing") {
62        // If decreasing, update the form labels accordingly and hide the increasing method box
63        changeValue("incdecLabel", "Number of Decreases ");
64        setBlockDisplay('smkc625a-esid-incdec-method-block', "none");
65    }
66
67    // A change in direction may clear stitch count errors so those values should be revalidated
68    processESIDStitchCountUpdate();
69}
70
71function processESIDIncMethodUpdate() {
72    // Hide the error message box since selecting this box clears any errors
73    changeValue('smkc625a-esid-incdec-method-notes', "");
74}
75
76function processESIDKnitStyleUpdate() {
77    // Capture the current selection
78    collectESIDFormValues();
79
80    // Hide the error message box since selecting this box clears any errors
81    changeValue('smkc625a-esid-knitstyle-notes', "");
82
83    // Display the selvedge stich block if 'flat' was selected (otherwise hide)
84    if (knitStyle == "flat") {
85        setBlockDisplay('smkc625a-esid-selvedge-block', "flex");
86    }
87    else if (knitStyle == "round") {
88        setBlockDisplay('smkc625a-esid-selvedge-block', "none");
89    }
90    // A change in knit style may clear stitch count errors so those values should be revalidated
91    processESIDStitchCountUpdate();
92}
93
94function processESIDSelvedgeUpdate() {
95    // Capture the current entries
96    collectESIDFormValues();
97
98    // Clear errors if both values are greater than 0
99    if (selvedge1 > 0 && selvedge2 > 0) {
100        changeValue("smkc625a-esid-selvedge-notes", "");
101    }
102    // A change in selvedge counts may clear stitch count errors so those values should be revalidated
103    processESIDStitchCountUpdate();
104}
105
106function processESIDStitchCountUpdate() {
107    // Capture the current entries
108    collectESIDFormValues();
109    tStitches = cStitches + tIncreases;
110
111
112    // Check to see if all conditions are valid
113    if (isESIDValidStitchCounts()) {
114        // Clear any error conditions that have been resolved with this change.
115        updateESIDTotalStitchCountDisplay();
116    }
117    else {
118        changeValue("smkc625a-esid-stitch-counts-notes", "");
119    }
120}
121
122function updateESIDTotalStitchCountDisplay() {
123    // Display total stitch count
124    document.getElementById("smkc625a-esid-stitch-counts-notes").style.color = "black";
125    changeValue("smkc625a-esid-stitch-counts-notes", "Total New Stitches: " + tStitches);
126}
127
128function isESIDValidStitchCounts() {
129    stitchCountErrorMessage = "";
130
131    // Capture the current entries
132    collectESIDFormValues();
133
134    if (cStitches == 0 || tIncreases == 0) {
135        let incdecText = direction == "decreasing" ? "Decreases" : "Increases";
136
137        stitchCountErrorMessage = "Current Stitches and Number of " + incdecText + " cannot be zero!";
138    }
139    else if (knitStyle == "round" && direction == "increasing") {
140        // round && increasing
141        if (tIncreases > cStitches) {
142            stitchCountErrorMessage = "Increases cannot be greater than current stitches! (" + cStitches + ")";
143        }
144    }
145    else if (knitStyle == "round" && direction == "decreasing") {
146        // round && decreasing
147        if (tIncreases > (cStitches / 2)) {
148            stitchCountErrorMessage = "Decreases cannot be greater than 1/2 current stitches! (" + (cStitches / 2) + ")";
149        }
150    }
151    else if (knitStyle == "flat" && direction == "increasing") {
152        // flat && increasing
153        if (tIncreases > (cStitches - selvedge1 - selvedge2 + 1)) {
154            stitchCountErrorMessage = "Increases cannot be greater than current stitches minus the sum of Selvedge Edge stitches plus 1! (" + (cStitches - selvedge1 - selvedge2 + 1) + ")";
155        }
156    }
157    else if (knitStyle == "flat" && direction == "decreasing") {
158        // flat && decreasing
159        if (tIncreases > (cStitches - selvedge1 - selvedge2) / 2) {
160            stitchCountErrorMessage = "Decreases cannot be greater than 1/2 the current stitches minus the sum of Selvedge Edge stitches! (" + ((cStitches - selvedge1 - selvedge2) / 2) + ")";
161        }
162    }
163    // If any messages were added, return false
164    if (stitchCountErrorMessage == "") {
165        return true;
166    }
167    else {
168        return false;
169    }
170}
171
172function collectESIDFormValues() {
173    direction = getRadioValue("smkc625a-esid-direction");
174    increaseMethod = getRadioValue("smkc625a-esid-incmethod");
175    knitStyle = getRadioValue("smkc625a-esid-knit_style");
176
177    selvedge1 = Number(document.getElementById("smkc625a-esid-sel1").value);
178    selvedge2 = Number(document.getElementById("smkc625a-esid-sel2").value);
179
180    cStitches = Number(currentStitchesInput.value);
181    tIncreases = Number(totalIncDecInput.value);
182}
183
184function copyESIDOutputToClipboard() {
185    navigator.clipboard.writeText(document.getElementById("smkc625a-esid-summary-output").innerText);
186}
187
188function isESIDClean() {
189    // Validate all inputs
190    // Increment the counter if any errors are found
191    // Display the message block wherever an error is found
192    // If counter > 0, return false
193    inputErrors = 0;
194
195    if (direction == "unselected") {
196        inputErrors++;
197        changeValue("smkc625a-esid-incdec-notes", "Must indicate direction!");
198    }
199
200    if (direction == "increasing" && increaseMethod == "unselected") {
201        inputErrors++;
202        changeValue("smkc625a-esid-incdec-method-notes", "Increase method required!");
203    }
204
205    if (knitStyle == "unselected") {
206        inputErrors++;
207        changeValue("smkc625a-esid-knitstyle-notes", "Please indicate knitting style!");
208    }
209
210    if (knitStyle == "flat" && (selvedge1 == 0 || selvedge2 == 0)) {
211        inputErrors++;
212        changeValue("smkc625a-esid-selvedge-notes", "Selvedge edge stitch counts cannot be zero!");
213    }
214
215    if (!isESIDValidStitchCounts()) {
216        inputErrors++;
217        document.getElementById("smkc625a-esid-stitch-counts-notes").style.color = "red";
218        document.getElementById("smkc625a-esid-stitch-counts-notes").innerHTML = stitchCountErrorMessage;
219    }
220
221    if (inputErrors == 0) {
222        return true;
223    }
224    else {
225        return false;
226    }
227}
228
229function calculateFinalESIDValues() {
230    collectESIDFormValues();
231
232    if ( ! isESIDClean() ) {
233        // There are errors so do not proceed with calculations
234        setBlockDisplay('smkc625a-esid-summary-block', "none");
235        return false;
236    }
237
238    
239    //Calculate Rates
240    var directionAdjustment = 0;
241    if (direction == "decreasing") {
242        directionAdjustment = -2;
243    }
244
245    var kfbAdjustment = 0;
246    if (increaseMethod == "kfb") {
247        kfbAdjustment = -1;
248    }
249
250    var rateAdjustment;
251    if (knitStyle == "flat") {
252        rate = (tStitches-selvedge1-selvedge2+directionAdjustment+kfbAdjustment)/(tIncreases-1);
253        rateAdjustment = -1;
254    }
255    else if (knitStyle == "round") {
256        rate = (tStitches)/(tIncreases);
257        rateAdjustment = 0;
258    }
259
260    // Calculate non-fractional low and high rates
261    rate1 = Math.ceil(rate);
262    rate2 = Math.floor(rate);
263
264    //Calculate Rate Times
265    rate1times = Math.round((rate - rate2) * (tIncreases + rateAdjustment));
266    rate2times = Math.round((1-(rate - rate2)) * (tIncreases + rateAdjustment));
267
268    if (knitStyle == "flat" && direction == "increasing" && rate1 == rate2) {
269        if (increaseMethod == "other") {
270            resultCode = "Work " + (selvedge1+kfbAdjustment) + " sts, *inc, work " + (rate+kfbAdjustment) + " sts; rep from * " + (tIncreases-2) + " more times, inc, work " + selvedge2 + " sts.";
271        }
272        else {
273            resultCode = "Work " + (selvedge1+kfbAdjustment) + " sts, *kfb, work " + (rate+kfbAdjustment) + " sts; rep from * " + (tIncreases-2) + " more times, kfb, work " + selvedge2 + " sts.";
274        }
275    }
276    else if (knitStyle == "flat" && direction == "increasing" && rate1 != rate2) {
277        if (increaseMethod == "other") {
278            resultCode = "Work " + (selvedge1+kfbAdjustment) + " sts, *inc, work " + (rate1+kfbAdjustment) + " or " + (rate2+kfbAdjustment) + " sts; rep from * " + (tIncreases-2) + " more times (mix rates evenly), inc, work " + selvedge2 + " sts.<br>";
279            resultCode += (rate1+kfbAdjustment) + " sts " + rate1times + " times.<br>";
280            resultCode += (rate2+kfbAdjustment) + " sts " + rate2times + " times.";
281        }
282        else {
283            resultCode = "Work " + (selvedge1+kfbAdjustment) + " sts, *kfb, work " + (rate1+kfbAdjustment) + " or " + (rate2+kfbAdjustment) + " sts; rep from * " + (tIncreases-2) + " more times (mix rates evenly), kfb, work " + selvedge2 + " sts.<br>";
284            resultCode += (rate1+kfbAdjustment) + " sts " + rate1times + " times.<br>";
285            resultCode += (rate2+kfbAdjustment) + " sts " + rate2times + " times.";
286        }
287    }
288    else if (knitStyle == "round" && direction == "increasing" && rate1 == rate2) {
289        if (increaseMethod == "other") {
290            resultCode = "*Inc, work " + (rate+kfbAdjustment) + " sts; rep from * " + (tIncreases-1) + " more times.";
291        }
292        else {
293            resultCode = "*Kfb, work " + (rate+kfbAdjustment) + " sts; rep from * " + (tIncreases-1) + " more times.";
294        }
295    }
296    else if (knitStyle == "round" && direction == "increasing" && rate1 != rate2) {
297        if (increaseMethod == "other") {
298            resultCode = "*Inc, work " + (rate1+kfbAdjustment) + " or " + (rate2+kfbAdjustment) + " sts; rep from * " + (tIncreases-1) + " more times (mix rates evenly).<br>";
299            resultCode += (rate1+kfbAdjustment) + " sts " + rate1times + " times.<br>";
300            resultCode += (rate2+kfbAdjustment) + " sts " + rate2times + " times.";
301        }
302        else {
303            resultCode = "*Kfb, work " + (rate1+kfbAdjustment) + " or " + (rate2+kfbAdjustment) + " sts; rep from * " + (tIncreases-1) + " more times (mix rates evenly).<br>";
304            resultCode += (rate1+kfbAdjustment) + " sts " + rate1times + " times.<br>";
305            resultCode += (rate2+kfbAdjustment) + " sts " + rate2times + " times.";
306        }
307    }
308    else if (knitStyle == "flat" && direction == "decreasing" && rate1 == rate2) {
309        resultCode = "Work " + (selvedge1+kfbAdjustment) + " sts, *k2tog, work " + (rate-2) + " sts; rep from * " + (tIncreases-2) + " more times, k2tog, work " + selvedge2 + " sts.";
310    }
311    else if (knitStyle == "flat" && direction == "decreasing" && rate1 != rate2) {
312        resultCode = "Work " + (selvedge1-kfbAdjustment) + " sts, *k2tog, work " + (rate1-2) + " or " + (rate2-2) + " sts; rep from * " + (tIncreases-2) + " more times (mix rates evenly), k2tog, work " + selvedge2 + " sts.<br>";
313        resultCode += (rate1-2+kfbAdjustment) + " sts " + rate1times + " times.<br>";
314        resultCode += (rate2-2+kfbAdjustment) + " sts " + rate2times + " times.";
315    }
316    else if (knitStyle == "round" && direction == "decreasing" && rate1 == rate2) {
317        resultCode = "*K2tog, work " + (rate-2+kfbAdjustment) + " sts; rep from * " + (tIncreases-1) + " more times.";
318    }
319    else if (knitStyle == "round" && direction == "decreasing" && rate1 != rate2) {
320        resultCode = "*K2tog, work " + (rate1-2+kfbAdjustment) + " or " + (rate2-2+kfbAdjustment) + " sts; rep from * " + (tIncreases-1) + " more times (mix rates evenly).<br>";
321        resultCode += (rate1-2+kfbAdjustment) + " sts " + rate1times + " times.<br>";
322        resultCode += (rate2-2+kfbAdjustment) + " sts " + rate2times + " times.";
323    }
324
325    updateESIDTotalStitchCountDisplay();
326
327    changeValue("smkc625a-esid-summary-output", resultCode);
328
329    setBlockDisplay('smkc625a-esid-summary-block', "block");
330
331    return false
vendor: 4 bytes, lines 331-332
331;
332}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.