Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.venv/
*.pyc
static/dashboard/js/*.js
static/dashboard/css/*.css
*~
.secret_key_store
*.secret_key_store.lock
85 changes: 85 additions & 0 deletions horizon/static/framework/util/long-running/long-running.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.long-running', [])
.factory('horizon.framework.util.long-running.service', serviceFactory);

/**
*
```js

// inject 'hz.framework.long-running.service' as longRunning here

var interval = 3000;

longRunning(function () {
return someApi.someMethod(); // returns a promise
}, interval)

// repeats invoke the async long-time-running operation
// if it returned with a `response` object that does not
// meet a certain condition.

.until(function (response) {
return response.data.foo !== null; // returns boolean
})

// executes this function when the `until` function return `true`,
// pass in the same arguments as the one passed to until function.

.then(function (response) {
myDataModel.foo = response.data.foo;
});

```
*/
serviceFactory.$inject = ['$q', '$timeout'];

function serviceFactory($q, $timeout) {
return longRunning;

function longRunning(operation, interval) {
var conditionFn, deferred = $q.defer();
loop();
return {
until: until
};

function until(func) {
conditionFn = func;
return deferred.promise;
}

function loop() {
$timeout(function () {
operation().then(function (data) {
if (conditionFn(data)) {
deferred.resolve(data);
} else {
loop();
}
},
loop);
}, interval);
}
}
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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';

describe('horizon.framework.util.long-running.service', function () {
var $q, $timeout, longRunning;

beforeEach(module('horizon.framework'));
beforeEach(inject(function ($injector) {
$q = $injector.get('$q');
$timeout = $injector.get('$timeout');
longRunning = $injector.get('horizon.framework.util.long-running.service');
}));

it('defines the service as a function.', function () {
expect(angular.isFunction(longRunning)).toBe(true);
});

it('works properly.', function () {
var complete = false;
var c = 0;

longRunning(function () {
return $q(function (resolve, reject) {
$timeout(function () {
if (c++ < 3) {
resolve({ data: {} });
} else {
resolve({ data: { foo: 'bar' } });
}
}, 200);
});
}, 3000)
.until(function (response) {
return response.data.foo === 'bar';
})
.then(function () {
complete = true;
});

$timeout.flush();
$timeout.flush();
expect(complete).toBe(false);

$timeout.flush();
$timeout.flush();
expect(complete).toBe(false);

$timeout.flush();
$timeout.flush();
expect(complete).toBe(false);

$timeout.flush();
$timeout.flush();
expect(complete).toBe(true);
});
});
})();
1 change: 1 addition & 0 deletions horizon/static/framework/util/util.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'horizon.framework.util.filters',
'horizon.framework.util.http',
'horizon.framework.util.i18n',
'horizon.framework.util.long-running',
'horizon.framework.util.promise-toggle',
'horizon.framework.util.tech-debt',
'horizon.framework.util.workflow',
Expand Down
30 changes: 30 additions & 0 deletions horizon/static/framework/widgets/modal/prompt/prompt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="modal-header">
<h3 class="modal-title">
{$ ::context.text.title $}
</h3>
</div>
<div class="modal-body clearfix">
<form name="promptForm">
<input class="form-control"
maxlength="{$ ::context.maxlength $}"
minlength="{$ ::context.minlength $}"
ng-model="input"
ng-pattern="{$ ::context.pattern $}"
ng-required="{$ ::context.required $}"
placeholder="{$ ::context.text.placeholder $}"
type="{$ ::context.type || 'text' $}">
</form>
</div>
<div class="modal-footer">
<button class="btn btn-default secondary"
type="button"
ng-click="cancel()">
{$ ::context.text.cancel $}
</button>
<button class="btn btn-primary"
type="button"
ng-click="submit(input)"
ng-disabled="promptForm.$invalid">
{$ ::context.text.submit $}
</button>
</div>
94 changes: 94 additions & 0 deletions horizon/static/framework/widgets/modal/prompt/prompt.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* © 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';

/**
* @ngdoc service
* @name horizon.framework.widgets.modal.prompt
*
* @description
* Emulating browser's window.prompt function to provide a popup
* dialog for user to input data.
*
* @param {object} params, the specification object for the popup dialog.
* @param {object} callback, the callback function with user input.
*
* @example:
```js

prompt({
text: {
title: gettext('Please input data'),
submit: gettext('Ok'),
cancel: gettext('Cancel'),
placeholder: gettext('Input data')
},
required: true
},
function (input) {
// ...
});

```
*/

angular
.module('horizon.framework.widgets.modal')
.factory('horizon.framework.widgets.modal.prompt', serviceFactory)
.controller('horizon.framework.widgets.modal.PromptController', PromptController);

serviceFactory.$inject = [
'$modal',
'horizon.framework.widgets.basePath'
];

function serviceFactory($modal, basePath) {
return prompt;

function prompt(params, callback) {
$modal.open({
controller: 'horizon.framework.widgets.modal.PromptController',
templateUrl: basePath + 'modal/prompt/prompt.html',
resolve: {
context: function() {
params.callback = callback;
return params;
}
}
});
}
}

PromptController.$inject = ['$scope', '$modalInstance', 'context'];

function PromptController($scope, $modalInstance, context) {
$scope.submit = submit;
$scope.cancel = cancel;
$scope.context = context || {};

function submit(input) {
context.callback(input);
$modalInstance.close();
}

function cancel() {
$modalInstance.dismiss('cancel');
}
}

})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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';

describe('prompt service', function () {
var prompt, scope, ctrl, modalInstance, context;

beforeEach(module('templates'));
beforeEach(module('ui.bootstrap'));
beforeEach(module('horizon.framework'));

beforeEach(inject(function ($injector, $controller) {
prompt = $injector.get('horizon.framework.widgets.modal.prompt');
scope = $injector.get('$rootScope').$new();

modalInstance = {
close: function () {},
dismiss: function () {}
};

context = {
callback: function (input) {}
};

ctrl = $controller('horizon.framework.widgets.modal.PromptController', {
$scope: scope,
$modalInstance: modalInstance,
context: context
});
}));

it ('is defined as function', function () {
expect(angular.isFunction(prompt)).toBe(true);
});

it('establishes a controller', function () {
expect(ctrl).toBeDefined();
});

it('sets context on the scope', function () {
expect(scope.context).toBeDefined();
});

it('sets action functions', function () {
expect(scope.submit).toBeDefined();
expect(scope.cancel).toBeDefined();
});

it('makes submit invoke callback function', function () {
expect(scope.submit).toBeDefined();
spyOn(context, 'callback');
scope.submit('something');
expect(context.callback).toHaveBeenCalledWith('something');
});

it('makes submit close the modal instance', function () {
expect(scope.submit).toBeDefined();
spyOn(modalInstance, 'close');
scope.submit('something');
expect(modalInstance.close.calls.count()).toBe(1);
});

it('makes cancel close the modal instance', function () {
expect(scope.cancel).toBeDefined();
spyOn(modalInstance, 'dismiss');
scope.cancel();
expect(modalInstance.dismiss).toHaveBeenCalledWith('cancel');
});
});

})();
Loading