Conditions | 1 |
Paths | 4 |
Total Lines | 109 |
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:
1 | /** |
||
5 | (function($, Symphony) { |
||
|
|||
6 | |||
7 | /** |
||
8 | * Pickable allows to show and hide elements based on the value of a select box. |
||
9 | * |
||
10 | * Each option is mapped to its associated content by matching the option `value` |
||
11 | * with the content `id`. If the option value is numeric, Pickable prefices it |
||
12 | * with `choice`. Only the content of the currently selected option is |
||
13 | * shown, all other elements associated with the given select box are hidden. |
||
14 | * |
||
15 | * If no content element of the given `id` is found, Pickable checks for a |
||
16 | * `data-request` attribute on the selected option. If a `data-request` URL is set, |
||
17 | * Pickable tries to fetch the content remotely and expects an content element with |
||
18 | * no additional markup in return. |
||
19 | * |
||
20 | * @name $.symphonyPickable |
||
21 | * @class |
||
22 | * |
||
23 | * @param {Object} options An object containing the element selectors specified below |
||
24 | * @param {String} [options.content='#contents'] Selector to find the container that wraps all pickable elements |
||
25 | * @param {String} [options.pickables='.pickable'] Selector used to find pickable elements |
||
26 | * |
||
27 | * @example |
||
28 | |||
29 | $('.picker').symphonyPickable(); |
||
30 | */ |
||
31 | $.fn.symphonyPickable = function(options) { |
||
32 | var objects = $(this), |
||
33 | settings = { |
||
34 | content: '#contents', |
||
35 | pickables: '.pickable' |
||
36 | }; |
||
37 | |||
38 | $.extend(settings, options); |
||
39 | |||
40 | /*------------------------------------------------------------------------- |
||
41 | Events |
||
42 | -------------------------------------------------------------------------*/ |
||
43 | |||
44 | // Switch content |
||
45 | objects.on('change.pickable', function pick(event) { |
||
46 | var object = $(this), |
||
47 | choice = object.val(), |
||
48 | relation = object.attr('id') || object.attr('name'), |
||
49 | related = $(settings.pickables + '[data-relation="' + relation + '"]'), |
||
50 | picked, request; |
||
51 | |||
52 | // Handle numeric values |
||
53 | if($.isNumeric(choice) === true) { |
||
54 | choice = 'choice' + choice; |
||
55 | } |
||
56 | |||
57 | // Hide all choices |
||
58 | object.trigger('pickstart.pickable'); |
||
59 | related.hide().find('input, select, textarea').prop('readonly', true); |
||
60 | |||
61 | // Selection found |
||
62 | picked = $('#' + choice); |
||
63 | if(picked.length > 0) { |
||
64 | picked.show().find('input, select, textarea').prop('readonly', false).trigger('pick.pickable'); |
||
65 | object.trigger('pickstop.pickable'); |
||
66 | } |
||
67 | |||
68 | // Selection not found |
||
69 | else { |
||
70 | request = object.data('request'); |
||
71 | |||
72 | // Fetch picked element |
||
73 | if(request) { |
||
74 | $.ajax({ |
||
75 | type: 'GET', |
||
76 | url: request, |
||
77 | data: { 'choice': choice }, |
||
78 | dataType: 'html', |
||
79 | success: function(remote) { |
||
80 | content.append(remote); |
||
81 | remote.trigger('pick.pickable'); |
||
82 | object.trigger('pickstop.pickable'); |
||
83 | } |
||
84 | }); |
||
85 | } |
||
86 | } |
||
87 | }); |
||
88 | |||
89 | /*------------------------------------------------------------------------- |
||
90 | Initialisation |
||
91 | -------------------------------------------------------------------------*/ |
||
92 | |||
93 | var content = $(settings.content); |
||
94 | |||
95 | // Set up relationships |
||
96 | objects.each(function init() { |
||
97 | var object = $(this), |
||
98 | relation = object.attr('id') || object.attr('name'); |
||
99 | |||
100 | object.find('option').each(function() { |
||
101 | $('#' + $(this).val()).attr('data-relation', relation); |
||
102 | }); |
||
103 | }); |
||
104 | |||
105 | // Show picked content |
||
106 | objects.trigger('change.pickable'); |
||
107 | |||
108 | /*-----------------------------------------------------------------------*/ |
||
109 | |||
110 | return objects; |
||
111 | }; |
||
112 | |||
113 | })(window.jQuery, window.Symphony); |
||
114 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.