Conditions | 21 |
Total Lines | 89 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like helpers.js ➔ liveBlock 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 | /** |
||
21 | export function liveBlock(liveBlockAttribute = "data-live", liveFormSelector = ".live-form") { |
||
22 | |||
23 | var btnToBlock = function(event, btn) { |
||
24 | btn.setAttribute(liveBlockAttribute, btn.getAttribute("src-"+liveBlockAttribute)); |
||
25 | getLiveBlock(btn); |
||
26 | } |
||
27 | |||
28 | var getLiveBlock = function(item) { |
||
29 | fetch(item.getAttribute(liveBlockAttribute), { |
||
30 | //headers: { "Content-Type": "application/json", Accept: "text/plain" }, |
||
31 | method: "POST", |
||
32 | credentials: "include" |
||
33 | }) |
||
34 | .then(function(response) { |
||
35 | return response.text(); |
||
36 | }) |
||
37 | .then(function(body) { |
||
38 | item.removeAttribute(liveBlockAttribute); |
||
39 | item.outerHTML = body; |
||
40 | }). |
||
41 | then(function() { |
||
42 | document.dispatchEvent(new Event("DOMChanged")); |
||
|
|||
43 | }); |
||
44 | } |
||
45 | |||
46 | var htmlLoader = '<div style="width:1em;height:1em;border: 2px solid #222;border-top-color: #fff;border-radius: 50%; animation: 1s spin linear infinite;"></div><style>@keyframes spin {from{transform:rotate(0deg)}to{transform:rotate(360deg)}}</style>'; |
||
47 | |||
48 | var setLoader = function (form) { |
||
49 | var $submitButton = getSubmitButton(form); |
||
50 | if ($submitButton !== undefined) { |
||
51 | var initialButton = $submitButton.outerHTML; |
||
52 | $submitButton.innerHTML = ''; |
||
53 | $submitButton.outerHTML = htmlLoader; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | var sendForm = function(form, liveFormBlock) { |
||
58 | setLoader(form); |
||
59 | |||
60 | var formData = new FormData(form.srcElement); |
||
61 | fetch(form.srcElement.action, { |
||
62 | method: "POST", |
||
63 | body: formData, |
||
64 | credentials: "include" |
||
65 | }) |
||
66 | .then(function(response) { |
||
67 | return response.text(); |
||
68 | }) |
||
69 | .then(function(body) { |
||
70 | liveFormBlock.outerHTML = body; |
||
71 | }) |
||
72 | .then(function() { |
||
73 | document.dispatchEvent(new Event("DOMChanged")); |
||
74 | }); |
||
75 | }; |
||
76 | |||
77 | var getSubmitButton = function(form) { |
||
78 | if (form.srcElement.querySelector("[type=submit]") !== null) { |
||
79 | return form.srcElement.querySelector("[type=submit]"); |
||
80 | } |
||
81 | if (form.srcElement.getElementsByTagName("button") !== null) { |
||
82 | return form.srcElement.getElementsByTagName("button")[0]; |
||
83 | } |
||
84 | return null; |
||
85 | }; |
||
86 | |||
87 | |||
88 | // Listen data-live |
||
89 | document.querySelectorAll("[" + liveBlockAttribute + "]").forEach(item => { |
||
90 | getLiveBlock(item); |
||
91 | }); |
||
92 | |||
93 | // Listen button src-data-live |
||
94 | document.querySelectorAll("[src-" + liveBlockAttribute + "]").forEach(item => { |
||
95 | item.addEventListener("click", event => { |
||
96 | btnToBlock(event, item); |
||
97 | }); |
||
98 | }); |
||
99 | |||
100 | // Listen live-form |
||
101 | document.querySelectorAll(liveFormSelector).forEach(item => { |
||
102 | if (item.querySelector("form") !== null) { |
||
103 | item.querySelector("form").addEventListener("submit", e => { |
||
104 | e.preventDefault(); |
||
105 | sendForm(e, item); |
||
106 | }); |
||
107 | } |
||
108 | }); |
||
109 | } |
||
110 | |||
341 |
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.