Loading public/js/app.js +55 −1 Original line number Diff line number Diff line Loading @@ -858,8 +858,9 @@ angular.module('cerebro').controller('OverviewController', ['$scope', '$http', angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce', 'DataService', 'AlertService', 'ModalService', 'AceEditorService', 'ClipboardService', function($scope, $http, $sce, DataService, AlertService, ModalService, AceEditorService) { AceEditorService, ClipboardService) { $scope.editor = undefined; $scope.response = undefined; Loading Loading @@ -904,6 +905,30 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', $scope.options = autocomplete.getAlternatives(text); } }; $scope.copyAsCURLCommand = function() { var method = $scope.method; var host = DataService.getHost(); var path = encodeURI($scope.path); if (path.substring(0, 1) !== '/') { path = '/' + path; } var body = JSON.stringify($scope.editor.getValue(), undefined, 1); var curl = 'curl -X' + method + ' \'' + host + path + '\''; if (['POST', 'PUT'].indexOf(method) >= 0) { curl += ' -d \'' + body + '\''; } ClipboardService.copy( curl, function() { AlertService.info('cURL request successfully copied to clipboard'); }, function() { AlertService.error('Error while copying request to clipboard'); } ); }; }] ); Loading Loading @@ -1750,6 +1775,35 @@ angular.module('cerebro').factory('AlertService', function() { return this; }); angular.module('cerebro').factory('ClipboardService', ['AlertService', '$document', '$window', function(AlertService, $document, $window) { var textarea = angular.element($document[0].createElement('textarea')); textarea.css({ position: 'absolute', left: '-9999px', top: ( $window.pageYOffset || $document[0].documentElement.scrollTop ) + 'px' }); textarea.attr({readonly: ''}); angular.element($document[0].body).append(textarea); this.copy = function(value, success, failure) { try { textarea.val(value); textarea.select(); $document[0].execCommand('copy'); success(); } catch (error) { failure(); } }; return this; } ]); angular.module('cerebro').factory('ClusterChangesService', [ '$rootScope', 'AlertService', 'RefreshService', 'DataService', function($rootScope, AlertService, RefreshService, DataService) { Loading public/rest.html +5 −0 Original line number Diff line number Diff line Loading @@ -47,6 +47,11 @@ </div> <div class="form-group row"> <div class="col-lg-12 text-right"> <div class="btn-group"> <button type="submit" class="btn btn-default" ng-click="copyAsCURLCommand()"> <i class="fa fa-clipboard"></i> cURL </button> </div> <div class="btn-group"> <button type="submit" class="btn btn-default" ng-click="editor.format()"> <i class="fa fa-align-left"></i> format Loading src/app/components/rest/controller.js +26 −1 Original line number Diff line number Diff line angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce', 'DataService', 'AlertService', 'ModalService', 'AceEditorService', 'ClipboardService', function($scope, $http, $sce, DataService, AlertService, ModalService, AceEditorService) { AceEditorService, ClipboardService) { $scope.editor = undefined; $scope.response = undefined; Loading Loading @@ -46,5 +47,29 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', $scope.options = autocomplete.getAlternatives(text); } }; $scope.copyAsCURLCommand = function() { var method = $scope.method; var host = DataService.getHost(); var path = encodeURI($scope.path); if (path.substring(0, 1) !== '/') { path = '/' + path; } var body = JSON.stringify($scope.editor.getValue(), undefined, 1); var curl = 'curl -X' + method + ' \'' + host + path + '\''; if (['POST', 'PUT'].indexOf(method) >= 0) { curl += ' -d \'' + body + '\''; } ClipboardService.copy( curl, function() { AlertService.info('cURL request successfully copied to clipboard'); }, function() { AlertService.error('Error while copying request to clipboard'); } ); }; }] ); src/app/shared/services/clipboard.js 0 → 100644 +28 −0 Original line number Diff line number Diff line angular.module('cerebro').factory('ClipboardService', ['AlertService', '$document', '$window', function(AlertService, $document, $window) { var textarea = angular.element($document[0].createElement('textarea')); textarea.css({ position: 'absolute', left: '-9999px', top: ( $window.pageYOffset || $document[0].documentElement.scrollTop ) + 'px' }); textarea.attr({readonly: ''}); angular.element($document[0].body).append(textarea); this.copy = function(value, success, failure) { try { textarea.val(value); textarea.select(); $document[0].execCommand('copy'); success(); } catch (error) { failure(); } }; return this; } ]); tests/shared/services/clipboard.tests.js 0 → 100644 +123 −0 Original line number Diff line number Diff line "use strict"; describe("ClipboardService", function() { var service, $window, $document; var elementFunction = angular.element; // keeps original function var document = [ { createElement: function(tag) { return element; }, documentElement: { scrollTop: '4321' }, execCommand: function(method) { }, body: 'document body' } ]; var element = { css: function(css) { }, val: function(value) { }, select: function() { }, attr: function(attr) { }, append: function() { }, on: function() { }, off: function() { } }; beforeEach(module("cerebro")); afterEach(function() { angular.element = elementFunction; // restores original function }); beforeEach(function() { var elementMock = { element: function(elem) { // replaces with mock return element; } }; spyOn(angular, 'element').andReturn(element); spyOn(element, 'css').andCallThrough(); spyOn(element, 'val').andCallThrough(); spyOn(element, 'select').andCallThrough(); spyOn(element, 'attr').andCallThrough(); spyOn(document[0], 'createElement').andCallThrough(); }); beforeEach(function() { module('cerebro'); module(function($provide) { $provide.value('$window', { pageYOffset: '1234' }); $provide.value('$document', document); }); }); beforeEach(inject(function($injector) { service = $injector.get('ClipboardService'); $window = $injector.get('$window'); $document = $injector.get('$document'); })); it("should instantiate textarea to hold data to copy", function() { expect(angular.element).toHaveBeenCalledWith(element); expect(document[0].createElement).toHaveBeenCalledWith('textarea'); expect(element.css).toHaveBeenCalledWith({ position: 'absolute', left: '-9999px', top: '1234px' }); expect(element.attr).toHaveBeenCalledWith({readonly: ''}); expect(angular.element).toHaveBeenCalledWith('document body'); } ); it("should copy the given text to the textarea and invoke copy and call success callback", function() { var callbacks = { success: function() {}, failure: function() {}, } spyOn($document[0], 'execCommand').andReturn(true); spyOn(callbacks, 'success').andReturn(true); service.copy('text to be copied', callbacks.success, callbacks.failure); expect(element.val).toHaveBeenCalledWith('text to be copied'); expect(element.select).toHaveBeenCalled(); expect($document[0].execCommand).toHaveBeenCalledWith('copy'); expect(callbacks.success).toHaveBeenCalled(); } ); it("should copy the given text to the textarea and invoke copy and call failure callback", function() { var callbacks = { success: function() { throw 'error' }, failure: function() {}, } spyOn($document[0], 'execCommand').andReturn(true); spyOn(callbacks, 'failure').andReturn(true); service.copy('text to be copied', callbacks.success, callbacks.failure); expect(element.val).toHaveBeenCalledWith('text to be copied'); expect(element.select).toHaveBeenCalled(); expect($document[0].execCommand).toHaveBeenCalledWith('copy'); expect(callbacks.failure).toHaveBeenCalled(); } ); }); Loading
public/js/app.js +55 −1 Original line number Diff line number Diff line Loading @@ -858,8 +858,9 @@ angular.module('cerebro').controller('OverviewController', ['$scope', '$http', angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce', 'DataService', 'AlertService', 'ModalService', 'AceEditorService', 'ClipboardService', function($scope, $http, $sce, DataService, AlertService, ModalService, AceEditorService) { AceEditorService, ClipboardService) { $scope.editor = undefined; $scope.response = undefined; Loading Loading @@ -904,6 +905,30 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', $scope.options = autocomplete.getAlternatives(text); } }; $scope.copyAsCURLCommand = function() { var method = $scope.method; var host = DataService.getHost(); var path = encodeURI($scope.path); if (path.substring(0, 1) !== '/') { path = '/' + path; } var body = JSON.stringify($scope.editor.getValue(), undefined, 1); var curl = 'curl -X' + method + ' \'' + host + path + '\''; if (['POST', 'PUT'].indexOf(method) >= 0) { curl += ' -d \'' + body + '\''; } ClipboardService.copy( curl, function() { AlertService.info('cURL request successfully copied to clipboard'); }, function() { AlertService.error('Error while copying request to clipboard'); } ); }; }] ); Loading Loading @@ -1750,6 +1775,35 @@ angular.module('cerebro').factory('AlertService', function() { return this; }); angular.module('cerebro').factory('ClipboardService', ['AlertService', '$document', '$window', function(AlertService, $document, $window) { var textarea = angular.element($document[0].createElement('textarea')); textarea.css({ position: 'absolute', left: '-9999px', top: ( $window.pageYOffset || $document[0].documentElement.scrollTop ) + 'px' }); textarea.attr({readonly: ''}); angular.element($document[0].body).append(textarea); this.copy = function(value, success, failure) { try { textarea.val(value); textarea.select(); $document[0].execCommand('copy'); success(); } catch (error) { failure(); } }; return this; } ]); angular.module('cerebro').factory('ClusterChangesService', [ '$rootScope', 'AlertService', 'RefreshService', 'DataService', function($rootScope, AlertService, RefreshService, DataService) { Loading
public/rest.html +5 −0 Original line number Diff line number Diff line Loading @@ -47,6 +47,11 @@ </div> <div class="form-group row"> <div class="col-lg-12 text-right"> <div class="btn-group"> <button type="submit" class="btn btn-default" ng-click="copyAsCURLCommand()"> <i class="fa fa-clipboard"></i> cURL </button> </div> <div class="btn-group"> <button type="submit" class="btn btn-default" ng-click="editor.format()"> <i class="fa fa-align-left"></i> format Loading
src/app/components/rest/controller.js +26 −1 Original line number Diff line number Diff line angular.module('cerebro').controller('RestController', ['$scope', '$http', '$sce', 'DataService', 'AlertService', 'ModalService', 'AceEditorService', 'ClipboardService', function($scope, $http, $sce, DataService, AlertService, ModalService, AceEditorService) { AceEditorService, ClipboardService) { $scope.editor = undefined; $scope.response = undefined; Loading Loading @@ -46,5 +47,29 @@ angular.module('cerebro').controller('RestController', ['$scope', '$http', $scope.options = autocomplete.getAlternatives(text); } }; $scope.copyAsCURLCommand = function() { var method = $scope.method; var host = DataService.getHost(); var path = encodeURI($scope.path); if (path.substring(0, 1) !== '/') { path = '/' + path; } var body = JSON.stringify($scope.editor.getValue(), undefined, 1); var curl = 'curl -X' + method + ' \'' + host + path + '\''; if (['POST', 'PUT'].indexOf(method) >= 0) { curl += ' -d \'' + body + '\''; } ClipboardService.copy( curl, function() { AlertService.info('cURL request successfully copied to clipboard'); }, function() { AlertService.error('Error while copying request to clipboard'); } ); }; }] );
src/app/shared/services/clipboard.js 0 → 100644 +28 −0 Original line number Diff line number Diff line angular.module('cerebro').factory('ClipboardService', ['AlertService', '$document', '$window', function(AlertService, $document, $window) { var textarea = angular.element($document[0].createElement('textarea')); textarea.css({ position: 'absolute', left: '-9999px', top: ( $window.pageYOffset || $document[0].documentElement.scrollTop ) + 'px' }); textarea.attr({readonly: ''}); angular.element($document[0].body).append(textarea); this.copy = function(value, success, failure) { try { textarea.val(value); textarea.select(); $document[0].execCommand('copy'); success(); } catch (error) { failure(); } }; return this; } ]);
tests/shared/services/clipboard.tests.js 0 → 100644 +123 −0 Original line number Diff line number Diff line "use strict"; describe("ClipboardService", function() { var service, $window, $document; var elementFunction = angular.element; // keeps original function var document = [ { createElement: function(tag) { return element; }, documentElement: { scrollTop: '4321' }, execCommand: function(method) { }, body: 'document body' } ]; var element = { css: function(css) { }, val: function(value) { }, select: function() { }, attr: function(attr) { }, append: function() { }, on: function() { }, off: function() { } }; beforeEach(module("cerebro")); afterEach(function() { angular.element = elementFunction; // restores original function }); beforeEach(function() { var elementMock = { element: function(elem) { // replaces with mock return element; } }; spyOn(angular, 'element').andReturn(element); spyOn(element, 'css').andCallThrough(); spyOn(element, 'val').andCallThrough(); spyOn(element, 'select').andCallThrough(); spyOn(element, 'attr').andCallThrough(); spyOn(document[0], 'createElement').andCallThrough(); }); beforeEach(function() { module('cerebro'); module(function($provide) { $provide.value('$window', { pageYOffset: '1234' }); $provide.value('$document', document); }); }); beforeEach(inject(function($injector) { service = $injector.get('ClipboardService'); $window = $injector.get('$window'); $document = $injector.get('$document'); })); it("should instantiate textarea to hold data to copy", function() { expect(angular.element).toHaveBeenCalledWith(element); expect(document[0].createElement).toHaveBeenCalledWith('textarea'); expect(element.css).toHaveBeenCalledWith({ position: 'absolute', left: '-9999px', top: '1234px' }); expect(element.attr).toHaveBeenCalledWith({readonly: ''}); expect(angular.element).toHaveBeenCalledWith('document body'); } ); it("should copy the given text to the textarea and invoke copy and call success callback", function() { var callbacks = { success: function() {}, failure: function() {}, } spyOn($document[0], 'execCommand').andReturn(true); spyOn(callbacks, 'success').andReturn(true); service.copy('text to be copied', callbacks.success, callbacks.failure); expect(element.val).toHaveBeenCalledWith('text to be copied'); expect(element.select).toHaveBeenCalled(); expect($document[0].execCommand).toHaveBeenCalledWith('copy'); expect(callbacks.success).toHaveBeenCalled(); } ); it("should copy the given text to the textarea and invoke copy and call failure callback", function() { var callbacks = { success: function() { throw 'error' }, failure: function() {}, } spyOn($document[0], 'execCommand').andReturn(true); spyOn(callbacks, 'failure').andReturn(true); service.copy('text to be copied', callbacks.success, callbacks.failure); expect(element.val).toHaveBeenCalledWith('text to be copied'); expect(element.select).toHaveBeenCalled(); expect($document[0].execCommand).toHaveBeenCalledWith('copy'); expect(callbacks.failure).toHaveBeenCalled(); } ); });