| Conditions | 1 |
| Paths | 16 |
| Total Lines | 268 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 | /* |
||
| 25 | (function() { |
||
| 26 | /** |
||
| 27 | * @class OCA.Circles.FileList |
||
| 28 | * @augments OCA.Files.FileList |
||
| 29 | * |
||
| 30 | * @classdesc Circles file list. |
||
| 31 | * Contains a list of files filtered by circles |
||
| 32 | * |
||
| 33 | * @param $el container element with existing markup for the #controls |
||
| 34 | * and a table |
||
| 35 | * @param [options] map of options, see other parameters |
||
| 36 | * @param {Array.<string>} [options.circlesIds] array of system tag ids to |
||
| 37 | * filter by |
||
| 38 | */ |
||
| 39 | var FileList = function($el, options) { |
||
| 40 | this.initialize($el, options); |
||
| 41 | }; |
||
| 42 | FileList.prototype = _.extend({}, OCA.Files.FileList.prototype, |
||
| 43 | /** @lends OCA.Circles.FileList.prototype */ { |
||
| 44 | id: 'circlesfilter', |
||
| 45 | appName: t('circles', 'Circles\' files'), |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Array of system tag ids to filter by |
||
| 49 | * |
||
| 50 | * @type Array.<string> |
||
| 51 | */ |
||
| 52 | _circlesIds: [], |
||
| 53 | _lastUsedTags: [], |
||
| 54 | |||
| 55 | _clientSideSort: true, |
||
| 56 | _allowSelection: false, |
||
| 57 | |||
| 58 | _filterField: null, |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @private |
||
| 62 | */ |
||
| 63 | initialize: function($el, options) { |
||
| 64 | OCA.Files.FileList.prototype.initialize.apply(this, arguments); |
||
| 65 | if (this.initialized) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | |||
| 69 | if (options && options.circlesIds) { |
||
| 70 | this._circlesIds = options.circlesIds; |
||
| 71 | } |
||
| 72 | |||
| 73 | OC.Plugins.attach('OCA.Circles.FileList', this); |
||
| 74 | |||
| 75 | var $controls = this.$el.find('#controls').empty(); |
||
| 76 | |||
| 77 | this._initFilterField($controls); |
||
| 78 | }, |
||
| 79 | |||
| 80 | destroy: function() { |
||
| 81 | this.$filterField.remove(); |
||
| 82 | |||
| 83 | OCA.Files.FileList.prototype.destroy.apply(this, arguments); |
||
| 84 | }, |
||
| 85 | |||
| 86 | _initFilterField: function($container) { |
||
| 87 | var self = this; |
||
| 88 | this.$filterField = $('<input type="hidden" name="circles"/>'); |
||
| 89 | $container.append(this.$filterField); |
||
| 90 | this.$filterField.select2({ |
||
| 91 | placeholder: t('circles', 'Select circles to filter by'), |
||
| 92 | allowClear: false, |
||
| 93 | multiple: true, |
||
| 94 | toggleSelect: true, |
||
| 95 | separator: ',', |
||
| 96 | query: _.bind(this._queryCirclesAutocomplete, this), |
||
| 97 | |||
| 98 | id: function(circle) { |
||
| 99 | return circle.unique_id; |
||
| 100 | }, |
||
| 101 | |||
| 102 | initSelection: function(element, callback) { |
||
| 103 | var val = $(element).val().trim(); |
||
| 104 | if (val) { |
||
| 105 | var circleIds = val.split(','), |
||
| 106 | circles = []; |
||
| 107 | |||
| 108 | OCA.Circles.api.listCircles("all", '', 1, function(result) { |
||
| 109 | _.each(circleIds, function(circleId) { |
||
| 110 | var circle = _.find(result.data,function(circleData) { |
||
| 111 | return circleData.unique_id == circleId; |
||
| 112 | }); |
||
| 113 | if (!_.isUndefined(circle)) { |
||
| 114 | circles.push(circle); |
||
| 115 | } |
||
| 116 | }); |
||
| 117 | |||
| 118 | callback(circles); |
||
| 119 | }); |
||
| 120 | |||
| 121 | } else { |
||
| 122 | callback([]); |
||
| 123 | } |
||
| 124 | }, |
||
| 125 | |||
| 126 | formatResult: function (circle) { |
||
| 127 | return circle.name; |
||
| 128 | }, |
||
| 129 | |||
| 130 | formatSelection: function (circle) { |
||
| 131 | return circle.name; |
||
| 132 | //return OC.SystemTags.getDescriptiveTag(tag)[0].outerHTML; |
||
| 133 | }, |
||
| 134 | |||
| 135 | sortResults: function(results) { |
||
| 136 | results.sort(function(a, b) { |
||
| 137 | var aLastUsed = self._lastUsedTags.indexOf(a.id); |
||
| 138 | var bLastUsed = self._lastUsedTags.indexOf(b.id); |
||
| 139 | |||
| 140 | if (aLastUsed !== bLastUsed) { |
||
| 141 | if (bLastUsed === -1) { |
||
| 142 | return -1; |
||
| 143 | } |
||
| 144 | if (aLastUsed === -1) { |
||
| 145 | return 1; |
||
| 146 | } |
||
| 147 | return aLastUsed < bLastUsed ? -1 : 1; |
||
| 148 | } |
||
| 149 | |||
| 150 | // Both not found |
||
| 151 | return OC.Util.naturalSortCompare(a.name, b.name); |
||
| 152 | }); |
||
| 153 | return results; |
||
| 154 | }, |
||
| 155 | |||
| 156 | escapeMarkup: function(m) { |
||
| 157 | // prevent double markup escape |
||
| 158 | return m; |
||
| 159 | }, |
||
| 160 | formatNoMatches: function() { |
||
| 161 | return t('systemtags', 'No circles found'); |
||
| 162 | } |
||
| 163 | }); |
||
| 164 | this.$filterField.on('change', _.bind(this._onTagsChanged, this)); |
||
| 165 | return this.$filterField; |
||
| 166 | }, |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Autocomplete function for dropdown results |
||
| 170 | * |
||
| 171 | * @param {Object} query select2 query object |
||
| 172 | */ |
||
| 173 | _queryCirclesAutocomplete: function(query) { |
||
| 174 | |||
| 175 | OCA.Circles.api.listCircles("all", query.term, 1, function(result) { |
||
| 176 | query.callback({ |
||
| 177 | results: result.data |
||
| 178 | }); |
||
| 179 | }); |
||
| 180 | /* |
||
| 181 | OC.SystemTags.collection.fetch({ |
||
| 182 | success: function() { |
||
| 183 | var results = OC.SystemTags.collection.filterByName(query.term); |
||
| 184 | |||
| 185 | query.callback({ |
||
| 186 | results: _.invoke(results, 'toJSON') |
||
| 187 | }); |
||
| 188 | } |
||
| 189 | }); |
||
| 190 | */ |
||
| 191 | }, |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Event handler for when the URL changed |
||
| 195 | */ |
||
| 196 | _onUrlChanged: function(e) { |
||
| 197 | if (e.dir) { |
||
| 198 | var circles = _.filter(e.dir.split('/'), function(val) { return val.trim() !== ''; }); |
||
| 199 | this.$filterField.select2('val', circles || []); |
||
| 200 | this._circlesIds = circles; |
||
| 201 | this.reload(); |
||
| 202 | } |
||
| 203 | }, |
||
| 204 | |||
| 205 | _onTagsChanged: function(ev) { |
||
| 206 | var val = $(ev.target).val().trim(); |
||
| 207 | if (val !== '') { |
||
| 208 | this._circlesIds = val.split(','); |
||
| 209 | } else { |
||
| 210 | this._circlesIds = []; |
||
| 211 | } |
||
| 212 | |||
| 213 | this.$el.trigger(jQuery.Event('changeDirectory', { |
||
| 214 | dir: this._circlesIds.join('/') |
||
| 215 | })); |
||
| 216 | this.reload(); |
||
| 217 | }, |
||
| 218 | |||
| 219 | updateEmptyContent: function() { |
||
| 220 | var dir = this.getCurrentDirectory(); |
||
| 221 | if (dir === '/') { |
||
| 222 | // root has special permissions |
||
| 223 | if (!this._circlesIds.length) { |
||
| 224 | // no tags selected |
||
| 225 | this.$el.find('#emptycontent').html('<div class="icon-systemtags"></div>' + |
||
| 226 | '<h2>' + t('systemtags', 'Please select circles to filter by') + '</h2>'); |
||
| 227 | } else { |
||
| 228 | // tags selected but no results |
||
| 229 | this.$el.find('#emptycontent').html('<div class="icon-systemtags"></div>' + |
||
| 230 | '<h2>' + t('systemtags', 'No files found for the selected circles') + '</h2>'); |
||
| 231 | } |
||
| 232 | this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty); |
||
| 233 | this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty); |
||
| 234 | } |
||
| 235 | else { |
||
| 236 | OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments); |
||
| 237 | } |
||
| 238 | }, |
||
| 239 | |||
| 240 | getDirectoryPermissions: function() { |
||
| 241 | return OC.PERMISSION_READ | OC.PERMISSION_DELETE; |
||
| 242 | }, |
||
| 243 | |||
| 244 | updateStorageStatistics: function() { |
||
| 245 | // no op because it doesn't have |
||
| 246 | // storage info like free space / used space |
||
| 247 | }, |
||
| 248 | |||
| 249 | reload: function() { |
||
| 250 | if (!this._circlesIds.length) { |
||
| 251 | // don't reload |
||
| 252 | this.updateEmptyContent(); |
||
| 253 | this.setFiles([]); |
||
| 254 | return $.Deferred().resolve(); |
||
| 255 | } |
||
| 256 | |||
| 257 | this._selectedFiles = {}; |
||
| 258 | this._selectionSummary.clear(); |
||
| 259 | if (this._currentFileModel) { |
||
| 260 | this._currentFileModel.off(); |
||
| 261 | } |
||
| 262 | this._currentFileModel = null; |
||
| 263 | this.$el.find('.select-all').prop('checked', false); |
||
| 264 | this.showMask(); |
||
| 265 | this._reloadCall = this.filesClient.getFilteredFiles( |
||
| 266 | { |
||
| 267 | circlesIds: this._circlesIds |
||
| 268 | }, |
||
| 269 | { |
||
| 270 | properties: this._getWebdavProperties() |
||
| 271 | } |
||
| 272 | ); |
||
| 273 | if (this._detailsView) { |
||
| 274 | // close sidebar |
||
| 275 | this._updateDetailsView(null); |
||
| 276 | } |
||
| 277 | var callBack = this.reloadCallback.bind(this); |
||
| 278 | return this._reloadCall.then(callBack, callBack); |
||
| 279 | }, |
||
| 280 | |||
| 281 | reloadCallback: function(status, result) { |
||
| 282 | if (result) { |
||
| 283 | // prepend empty dir info because original handler |
||
| 284 | result.unshift({}); |
||
| 285 | } |
||
| 286 | |||
| 287 | return OCA.Files.FileList.prototype.reloadCallback.call(this, status, result); |
||
| 288 | } |
||
| 289 | }); |
||
| 290 | |||
| 291 | OCA.Circles.FileList = FileList; |
||
| 292 | })(); |
||
| 293 | |||
| 294 |
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.