Conditions | 1 |
Paths | 1 |
Total Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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:
1 | // Directives |
||
19 | Directives.prototype.defineConfirm = function () { |
||
20 | const _this = this |
||
21 | const DirectiveDefinition = {} |
||
22 | |||
23 | const getConfirmMessage = function(binding) { |
||
24 | if (binding.value && binding.value.message) { |
||
25 | return binding.value.message |
||
26 | } |
||
27 | return typeof binding.value === 'string' ? binding.value : null |
||
28 | } |
||
29 | |||
30 | const getOptions = function(binding) { |
||
31 | let options = typeof binding.value === 'object' ? binding.value : {} |
||
32 | |||
33 | delete options['ok'] |
||
34 | delete options['cancel'] |
||
35 | |||
36 | if(binding.arg && CONFIRM_TYPES.hasOwnProperty(binding.arg.toUpperCase())){ |
||
37 | options.type = CONFIRM_TYPES[binding.arg.toUpperCase()] |
||
38 | } |
||
39 | |||
40 | return options |
||
41 | } |
||
42 | |||
43 | const getCatchCallback = function(binding) { |
||
44 | if (binding.value && binding.value.cancel) { |
||
45 | return binding.value.cancel |
||
46 | } |
||
47 | return noop |
||
48 | } |
||
49 | |||
50 | const getThenCallback = function(binding, el){ |
||
51 | if (binding.value && binding.value.ok) { |
||
52 | return binding.value.ok |
||
53 | } else { |
||
54 | return () => { |
||
55 | // Unbind to allow original event |
||
56 | el.removeEventListener('click', el.VuejsDialog.clickHandler, true) |
||
57 | // Trigger original event |
||
58 | clickNode(el) |
||
59 | // Re-bind listener |
||
60 | el.addEventListener('click', el.VuejsDialog.clickHandler, true) |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | |||
65 | const clickHandler = function (event, el, binding) { |
||
66 | event.preventDefault() |
||
67 | event.stopImmediatePropagation() |
||
68 | |||
69 | let options = getOptions(binding) |
||
70 | let confirmMessage = getConfirmMessage(binding) |
||
71 | let thenCallback = getThenCallback(binding, el) |
||
72 | let catchCallback = getCatchCallback(binding) |
||
73 | |||
74 | _this.Vue.dialog |
||
75 | .confirm(confirmMessage, options) |
||
76 | .then(thenCallback) |
||
77 | .catch(catchCallback) |
||
78 | } |
||
79 | |||
80 | |||
81 | DirectiveDefinition.bind = (el, binding) => { |
||
82 | if (el.VuejsDialog === undefined) { |
||
83 | el.VuejsDialog = {} |
||
84 | } |
||
85 | |||
86 | el.VuejsDialog.clickHandler = function (event) { |
||
87 | clickHandler(event, el, binding) |
||
88 | } |
||
89 | |||
90 | el.addEventListener('click', el.VuejsDialog.clickHandler, true) |
||
91 | } |
||
92 | |||
93 | DirectiveDefinition.unbind = (el) => { |
||
94 | el.removeEventListener('click', el.VuejsDialog.clickHandler, true) |
||
95 | } |
||
96 | |||
97 | return DirectiveDefinition |
||
98 | } |
||
99 | |||
105 |