JSCS Cleanup - horizon/ tech-debt

Refactor codebase to follow JP styleguide using eslint.

Use Functions with DI for Controllers.
Use this instead of $scope.
Name the files as per convention.
Add Apache License for the files.

Change-Id: I0ce1156c036a6175bc96892cbcf0211d8712d7b3
Partially-Implements: blueprint jscs-cleanup
This commit is contained in:
Nathan Zeplowitz 2015-07-07 16:38:32 -07:00 committed by Rajat Vig
parent 5796aa97a7
commit 1461b920c9
12 changed files with 222 additions and 114 deletions

View File

@ -0,0 +1,22 @@
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
'use strict';
angular
.module('horizon.framework.util.tech-debt')
.controller('DummyController', angular.noop);
})();

View File

@ -1,5 +0,0 @@
(function () {
'use strict';
angular.module('horizon.framework.util.tech-debt')
.controller('DummyCtrl', function () {});
}());

View File

@ -1,59 +0,0 @@
(function () {
'use strict';
angular
.module('horizon.framework.util.tech-debt')
.factory('horizon.framework.util.tech-debt.helper-functions', utils);
// An example of using the John Papa recommended $inject instead of in-line
// array syntax
utils.$inject = ['$rootScope', '$compile'];
function utils($rootScope, $compile) {
return {
capitalize: function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
/*
Adds commas to any integer or numbers within a string for human display.
EG:
horizon.utils.humanizeNumbers(1234); -> "1,234"
horizon.utils.humanizeNumbers("My Total: 1234"); -> "My Total: 1,234"
*/
humanizeNumbers: function (number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
},
/*
Truncate a string at the desired length. Optionally append an ellipsis
to the end of the string.
EG:
horizon.utils.truncate("String that is too long.", 18, true); ->
"String that is too…"
*/
truncate: function (string, size, includeEllipsis) {
if (string.length > size) {
if (includeEllipsis) {
return string.substring(0, (size - 3)) + "…";
}
return string.substring(0, size);
}
return string;
},
loadAngular: function (element) {
try {
$compile(element)($rootScope);
$rootScope.$apply();
} catch (err) {}
/*
Compilation fails when it could not find a directive,
fails silently on this, it is an angular behaviour.
*/
}
};
}
}());

View File

@ -0,0 +1,79 @@
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
'use strict';
angular
.module('horizon.framework.util.tech-debt')
.factory('horizon.framework.util.tech-debt.helper-functions', helperFunctionsService);
helperFunctionsService.$inject = ['$rootScope', '$compile'];
function helperFunctionsService($rootScope, $compile) {
var service = {
capitalize: capitalize,
humanizeNumbers: humanizeNumbers,
truncate: truncate,
loadAngular: loadAngular
};
return service;
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/*
* Adds commas to any integer or numbers within a string for human display.
*
* Example:
* horizon.utils.humanizeNumbers(1234); -> "1,234"
* horizon.utils.humanizeNumbers("My Total: 1234"); -> "My Total: 1,234"
*
*/
function humanizeNumbers(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
/*
* Truncate a string at the desired length. Optionally append an ellipsis
* to the end of the string.
*
* Example:
* horizon.utils.truncate("String that is too long.", 18, true); ->
* "String that is too…"
*
*/
function truncate(string, size, includeEllipsis) {
if (string.length > size) {
if (includeEllipsis) {
return string.substring(0, (size - 3)) + '…';
}
return string.substring(0, size);
}
return string;
}
function loadAngular(element) {
try {
$compile(element)($rootScope);
$rootScope.$apply();
} catch (err) {}
}
}
})();

View File

@ -1,3 +1,17 @@
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function() {
'use strict';

View File

@ -0,0 +1,40 @@
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
'use strict';
angular
.module('horizon.framework.util.tech-debt')
.directive('imageFileOnChange', imageFileOnChange);
function imageFileOnChange() {
var directive = {
require: 'ngModel',
restrict: 'A',
link: link
};
return directive;
function link($scope, element, attrs, ngModel) {
element.bind('change', function (event) {
var files = event.target.files;
var file = files[0];
ngModel.$setViewValue(file);
$scope.$apply();
});
}
}
})();

View File

