From a710723c227868f7dbe94cdcbe15b85ecdb90c61 Mon Sep 17 00:00:00 2001 From: Sahar Jafari Date: Wed, 30 Mar 2016 20:04:10 -0700 Subject: [PATCH] fix all errors in test/ --- .eslintrc.js | 8 +- package.json | 8 -- scripts/controllers/header.js | 104 ++++++++++---------- scripts/controllers/import-file.js | 16 +-- scripts/controllers/importurl.js | 12 ++- scripts/controllers/main.js | 41 ++++---- scripts/controllers/paste-json.js | 9 +- scripts/controllers/preferences.js | 3 +- scripts/controllers/preview.js | 30 +++--- scripts/controllers/security.js | 10 +- scripts/directives/auto-focus.js | 6 +- scripts/directives/collapsewhen.js | 12 +-- scripts/directives/on-file-change.js | 10 +- scripts/directives/operation.js | 16 +-- scripts/directives/schemamodel.js | 17 ++-- scripts/directives/scroll-into-view-when.js | 6 +- scripts/directives/stop-event.js | 6 +- scripts/directives/track-event.js | 8 +- scripts/directives/tryoperation.js | 75 +++++++------- test/.eslintrc.js | 5 +- 20 files changed, 192 insertions(+), 210 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 76884f54f1c..559ffd34ea3 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,3 +1,9 @@ module.exports = { - extends: 'google' + extends: 'google', + globals: { + SwaggerEditor: false + }, + env: { + browser: true + } }; diff --git a/package.json b/package.json index c8b5094df5f..845bfb73642 100644 --- a/package.json +++ b/package.json @@ -85,14 +85,6 @@ "worker-loader": "^0.7.0", "yaml-js": "^0.1.3" }, - "eslintConfig": { - "globals": { - "window": true - }, - "env": { - "browser": true - } - }, "engines": { "node": ">=5.8.0" }, diff --git a/scripts/controllers/header.js b/scripts/controllers/header.js index 743a8c577f0..eb8aac49415 100644 --- a/scripts/controllers/header.js +++ b/scripts/controllers/header.js @@ -3,15 +3,14 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, $stateParams, $state, $rootScope, Storage, Builder, FileLoader, Editor, Codegen, Preferences, YAML, defaults, strings, $localStorage) { - if ($stateParams.path) { - $scope.breadcrumbs = [{ active: true, name: $stateParams.path }]; + $scope.breadcrumbs = [{active: true, name: $stateParams.path}]; } else { - $scope.breadcrumbs = []; + $scope.breadcrumbs = []; } // var statusTimeout; - $rootScope.$watch('progressStatus', function (progressStatus) { + $rootScope.$watch('progressStatus', function(progressStatus) { var status = strings.stausMessages[progressStatus]; var statusClass = null; @@ -41,20 +40,20 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, $scope.disableCodeGen = defaults.disableCodeGen; if (!defaults.disableCodeGen) { - Codegen.getServers().then(function (servers) { + Codegen.getServers().then(function(servers) { $scope.servers = servers; - }, function () { + }, function() { $scope.serversNotAvailable = true; }); - Codegen.getClients().then(function (clients) { + Codegen.getClients().then(function(clients) { $scope.clients = clients; - }, function () { + }, function() { $scope.clientsNotAvailable = true; }); } - $scope.getSDK = function (type, language) { + $scope.getSDK = function(type, language) { Codegen.getSDK(type, language).then(noop, showCodegenError); }; @@ -64,7 +63,7 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, controller: 'GeneralModal', size: 'large', resolve: { - data: function () { + data: function() { if (resp.data) { return resp.data; } @@ -75,28 +74,28 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, }); } - $scope.showFileMenu = function () { + $scope.showFileMenu = function() { return !defaults.disableFileMenu; }; - $scope.showHeaderBranding = function () { + $scope.showHeaderBranding = function() { return defaults.headerBranding; }; - $scope.newProject = function () { - FileLoader.loadFromUrl('spec-files/guide.yaml').then(function (value) { + $scope.newProject = function() { + FileLoader.loadFromUrl('spec-files/guide.yaml').then(function(value) { $rootScope.editorValue = value; Storage.save('yaml', value); $state.go('home', {tags: null}); }); }; - $scope.onFileMenuOpen = function () { + $scope.onFileMenuOpen = function() { assignDownloadHrefs(); $rootScope.$broadcast('toggleWatchers', false); }; - $scope.openImportFile = function () { + $scope.openImportFile = function() { $uibModal.open({ templateUrl: 'templates/file-import.html', controller: 'FileImportCtrl', @@ -104,7 +103,7 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, }); }; - $scope.openImportUrl = function () { + $scope.openImportUrl = function() { $uibModal.open({ templateUrl: 'templates/url-import.html', controller: 'UrlImportCtrl', @@ -112,7 +111,7 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, }); }; - $scope.openPasteJSON = function () { + $scope.openPasteJSON = function() { $uibModal.open({ templateUrl: 'templates/paste-json.html', controller: 'PasteJSONCtrl', @@ -120,7 +119,7 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, }); }; - $scope.openAbout = function () { + $scope.openAbout = function() { $uibModal.open({ templateUrl: 'templates/about.html', size: 'large', @@ -128,7 +127,7 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, }); }; - $rootScope.toggleAboutEditor = function (value) { + $rootScope.toggleAboutEditor = function(value) { $rootScope.showAbout = value; $localStorage.showIntro = value; }; @@ -137,7 +136,7 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, $scope.resetSettings = Editor.resetSettings; $scope.adjustFontSize = Editor.adjustFontSize; - $scope.openExamples = function () { + $scope.openExamples = function() { $uibModal.open({ templateUrl: 'templates/open-examples.html', controller: 'OpenExamplesCtrl', @@ -145,7 +144,7 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, }); }; - $scope.openPreferences = function () { + $scope.openPreferences = function() { $uibModal.open({ templateUrl: 'templates/preferences.html', controller: 'PreferencesCtrl', @@ -153,16 +152,15 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, }); }; - $scope.isLiveRenderEnabled = function () { - return !!Preferences.get('liveRender'); + $scope.isLiveRenderEnabled = function() { + return Boolean(Preferences).get('liveRender'); }; function assignDownloadHrefs() { var MIME_TYPE = 'text/plain'; var yaml = $rootScope.editorValue; - YAML.load(yaml, function (error, json) { - + YAML.load(yaml, function(error, json) { // Don't assign if there is an error if (error) { return; @@ -176,15 +174,7 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, jsonParseError = error; } - if (!jsonParseError) { - YAML.dump(json, function (error, yamlStr) { - assign(yamlStr, json); - }); - } else { - assign(yaml, json); - } - - function assign(yaml, json) { + var assign = function (yaml, json) { // swagger and version should be a string to comfort with the schema if (json.info.version) { json.info.version = String(json.info.version); @@ -214,39 +204,47 @@ SwaggerEditor.controller('HeaderCtrl', function HeaderCtrl($scope, $uibModal, 'swagger.yaml', $scope.yamlDownloadHref ].join(':'); + }; + + if (jsonParseError) { + assign(yaml, json); + } else { + YAML.dump(json, function(error, yamlStr) { + assign(yamlStr, json); + }); } }); } - $scope.capitalizeGeneratorName = function (name) { + $scope.capitalizeGeneratorName = function(name) { var names = { - jaxrs: 'JAX-RS', - nodejs: 'Node.js', - scalatra: 'Scalatra', + 'jaxrs': 'JAX-RS', + 'nodejs': 'Node.js', + 'scalatra': 'Scalatra', 'spring-mvc': 'Spring MVC', - android: 'Android', + 'android': 'Android', 'async-scala': 'Async Scala', - csharp: 'C#', - CsharpDotNet2: 'C# .NET 2.0', - qt5cpp: 'Qt 5 C++', - java: 'Java', - objc: 'Objective-C', - php: 'PHP', - python: 'Python', - ruby: 'Ruby', - scala: 'Scala', + 'csharp': 'C#', + 'CsharpDotNet2': 'C# .NET 2.0', + 'qt5cpp': 'Qt 5 C++', + 'java': 'Java', + 'objc': 'Objective-C', + 'php': 'PHP', + 'python': 'Python', + 'ruby': 'Ruby', + 'scala': 'Scala', 'dynamic-html': 'Dynamic HTML', - html: 'HTML', - swagger: 'Swagger JSON', + 'html': 'HTML', + 'swagger': 'Swagger JSON', 'swagger-yaml': 'Swagger YAML', - tizen: 'Tizen' + 'tizen': 'Tizen' }; if (names[name]) { return names[name]; } - return name.split(/\s+|\-/).map(function (word) { + return name.split(/\s+|\-/).map(function(word) { return word[0].toUpperCase() + word.substr(1); }).join(' '); }; diff --git a/scripts/controllers/import-file.js b/scripts/controllers/import-file.js index 8c1a91b39f7..daa51ae7e63 100644 --- a/scripts/controllers/import-file.js +++ b/scripts/controllers/import-file.js @@ -1,18 +1,20 @@ 'use strict'; +var angular = require("angular"); + SwaggerEditor.controller('FileImportCtrl', function FileImportCtrl($scope, $uibModalInstance, $rootScope, $localStorage, $state, FileLoader, Storage) { var results; - $scope.fileChanged = function ($fileContent) { - FileLoader.load($fileContent).then(function (res) { - $scope.$apply(function () { + $scope.fileChanged = function($fileContent) { + FileLoader.load($fileContent).then(function(res) { + $scope.$apply(function() { results = res; }); }); }; - $scope.ok = function () { + $scope.ok = function() { if (angular.isString(results)) { $rootScope.editorValue = results; Storage.save('yaml', results); @@ -21,12 +23,12 @@ SwaggerEditor.controller('FileImportCtrl', function FileImportCtrl($scope, $uibModalInstance.close(); }; - $scope.isInvalidFile = function () { + $scope.isInvalidFile = function() { return results === null; }; - $scope.isFileSelected = function () { - return !!results; + $scope.isFileSelected = function() { + return Boolean(results); }; $scope.cancel = $uibModalInstance.close; diff --git a/scripts/controllers/importurl.js b/scripts/controllers/importurl.js index 0ecf85be359..c8e994ffa03 100644 --- a/scripts/controllers/importurl.js +++ b/scripts/controllers/importurl.js @@ -1,5 +1,7 @@ 'use strict'; +var angular = require('angular'); + SwaggerEditor.controller('UrlImportCtrl', function FileImportCtrl($scope, $uibModalInstance, $localStorage, $rootScope, $state, FileLoader, Storage) { var results; @@ -16,14 +18,14 @@ SwaggerEditor.controller('UrlImportCtrl', function FileImportCtrl($scope, if (_.startsWith(url, 'http')) { $scope.fetching = true; - FileLoader.loadFromUrl(url, !$scope.opts.useProxy).then(function (data) { - $scope.$apply(function () { + FileLoader.loadFromUrl(url, !$scope.opts.useProxy).then(function(data) { + $scope.$apply(function() { results = data; $scope.canImport = true; $scope.fetching = false; }); - }).catch(function (error) { - $scope.$apply(function () { + }).catch(function(error) { + $scope.$apply(function() { $scope.error = error; $scope.canImport = false; $scope.fetching = false; @@ -36,7 +38,7 @@ SwaggerEditor.controller('UrlImportCtrl', function FileImportCtrl($scope, $scope.fetch = _.throttle(fetch, 200); - $scope.ok = function () { + $scope.ok = function() { if (angular.isString(results)) { Storage.save('yaml', results); $rootScope.editorValue = results; diff --git a/scripts/controllers/main.js b/scripts/controllers/main.js index b6655e149dc..adc84b42a5f 100644 --- a/scripts/controllers/main.js +++ b/scripts/controllers/main.js @@ -5,23 +5,30 @@ var $ = require('jquery'); SwaggerEditor.controller('MainCtrl', function MainCtrl( $scope, $rootScope, $stateParams, $location, Editor, Storage, FileLoader, Analytics, defaults) { - Analytics.initialize(); $rootScope.$on('$stateChangeStart', Editor.initializeEditor); - $rootScope.$on('$stateChangeStart', loadYaml); - // TODO: find a better way to add the branding class (grunt html template) + // find a better way to add the branding class (grunt html template) (to do) $('body').addClass(defaults.brandingCssClass); - loadYaml(); + /* + * Assigns the YAML string to editor + * + * @param {string} yaml - the Swagger document YAML or JSON + */ + var assign = function(yaml) { + if (yaml) { + Storage.save('yaml', yaml); + $rootScope.editorValue = yaml; + } + }; /* * Load Default or URL YAML */ - function loadYaml() { - - Storage.load('yaml').then(function (yaml) { + var loadYaml = function() { + Storage.load('yaml').then(function(yaml) { var url; var disableProxy = false; @@ -41,19 +48,11 @@ SwaggerEditor.controller('MainCtrl', function MainCtrl( FileLoader.loadFromUrl(url, disableProxy).then(assign); } }); - } + }; - /* - * Assigns the YAML string to editor - * - * @param {string} yaml - the Swagger document YAML or JSON - */ - function assign(yaml) { - if (yaml) { - Storage.save('yaml', yaml); - $rootScope.editorValue = yaml; - } - } + loadYaml(); + + $rootScope.$on('$stateChangeStart', loadYaml); // ----------------------- File drag and drop -------------------------------- @@ -61,7 +60,7 @@ SwaggerEditor.controller('MainCtrl', function MainCtrl( $scope.draggedFiles = []; // Watch for dropped files and trigger file reader - $scope.$watch('draggedFiles', function () { + $scope.$watch('draggedFiles', function() { if ($scope.draggedFiles instanceof File) { fileReader.readAsText($scope.draggedFiles, 'utf-8'); @@ -69,7 +68,7 @@ SwaggerEditor.controller('MainCtrl', function MainCtrl( }); // on reader success load the string - fileReader.onloadend = function () { + fileReader.onloadend = function() { if (fileReader.result) { FileLoader.load(fileReader.result).then(assign); } diff --git a/scripts/controllers/paste-json.js b/scripts/controllers/paste-json.js index b00ec8d89a7..0406ff6a4e2 100644 --- a/scripts/controllers/paste-json.js +++ b/scripts/controllers/paste-json.js @@ -2,10 +2,9 @@ SwaggerEditor.controller('PasteJSONCtrl', function PasteJSONCtrl($scope, $uibModalInstance, $rootScope, $state, Storage, YAML, SwayWorker) { - var json; - $scope.checkJSON = function (newJson) { + $scope.checkJSON = function(newJson) { $scope.canImport = false; try { @@ -16,7 +15,7 @@ SwaggerEditor.controller('PasteJSONCtrl', function PasteJSONCtrl($scope, return; } - SwayWorker.run({definition: json}, function (data) { + SwayWorker.run({definition: json}, function(data) { $scope.canImport = true; $scope.error = null; @@ -28,8 +27,8 @@ SwaggerEditor.controller('PasteJSONCtrl', function PasteJSONCtrl($scope, }); }; - $scope.ok = function () { - YAML.dump(json, function (error, result) { + $scope.ok = function() { + YAML.dump(json, function(error, result) { Storage.save('yaml', result); $rootScope.editorValue = result; $state.go('home', {tags: null}); diff --git a/scripts/controllers/preferences.js b/scripts/controllers/preferences.js index ddd9e61183f..4db2d4f0b27 100644 --- a/scripts/controllers/preferences.js +++ b/scripts/controllers/preferences.js @@ -2,12 +2,11 @@ SwaggerEditor.controller('PreferencesCtrl', function PreferencesCtrl($scope, $uibModalInstance, Preferences) { - $scope.keyPressDebounceTime = Preferences.get('keyPressDebounceTime'); $scope.liveRender = Preferences.get('liveRender'); $scope.autoComplete = Preferences.get('autoComplete'); - $scope.save = function () { + $scope.save = function() { var value = parseInt($scope.keyPressDebounceTime, 10); if (value > 0) { Preferences.set('keyPressDebounceTime', value); diff --git a/scripts/controllers/preview.js b/scripts/controllers/preview.js index 19a77d12b11..948e93c7e57 100644 --- a/scripts/controllers/preview.js +++ b/scripts/controllers/preview.js @@ -1,9 +1,10 @@ 'use strict'; +var angular = require('angular'); + SwaggerEditor.controller('PreviewCtrl', function PreviewCtrl(Storage, Builder, ASTManager, Editor, FocusedPath, TagManager, Preferences, FoldStateManager, $scope, $rootScope, $stateParams, $sessionStorage) { - $scope.loadLatest = loadLatest; $scope.tagIndexFor = TagManager.tagIndexFor; $scope.getAllTags = TagManager.getAllTags; @@ -42,22 +43,19 @@ SwaggerEditor.controller('PreviewCtrl', function PreviewCtrl(Storage, Builder, * General callback for builder results */ function onBuild(result) { - $scope.$broadcast('toggleWatchers', true); // turn watchers back on if (result.specs && result.specs.securityDefinitions) { var securityKeys = {}; - _.forEach(result.specs.securityDefinitions, function (security, key) { + _.forEach(result.specs.securityDefinitions, function(security, key) { securityKeys[key] = SparkMD5.hash(JSON.stringify(security)); }); $sessionStorage.securityKeys = securityKeys; } - $rootScope.$apply(function () { - + $rootScope.$apply(function() { if (result.specs) { - TagManager.registerTagsFromSpec(result.specs); // Retrive and put back fold state @@ -77,13 +75,13 @@ SwaggerEditor.controller('PreviewCtrl', function PreviewCtrl(Storage, Builder, function onBuildSuccess(result) { onBuild(result); - $rootScope.$apply(function () { + $rootScope.$apply(function() { $rootScope.progressStatus = 'success-process'; }); Editor.clearAnnotation(); - _.each(result.warnings, function (warning) { + _.each(result.warnings, function(warning) { Editor.annotateSwaggerError(warning, 'warning'); }); } @@ -94,7 +92,7 @@ SwaggerEditor.controller('PreviewCtrl', function PreviewCtrl(Storage, Builder, function onBuildFailure(result) { onBuild(result); - $rootScope.$apply(function () { + $rootScope.$apply(function() { if (angular.isArray(result.errors)) { if (result.errors[0].yamlError) { Editor.annotateYAMLErrors(result.errors[0].yamlError); @@ -125,15 +123,13 @@ SwaggerEditor.controller('PreviewCtrl', function PreviewCtrl(Storage, Builder, * @param {array} path - an array of keys into specs structure */ function focusEdit($event, path) { - $event.stopPropagation(); ASTManager.positionRangeForPath($rootScope.editorValue, path) - .then(function (range) { + .then(function(range) { Editor.gotoLine(range.start.line); Editor.focus(); }); - } /** @@ -222,17 +218,16 @@ SwaggerEditor.controller('PreviewCtrl', function PreviewCtrl(Storage, Builder, * */ function listAllOperation() { - // unfold folded paths first - _.each($scope.specs.paths, function (path, pathName) { + _.each($scope.specs.paths, function(path, pathName) { if (_.isObject(path) && path.$folded === true) { path.$folded = false; FoldStateManager.foldEditor(['paths', pathName], false); } }); - _.each($scope.specs.paths, function (path, pathName) { - _.each(path, function (operation, operationName) { + _.each($scope.specs.paths, function(path, pathName) { + _.each(path, function(operation, operationName) { if (_.isObject(operation)) { operation.$folded = true; FoldStateManager.foldEditor([ @@ -250,8 +245,7 @@ SwaggerEditor.controller('PreviewCtrl', function PreviewCtrl(Storage, Builder, * */ function listAllDefnitions() { - _.each($scope.specs.definitions, function (definition, definitionName) { - + _.each($scope.specs.definitions, function(definition, definitionName) { if (_.isObject(definition)) { definition.$folded = true; FoldStateManager.foldEditor(['definitions', definitionName], true); diff --git a/scripts/controllers/security.js b/scripts/controllers/security.js index 4ef1e6133f0..98285a03762 100644 --- a/scripts/controllers/security.js +++ b/scripts/controllers/security.js @@ -2,7 +2,7 @@ SwaggerEditor.controller('SecurityCtrl', function SecurityCtrl($scope, $modal, AuthManager) { - $scope.getHumanSecurityType = function (type) { + $scope.getHumanSecurityType = function(type) { var types = { basic: 'HTTP Basic Authentication', oauth2: 'OAuth 2.0', @@ -14,13 +14,13 @@ SwaggerEditor.controller('SecurityCtrl', function SecurityCtrl($scope, $modal, $scope.isAuthenticated = AuthManager.securityIsAuthenticated; - $scope.authenticate = function (securityName, security) { + $scope.authenticate = function(securityName, security) { if (security.type === 'basic') { $modal.open({ templateUrl: 'templates/auth/basic.html', controller: function BasicAuthAuthenticateCtrl($scope, $uibModalInstance) { $scope.cancel = $uibModalInstance.close; - $scope.authenticate = function (username, password) { + $scope.authenticate = function(username, password) { AuthManager.basicAuth(securityName, security, { username: username, password: password @@ -35,7 +35,7 @@ SwaggerEditor.controller('SecurityCtrl', function SecurityCtrl($scope, $modal, templateUrl: 'templates/auth/oauth2.html', controller: function OAuth2AuthenticateCtrl($scope, $uibModalInstance) { $scope.cancel = $uibModalInstance.close; - $scope.authenticate = function (accessToken) { + $scope.authenticate = function(accessToken) { if (!accessToken) { return; } @@ -52,7 +52,7 @@ SwaggerEditor.controller('SecurityCtrl', function SecurityCtrl($scope, $modal, templateUrl: 'templates/auth/api-key.html', controller: function APIKeyAuthenticateCtrl($scope, $uibModalInstance) { $scope.cancel = $uibModalInstance.close; - $scope.authenticate = function (apiKey) { + $scope.authenticate = function(apiKey) { if (!apiKey) { return; } diff --git a/scripts/directives/auto-focus.js b/scripts/directives/auto-focus.js index 7faa31c2521..b8f3c33f4eb 100644 --- a/scripts/directives/auto-focus.js +++ b/scripts/directives/auto-focus.js @@ -1,11 +1,11 @@ 'use strict'; // From https://gist.github.com/mlynch/dd407b93ed288d499778 -SwaggerEditor.directive('autoFocus', function ($timeout) { +SwaggerEditor.directive('autoFocus', function($timeout) { return { restrict: 'A', - link: function ($scope, $element, $attributes) { - $timeout(function () { + link: function($scope, $element, $attributes) { + $timeout(function() { $element[0].focus(); }, $attributes.autoFocus || 1); } diff --git a/scripts/directives/collapsewhen.js b/scripts/directives/collapsewhen.js index 83c0155a0a9..15380fa98c6 100644 --- a/scripts/directives/collapsewhen.js +++ b/scripts/directives/collapsewhen.js @@ -1,20 +1,20 @@ 'use strict'; -SwaggerEditor.directive('collapseWhen', function () { - var TRANSITION_DURATION = 200; //ms +SwaggerEditor.directive('collapseWhen', function() { + var TRANSITION_DURATION = 200; // ms return { restrict: 'A', link: function postLink(scope, element, attrs) { var buffer = null; - function cleanUp() { + var cleanUp = function() { // remove style attribute after animation // TDOD: just remove 'height' from style - setTimeout(function () { + setTimeout(function() { element.removeAttr('style'); }, TRANSITION_DURATION); - } + }; // If it's collapsed initially if (attrs.collapseWhen) { @@ -25,7 +25,7 @@ SwaggerEditor.directive('collapseWhen', function () { clone.remove(); } - scope.$watch(attrs.collapseWhen, function (val) { + scope.$watch(attrs.collapseWhen, function(val) { if (val) { buffer = element.height(); element.height(buffer); diff --git a/scripts/directives/on-file-change.js b/scripts/directives/on-file-change.js index 56fc30e4384..8d86ba67410 100644 --- a/scripts/directives/on-file-change.js +++ b/scripts/directives/on-file-change.js @@ -1,17 +1,17 @@ 'use strict'; -SwaggerEditor.directive('onReadFile', ['$parse', function ($parse) { +SwaggerEditor.directive('onReadFile', ['$parse', function($parse) { return { restrict: 'A', scope: false, - link: function (scope, element, attrs) { + link: function(scope, element, attrs) { var fn = $parse(attrs.onReadFile); - element.on('change', function (onChangeEvent) { + element.on('change', function(onChangeEvent) { var reader = new FileReader(); - reader.onload = function (onLoadEvent) { - scope.$apply(function () { + reader.onload = function(onLoadEvent) { + scope.$apply(function() { fn(scope, {$fileContent: onLoadEvent.target.result}); }); }; diff --git a/scripts/directives/operation.js b/scripts/directives/operation.js index d9ccd800c1e..6c23dc2572a 100644 --- a/scripts/directives/operation.js +++ b/scripts/directives/operation.js @@ -1,12 +1,12 @@ 'use strict'; -SwaggerEditor.directive('swaggerOperation', function (defaults) { +SwaggerEditor.directive('swaggerOperation', function(defaults) { return { restrict: 'E', replace: true, templateUrl: 'templates/operation.html', scope: false, - link: function ($scope) { + link: function($scope) { $scope.isTryOpen = false; $scope.enableTryIt = defaults.enableTryIt; $scope.toggleTry = function toggleTry() { @@ -83,8 +83,8 @@ SwaggerEditor.directive('swaggerOperation', function (defaults) { * @param responses {object} - a hash of responses * @returns boolean */ - $scope.hasAResponseWithSchema = function (responses) { - return _.keys(responses).some(function (responseCode) { + $scope.hasAResponseWithSchema = function(responses) { + return _.keys(responses).some(function(responseCode) { return responses[responseCode] && responses[responseCode].schema; }); }; @@ -96,8 +96,8 @@ SwaggerEditor.directive('swaggerOperation', function (defaults) { * @param responses {object} - a hash of responses * @returns boolean */ - $scope.hasAResponseWithHeaders = function (responses) { - return _.keys(responses).some(function (responseCode) { + $scope.hasAResponseWithHeaders = function(responses) { + return _.keys(responses).some(function(responseCode) { return responses[responseCode] && responses[responseCode].headers; }); }; @@ -109,8 +109,8 @@ SwaggerEditor.directive('swaggerOperation', function (defaults) { * @param responses {object} - a hash of responses * @returns boolean */ - $scope.hasAResponseWithExamples = function (responses) { - return _.keys(responses).some(function (responseCode) { + $scope.hasAResponseWithExamples = function(responses) { + return _.keys(responses).some(function(responseCode) { return responses[responseCode] && responses[responseCode].examples; }); }; diff --git a/scripts/directives/schemamodel.js b/scripts/directives/schemamodel.js index 54ebaff0ba3..36eb299a525 100644 --- a/scripts/directives/schemamodel.js +++ b/scripts/directives/schemamodel.js @@ -3,8 +3,7 @@ require('json-formatter-js'); // exposes global var JSONSchemaView = require('json-schema-view-js'); -SwaggerEditor.directive('schemaModel', function () { - +SwaggerEditor.directive('schemaModel', function() { return { templateUrl: 'templates/schema-model.html', restrict: 'E', @@ -16,21 +15,21 @@ SwaggerEditor.directive('schemaModel', function () { link: function postLink($scope, $element) { $scope.mode = 'schema'; - $scope.switchMode = function () { + $scope.switchMode = function() { $scope.mode = $scope.mode === 'json' ? 'schema' : 'json'; }; - $scope.$watch('schema', render); - - render(); - - function render() { + var render = function() { var formatter = new JSONFormatter($scope.schema, 1); $element.find('td.view.json').html(formatter.render()); var schemaView = new JSONSchemaView($scope.schema, 1); $element.find('td.view.schema').html(schemaView.render()); - } + }; + + $scope.$watch('schema', render); + + render(); } }; }); diff --git a/scripts/directives/scroll-into-view-when.js b/scripts/directives/scroll-into-view-when.js index ce144185cad..9ca445b449f 100644 --- a/scripts/directives/scroll-into-view-when.js +++ b/scripts/directives/scroll-into-view-when.js @@ -1,12 +1,10 @@ 'use strict'; -SwaggerEditor.directive('scrollIntoViewWhen', function () { - +SwaggerEditor.directive('scrollIntoViewWhen', function() { return { restrict: 'A', link: function postLink($scope, $element, $attrs) { - - $scope.$watch($attrs.scrollIntoViewWhen, function (val) { + $scope.$watch($attrs.scrollIntoViewWhen, function(val) { if (val) { $element.scrollIntoView(100); } diff --git a/scripts/directives/stop-event.js b/scripts/directives/stop-event.js index e26a055de16..4affc88eba3 100644 --- a/scripts/directives/stop-event.js +++ b/scripts/directives/stop-event.js @@ -1,11 +1,11 @@ 'use strict'; // From http://stackoverflow.com/a/19846473/650722 -SwaggerEditor.directive('stopEvent', function () { +SwaggerEditor.directive('stopEvent', function() { return { restrict: 'A', - link: function (scope, element) { - element.bind('click', function (e) { + link: function(scope, element) { + element.bind('click', function(e) { e.stopPropagation(); }); } diff --git a/scripts/directives/track-event.js b/scripts/directives/track-event.js index c68aa81e759..46043e4103a 100644 --- a/scripts/directives/track-event.js +++ b/scripts/directives/track-event.js @@ -1,5 +1,7 @@ 'use strict'; +var angular = require('angular'); + /* * An attribute directive that will fire analytics event when element that this * directive is attached to is clicked @@ -8,11 +10,11 @@ * For example track-event="project new" will fire `project` event with `new` * subevent */ -SwaggerEditor.directive('trackEvent', function (Analytics) { +SwaggerEditor.directive('trackEvent', function(Analytics) { return { restrict: 'A', - link: function ($scope, $element, $attributes) { - $element.bind('click', function () { + link: function($scope, $element, $attributes) { + $element.bind('click', function() { var eventName = $attributes.trackEvent; if (angular.isString(eventName)) { diff --git a/scripts/directives/tryoperation.js b/scripts/directives/tryoperation.js index 1f61d47a716..0ac75c6c7ca 100644 --- a/scripts/directives/tryoperation.js +++ b/scripts/directives/tryoperation.js @@ -1,11 +1,12 @@ 'use strict'; +var angular = require('angular'); + var _ = require('lodash'); var $ = require('jquery'); -SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, +SwaggerEditor.controller('TryOperation', function($scope, formdataFilter, AuthManager, SchemaForm) { - var parameters = $scope.getParameters(); var securityOptions = getSecurityOptions(); var FILE_TYPE = ' F I L E '; // File key identifier for file types @@ -29,7 +30,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, configureSchemaForm(); // Deeply watch specs for updates to regenerate the from - $scope.$watch('specs', function () { + $scope.$watch('specs', function() { $scope.requestModel = makeRequestModel(); $scope.requestSchema = makeRequestSchema(); }, true); @@ -52,8 +53,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, /* * configure SchemaForm directive based on request schema */ - function configureSchemaForm() { - + var configureSchemaForm = function() { // Determine if this request has a loose body parameter schema // A loose body parameter schema is a body parameter that allows additional // properties or has no properties object @@ -63,13 +63,9 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, var loose = false; // loose schema is only for requests with body parameter - if (!hasRequestBody()) { - loose = false; - - } else { + if (hasRequestBody()) { // we're accessing deep in the schema. many operations can fail here try { - for (var p in $scope.requestSchema.properties.parameters.properties) { var param = $scope.requestSchema.properties.parameters.properties[p]; if (param.in === 'body' && isLooseJSONSchema(param)) { @@ -77,10 +73,12 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, } } } catch (e) {} + } else { + loose = false; } SchemaForm.options = _.extend(defaultOptions, loose ? looseOptions : {}); - } + }; /* * Determines if a JSON Schema is loose @@ -89,8 +87,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, * * @returns {boolean} */ - function isLooseJSONSchema(schema) { - + var isLooseJSONSchema = function(schema) { // loose object if (schema.additionalProperties || _.isEmpty(schema.properties)) { return true; @@ -102,12 +99,11 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, (schema.items.additionalProperties || _.isEmpty(schema.items.properties)) ) { - return true; } return false; - } + }; /* * Appends JSON Editor options for schema recursively so if a schema needs to @@ -117,7 +113,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, * * @returns {object} - A JSON Schema object */ - function appendJSONEditorOptions(schema) { + var appendJSONEditorOptions = function(schema) { var looseOptions = { no_additional_properties: false, // jshint ignore:line disable_properties: false, // jshint ignore:line @@ -132,7 +128,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, _.each(schema.properties, appendJSONEditorOptions); return schema; - } + }; /* * Makes the request schema to generate the form in the template @@ -141,8 +137,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, * @returns {object} - A JSON Schema containing all properties required to * make this call */ - function makeRequestSchema() { - + var makeRequestSchema = function() { // base schema var schema = { type: 'object', @@ -206,7 +201,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, // Add a new property for each parameter parameters.map(pickSchemaFromParameter).map(normalizeJSONSchema) - .forEach(function (paramSchema) { + .forEach(function(paramSchema) { // extend the parameters property with the schema schema.properties.parameters @@ -215,7 +210,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, } return schema; - } + }; /* * Makes a model with empty values that conforms to the JSON Schema generated @@ -224,7 +219,6 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, * @returns {object} - the model */ function makeRequestModel() { - // base model var model = { @@ -242,7 +236,6 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, // Add Content-Type header only if this operation has a body parameter if (hasRequestBody()) { - // Default to application/json model.contentType = 'application/json'; } @@ -251,7 +244,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, if (parameters.length) { model.parameters = {}; parameters.map(pickSchemaFromParameter).map(normalizeJSONSchema) - .forEach(function (paramSchema) { + .forEach(function(paramSchema) { var defaults = { object: {}, array: [], @@ -368,22 +361,22 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, // operation level securities if (_.isArray($scope.operation.security)) { - $scope.operation.security.map(function (security) { - _.keys(security).forEach(function (key) { + $scope.operation.security.map(function(security) { + _.keys(security).forEach(function(key) { securityOptions = securityOptions.concat(key); }); }); // root level securities } else if (_.isArray($scope.specs.security)) { - $scope.specs.security.map(function (security) { - _.keys(security).forEach(function (key) { + $scope.specs.security.map(function(security) { + _.keys(security).forEach(function(key) { securityOptions = securityOptions.concat(key); }); }); } - return _.uniq(securityOptions).filter(function (security) { + return _.uniq(securityOptions).filter(function(security) { // only return authenticated options return AuthManager.securityIsAuthenticated(security); @@ -437,7 +430,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, return result; } - Object.keys(schema.properties).forEach(function (propertyName) { + Object.keys(schema.properties).forEach(function(propertyName) { // if this property is an object itself, recurse if (schema.properties[propertyName].type === 'object') { @@ -520,7 +513,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, var queryParamsStr; var pathStr; var isCollectionQueryParam = parameters.filter(parameterTypeFilter('query')) - .some(function (parameter) { + .some(function(parameter) { // if a query parameter has a collection format it doesn't matter what // is it's value, it will force the URL to not use `[]` in query string @@ -538,7 +531,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, // if there are selected securities and they are located in the query append // them to the URL if (angular.isArray(requestModel.security)) { - requestModel.security.forEach(function (securityOption) { + requestModel.security.forEach(function(securityOption) { var auth = AuthManager.getAuth(securityOption); // if auth exists and it's an api key in query, add it to query params @@ -560,7 +553,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, // a simple replace method where it uses the available path parameter // value to replace the path parameter or leave it as it is if path // parameter doesn't exist. - function (match) { + function(match) { var matchKey = match.substring(1, match.length - 1); if (angular.isDefined(pathParams[matchKey])) { @@ -596,7 +589,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, // add header based securities to list of headers if (angular.isArray($scope.requestModel.security)) { - $scope.requestModel.security.forEach(function (secuirtyOption) { + $scope.requestModel.security.forEach(function(secuirtyOption) { var auth = AuthManager.getAuth(secuirtyOption); @@ -792,7 +785,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, function parseHeaders(headers) { var result = {}; - headers.split('\n').forEach(function (line) { + headers.split('\n').forEach(function(line) { var key = line.split(':')[0]; var value = line.split(':')[1]; if (key && angular.isString(key) && angular.isString(value)) { @@ -821,7 +814,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, contentType: $scope.contentType }) - .fail(function (jqXHR, textStatus, errorThrown) { + .fail(function(jqXHR, textStatus, errorThrown) { $scope.xhrInProgress = false; $scope.textStatus = textStatus; $scope.error = errorThrown; @@ -830,7 +823,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, $scope.$digest(); }) - .done(function (data, textStatus, jqXHR) { + .done(function(data, textStatus, jqXHR) { $scope.textStatus = textStatus; $scope.xhrInProgress = false; $scope.responseData = data; @@ -848,7 +841,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, * * @returns {string} */ - $scope.prettyPrint = function (input) { + $scope.prettyPrint = function(input) { // Try if it's JSON and return pretty JSON try { @@ -865,7 +858,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, * * @returns {boolean} */ - $scope.isJson = function (value) { + $scope.isJson = function(value) { // if value is already parsed return true if (angular.isObject(value) || angular.isArray(value)) { @@ -890,7 +883,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, * * @returns {boolean} */ - $scope.isType = function (headers, type) { + $scope.isType = function(headers, type) { var regex = new RegExp(type); headers = headers || {}; @@ -902,7 +895,7 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter, * * @returns {boolean} */ - $scope.isCrossOrigin = function () { + $scope.isCrossOrigin = function() { return $scope.specs.host && $scope.specs.host !== $scope.locationHost; }; }); diff --git a/test/.eslintrc.js b/test/.eslintrc.js index 5b4d25f4b46..0a588eace4d 100644 --- a/test/.eslintrc.js +++ b/test/.eslintrc.js @@ -3,9 +3,8 @@ module.exports = { extends: "../.eslintrc.js", globals: { - sinon: true, - inject: true, - window: true + sinon: false, + inject: false }, env: { jasmine: true,