diff --git a/horizon/karma.conf.js b/horizon/karma.conf.js index 27f6248bc3..6b6c9db933 100644 --- a/horizon/karma.conf.js +++ b/horizon/karma.conf.js @@ -150,7 +150,7 @@ module.exports = function (config) { statements: 91, // target 100 branches: 81, // target 100 functions: 90, // target 100 - lines: 91 // target 100 + lines: 92 // target 100 } }); }; diff --git a/horizon/static/framework/util/file/file-reader.service.js b/horizon/static/framework/util/file/file-reader.service.js new file mode 100644 index 0000000000..675fd77458 --- /dev/null +++ b/horizon/static/framework/util/file/file-reader.service.js @@ -0,0 +1,62 @@ +/* + * Copyright 2015 Hewlett Packard Enterprise Development Company LP + * + * 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.file') + .factory('horizon.framework.util.file.file-reader', + fileReaderService); + + fileReaderService.$inject = ['$q']; + + /** + * @ngdoc service + * @name horizon.framework.util.file.fileReaderService + * @description + * Service for reading file contents. Used for processing client-side + * files, such as loading a config file into launch instance. + * + * readTextFile - reads a text file and returns a promise of its contents. + */ + function fileReaderService($q) { + var service = { + readTextFile: readTextFile + }; + + return service; + + //////////////// + + function readTextFile(file, fileReader) { + var reader = fileReader || new FileReader(); + var deferred = $q.defer(); + reader.onloadend = loadEnd; + reader.readAsArrayBuffer(file.slice(0, file.size)); + return deferred.promise; + + function loadEnd(e) { + var charArray = new Uint8Array(e.target.result); + var textContent = [].map.call(charArray, strFromCharCode).join(''); + deferred.resolve(textContent); + + function strFromCharCode(char) { + return String.fromCharCode(char); + } + } + } + } +})(); diff --git a/horizon/static/framework/util/file/file-reader.service.spec.js b/horizon/static/framework/util/file/file-reader.service.spec.js new file mode 100644 index 0000000000..95a057b448 --- /dev/null +++ b/horizon/static/framework/util/file/file-reader.service.spec.js @@ -0,0 +1,54 @@ +/* + * 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'; + + describe('fileReader service', function() { + var $scope, fileReader; + + beforeEach(module('horizon.framework.util.file')); + beforeEach(inject(function($injector) { + fileReader = $injector.get('horizon.framework.util.file.file-reader'); + $scope = $injector.get('$rootScope'); + })); + + it('should return a promise that resolves to a string', function(done) { + var fileReaderStub = {}; + var arrayBuffer = str2ab('file contents'); + fileReaderStub.readAsArrayBuffer = function() {}; + + var filePromise = fileReader.readTextFile('file', fileReaderStub); + filePromise.then(function(contents) { + expect(contents).toEqual('file contents'); + done(); + }); + + fileReaderStub.onloadend({ + target: { + result: arrayBuffer + } + }); + $scope.$apply(); + }); + }); + + function str2ab(str) { + var buf = new ArrayBuffer(str.length); // 2 bytes for each char + var bufView = new Uint8Array(buf); + for (var i = 0, strLen = str.length; i < strLen; i++) { + bufView[i] = str.charCodeAt(i); + } + return buf; + } +})(); diff --git a/horizon/static/framework/util/file/file.module.js b/horizon/static/framework/util/file/file.module.js new file mode 100644 index 0000000000..b6a9063010 --- /dev/null +++ b/horizon/static/framework/util/file/file.module.js @@ -0,0 +1,22 @@ +/* + * Copyright 2015 Hewlett Packard Enterprise Development Company LP + * + * 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.file', []); + +})(); diff --git a/horizon/static/framework/util/util.module.js b/horizon/static/framework/util/util.module.js index 51da4e2be3..5bf2dbe1ac 100644 --- a/horizon/static/framework/util/util.module.js +++ b/horizon/static/framework/util/util.module.js @@ -4,6 +4,7 @@ angular .module('horizon.framework.util', [ 'horizon.framework.util.bind-scope', + 'horizon.framework.util.file', 'horizon.framework.util.filters', 'horizon.framework.util.http', 'horizon.framework.util.i18n', diff --git a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/configuration/load-edit.directive.js b/horizon/static/framework/widgets/load-edit/load-edit.directive.js similarity index 69% rename from openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/configuration/load-edit.directive.js rename to horizon/static/framework/widgets/load-edit/load-edit.directive.js index 2a2d0d42f5..8e4ebf382b 100644 --- a/openstack_dashboard/dashboards/project/static/dashboard/project/workflow/launch-instance/configuration/load-edit.directive.js +++ b/horizon/static/framework/widgets/load-edit/load-edit.directive.js @@ -1,4 +1,6 @@ /* + * Copyright 2015 Hewlett Packard Enterprise Development Company LP + * * 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 @@ -14,9 +16,9 @@ (function () { 'use strict'; - /* + /** * @ngdoc directive - * @name horizon.dashboard.project.workflow.launch-instance:loadEdit + * @name horizon.framework.widgets:loadEdit * @scope * @element * @description @@ -29,15 +31,16 @@ * See configuration.html for example usage. */ angular - .module('horizon.dashboard.project.workflow.launch-instance') + .module('horizon.framework.widgets.load-edit') .directive('loadEdit', loadEdit); loadEdit.$inject = [ - 'horizon.dashboard.project.workflow.launch-instance.basePath', - '$timeout' + '$timeout', + 'horizon.framework.util.file.file-reader', + 'horizon.framework.widgets.load-edit.basePath' ]; - function loadEdit(basePath, $timeout) { + function loadEdit($timeout, fileReader, basePath) { var directive = { restrict: 'E', scope: { @@ -46,7 +49,7 @@ key: '@' }, link: link, - templateUrl: basePath + 'configuration/load-edit.html' + templateUrl: basePath + 'load-edit.html' }; return directive; @@ -94,34 +97,25 @@ var file = event.originalEvent.target.files[0]; if (file) { - var reader = new FileReader(); - - reader.onloadend = function (e) { - $scope.$applyAsync(function () { - var charArray = new Uint8Array(e.target.result); - - $scope.textContent = [].map.call(charArray, - function (char) { - return String.fromCharCode(char); - } - ).join(''); - }); - }; - - reader.readAsArrayBuffer(file.slice(0, file.size)); - - /* Once the DOM manipulation is done, update the scriptLength, so that - * user knows the length of the script loaded into the