diff --git a/horizon/karma.conf.js b/horizon/karma.conf.js index 434fbb1d9..b8eeed9bc 100644 --- a/horizon/karma.conf.js +++ b/horizon/karma.conf.js @@ -82,6 +82,9 @@ module.exports = function (config) { xstaticPath + 'angular_fileupload/data/ng-file-upload-all.js', xstaticPath + 'spin/data/spin.js', xstaticPath + 'spin/data/spin.jquery.js', + xstaticPath + 'tv4/data/tv4.js', + xstaticPath + 'objectpath/data/ObjectPath.js', + xstaticPath + 'angular_schema_form/data/schema-form.js', // from jasmine_tests.py; only those that are deps for others 'horizon/js/horizon.js', diff --git a/horizon/static/framework/util/util.module.js b/horizon/static/framework/util/util.module.js index 6d95727f4..4de682f07 100644 --- a/horizon/static/framework/util/util.module.js +++ b/horizon/static/framework/util/util.module.js @@ -12,6 +12,7 @@ 'horizon.framework.util.promise-toggle', 'horizon.framework.util.q', 'horizon.framework.util.tech-debt', + 'horizon.framework.util.uuid', 'horizon.framework.util.workflow', 'horizon.framework.util.validators', 'horizon.framework.util.extensible' diff --git a/horizon/static/framework/util/uuid/uuid.js b/horizon/static/framework/util/uuid/uuid.js new file mode 100644 index 000000000..a728934f5 --- /dev/null +++ b/horizon/static/framework/util/uuid/uuid.js @@ -0,0 +1,45 @@ +/* + * (c) Copyright 2016 Cisco Systems + * + * 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.uuid', []) + .factory('horizon.framework.util.uuid.service', uuidService); + + /** + * @name horizon.framework.util.uuid + * @description + * Generates a UUID. This is useful for ensuring HTML components + * have unique IDs for interactions. + */ + function uuidService() { + var service = { + generate: generate + }; + return service; + + function generate() { + var d = new Date().getTime(); + var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = (d + Math.random() * 16) % 16 | 0; + d = Math.floor(d / 16); + return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); + }); + return uuid; + } + } +})(); diff --git a/horizon/static/framework/util/uuid/uuid.spec.js b/horizon/static/framework/util/uuid/uuid.spec.js new file mode 100644 index 000000000..5b5729439 --- /dev/null +++ b/horizon/static/framework/util/uuid/uuid.spec.js @@ -0,0 +1,70 @@ +/* + * (c) Copyright 2016 Cisco Systems + * + * 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('horizon.framework.util.uuid module', function() { + it('should have been defined', function () { + expect(angular.module('horizon.framework.util.uuid')).toBeDefined(); + }); + }); + + describe('uuid', function () { + var uuid; + + beforeEach(module('horizon.framework')); + beforeEach(inject(function ($injector) { + uuid = $injector.get('horizon.framework.util.uuid.service'); + })); + + it('should be defined', function () { + expect(uuid).toBeDefined(); + }); + + it('should generate multiple unique IDs', function() { + var unique = []; + var ids = []; + var i, j, potentialUUID, uniqueLen, isUnique; + + // Generate 10 IDs + for (i = 0; i < 10; i += 1) { + ids.push(uuid.generate()); + } + + // Check that the IDs are unique + // Iterate through the IDs, check that it isn't part of our unique list, + // then append + for (i -= 1; i >= 0; i -= 1) { + potentialUUID = ids[i]; + isUnique = true; + for (j = 0, uniqueLen = unique.length; j < uniqueLen; j += 1) { + if (potentialUUID === unique[j]) { + isUnique = false; + } + } + if (isUnique) { + unique.push(potentialUUID); + } + } + + // Reverse the array, because Jasmine's "toEqual" won't work otherwise. + unique.reverse(); + + expect(ids).toEqual(unique); + }); + }); +}()); diff --git a/horizon/static/framework/widgets/form/builders.provider.js b/horizon/static/framework/widgets/form/builders.provider.js new file mode 100644 index 000000000..a502ff403 --- /dev/null +++ b/horizon/static/framework/widgets/form/builders.provider.js @@ -0,0 +1,66 @@ +/** + * (c) Copyright 2016 Cisco Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use self 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('schemaForm') + .provider('hzBuilder', provider); + + /** + * @ngDoc provider + * @name horizon.framework.widgets.form.builders + */ + function provider() { + var builders = { + tabsBuilder: tabsBuilder + }; + + this.$get = function() { + return builders; + }; + + function tabsBuilder(args) { + if (args.form.tabs && args.form.tabs.length > 0) { + var tabLi = args.fieldFrag.querySelector('li'); + /* eslint-disable max-len */ + tabLi.setAttribute('ng-if', '!tab.condition ? true : evalExpr(tab.condition, { model: model, "arrayIndex": $index })'); + /* eslint-enable max-len */ + var tabContent = args.fieldFrag.querySelector('.tab-content'); + + args.form.tabs.forEach(function(tab, index) { + tab.items.forEach(function(item) { + if (item.required) { + tab.required = true; + } + }); + var div = document.createElement('div'); + div.setAttribute('ng-show', 'model.tabs.selected === ' + index); + div.setAttribute('ng-if', tab.condition || true); + + var childFrag = args.build( + tab.items, + args.path + '.tabs[' + index + '].items', + args.state + ); + div.appendChild(childFrag); + tabContent.appendChild(div); + }); + } + } + } +})(); diff --git a/horizon/static/framework/widgets/form/builders.provider.spec.js b/horizon/static/framework/widgets/form/builders.provider.spec.js new file mode 100644 index 000000000..7ebaec27e --- /dev/null +++ b/horizon/static/framework/widgets/form/builders.provider.spec.js @@ -0,0 +1,64 @@ +/* + * (c) Copyright 2016 Cisco Systems + * + * 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('hzBuilderProvider', function() { + var provider, $compile, $scope, args, element, elementTwo; + + var html = "