@ -1,21 +0,0 @@
(function () {
'use strict';
angular
.module('horizon.framework.util.tech-debt')
.directive('imageFileOnChange', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function ($scope, element, attrs, ngModel) {
element.bind('change', function (event) {
var files = event.target.files;
var file = files[0];
ngModel.$setViewValue(file);
$scope.$apply();
});
}
};
});
}());

View File

@ -1,25 +1,48 @@
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
'use strict';
angular
.module('horizon.framework.util.tech-debt')
.controller('hzModalFormUpdateMetadataCtrl', [
'$scope', '$window',
function ($scope, $window) {
$scope.tree = null;
$scope.available = $window.available_metadata.namespaces;
$scope.existing = $window.existing_metadata;
.controller('hzModalFormUpdateMetadataController', hzModalFormUpdateMetadataController);
hzModalFormUpdateMetadataController.$inject = ['$scope', '$window'];
function hzModalFormUpdateMetadataController($scope, $window) {
var ctrl = this;
ctrl.tree = null;
ctrl.available = $window.available_metadata.namespaces;
ctrl.existing = $window.existing_metadata;
ctrl.saveMetadata = function () {
var metadata = [];
angular.forEach(ctrl.tree.getExisting(), function (value, key) {
metadata.push({
key: key,
value: value
});
});
ctrl.metadata = angular.toJson(metadata);
};
}
$scope.saveMetadata = function () {
var metadata = [];
angular.forEach($scope.tree.getExisting(), function (value, key) {
metadata.push({
key: key,
value: value
});
});
$scope.metadata = JSON.stringify(metadata);
};
}
]);
}());

View File

@ -1,6 +1,21 @@
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
'use strict';
angular.module('horizon.framework.util.tech-debt', []);
angular
.module('horizon.framework.util.tech-debt', []);
}());
})();

View File

@ -7,7 +7,7 @@
<hr>
{% endif %}
<form id="{% block form_id %}{{ form_id }}{% endblock %}"
ng-controller="{% block ng_controller %}DummyCtrl{% endblock %}"
ng-controller="{% block ng_controller %}DummyController{% endblock %}"
name="{% block form_name %}{% endblock %}"
autocomplete="{% block autocomplete %}{% if form.no_autocomplete %}off{% endif %}{% endblock %}"
class="{% block form_class %}{% endblock %}"

View File

@ -4,12 +4,12 @@
{% block form_name %}metadataForm{% endblock %}
{% block form_validation %}novalidate{% endblock %}
{% block ng_controller %}hzModalFormUpdateMetadataCtrl{% endblock %}
{% block ng_controller %}hzModalFormUpdateMetadataController as metadataCtrl{% endblock %}
{% block modal-body %}
<metadata-tree available="available"
existing="existing"
model="tree"></metadata-tree>
<metadata-tree available="metadataCtrl.available"
existing="metadataCtrl.existing"
model="metadataCtrl.tree"></metadata-tree>
<script type="text/javascript">
var existing_metadata = JSON.parse('{{ existing_metadata|escapejs }}');
var available_metadata = JSON.parse('{{ available_metadata|escapejs }}');
@ -20,10 +20,10 @@
<div>
<input class="btn btn-primary pull-right"
ng-disabled="metadataForm.$invalid"
ng-click="saveMetadata()" type="submit"
ng-click="metadataCtrl.saveMetadata()" type="submit"
value="{% trans "Save" %}"/>
<a class="btn btn-default secondary cancel close">{% trans "Cancel" %}</a>
<input type="hidden" name="metadata" ng-value="metadata"
ng-model="metadata">
<input type="hidden" name="metadata" ng-value="metadataCtrl.metadata"
ng-model="metadataCtrl.metadata">
</div>
{% endblock %}

View File

@ -3,7 +3,7 @@
{% load url from future %}
{% block form_id %}update_object_form{% endblock %}
{% block ng_controller %}DummyCtrl{% endblock %}
{% block ng_controller %}DummyController{% endblock %}
{% block form_name %}updateForm{% endblock %}
{% block form_action %}{% url 'horizon:project:containers:object_update' container_name subfolder_path object_path %}{% endblock %}
{% block form_attrs %}enctype="multipart/form-data"{% endblock %}