2015-02-19 21:21:54 +00:00
|
|
|
/**
|
|
|
|
* Service which provides helper methods for performing some simple UI operations.
|
|
|
|
*/
|
|
|
|
angular.module('quay').factory('UIService', [function() {
|
2015-03-12 19:22:47 +00:00
|
|
|
var CheckStateController = function(items, opt_checked) {
|
2015-03-10 05:03:39 +00:00
|
|
|
this.items = items;
|
2015-03-12 19:22:47 +00:00
|
|
|
this.checked = opt_checked || [];
|
|
|
|
this.listeners_ = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.listen = function(callback) {
|
|
|
|
this.listeners_.push(callback);
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.isChecked = function(item) {
|
|
|
|
return $.inArray(item, this.checked) >= 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.toggleItem = function(item) {
|
|
|
|
if (this.isChecked(item)) {
|
|
|
|
this.uncheckItem(item);
|
|
|
|
} else {
|
|
|
|
this.checkItem(item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.toggleItems = function() {
|
|
|
|
if (this.checked.length) {
|
|
|
|
this.checked = [];
|
|
|
|
} else {
|
|
|
|
this.checked = this.items.slice();
|
|
|
|
}
|
2015-03-12 19:22:47 +00:00
|
|
|
this.callListeners_();
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.checkByFilter = function(filter) {
|
|
|
|
this.checked = $.grep(this.items, filter);
|
2015-03-12 19:22:47 +00:00
|
|
|
this.callListeners_();
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.checkItem = function(item) {
|
|
|
|
this.checked.push(item);
|
2015-03-12 19:22:47 +00:00
|
|
|
this.callListeners_();
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.uncheckItem = function(item) {
|
|
|
|
this.checked = $.grep(this.checked, function(cItem) {
|
|
|
|
return cItem != item;
|
|
|
|
});
|
2015-03-12 19:22:47 +00:00
|
|
|
this.callListeners_();
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
2015-03-12 19:22:47 +00:00
|
|
|
CheckStateController.prototype.callListeners_ = function() {
|
|
|
|
var checked = this.checked;
|
|
|
|
this.listeners_.map(function(listener) {
|
|
|
|
listener(checked);
|
|
|
|
});
|
|
|
|
};
|
2015-03-10 05:03:39 +00:00
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
var uiService = {};
|
|
|
|
|
|
|
|
uiService.hidePopover = function(elem) {
|
|
|
|
var popover = $(elem).data('bs.popover');
|
|
|
|
if (popover) {
|
|
|
|
popover.hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-30 22:44:23 +00:00
|
|
|
uiService.showPopover = function(elem, content, opt_placement) {
|
2015-02-19 21:21:54 +00:00
|
|
|
var popover = $(elem).data('bs.popover');
|
|
|
|
if (!popover) {
|
2015-03-30 22:44:23 +00:00
|
|
|
$(elem).popover({'content': '-', 'placement': opt_placement || 'left'});
|
2015-02-19 21:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
var popover = $(elem).data('bs.popover');
|
|
|
|
popover.options.content = content;
|
|
|
|
popover.show();
|
|
|
|
}, 500);
|
|
|
|
};
|
|
|
|
|
2015-03-30 22:44:23 +00:00
|
|
|
uiService.showFormError = function(elem, result, opt_placement) {
|
2015-02-19 21:21:54 +00:00
|
|
|
var message = result.data['message'] || result.data['error_description'] || '';
|
|
|
|
if (message) {
|
2015-03-30 22:44:23 +00:00
|
|
|
uiService.showPopover(elem, message, opt_placement);
|
2015-02-19 21:21:54 +00:00
|
|
|
} else {
|
|
|
|
uiService.hidePopover(elem);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-12 19:22:47 +00:00
|
|
|
uiService.createCheckStateController = function(items, opt_checked) {
|
|
|
|
return new CheckStateController(items, opt_checked);
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
2015-03-25 22:43:12 +00:00
|
|
|
uiService.showPasswordDialog = function(message, callback, opt_canceledCallback) {
|
|
|
|
var success = function() {
|
|
|
|
var password = $('#passDialogBox').val();
|
|
|
|
$('#passDialogBox').val('');
|
|
|
|
callback(password);
|
|
|
|
};
|
|
|
|
|
|
|
|
var canceled = function() {
|
|
|
|
$('#passDialogBox').val('');
|
|
|
|
opt_canceledCallback && opt_canceledCallback();
|
|
|
|
};
|
|
|
|
|
|
|
|
var box = bootbox.dialog({
|
|
|
|
"message": message +
|
|
|
|
'<form style="margin-top: 10px" action="javascript:void(0)">' +
|
|
|
|
'<input id="passDialogBox" class="form-control" type="password" placeholder="Current Password">' +
|
|
|
|
'</form>',
|
|
|
|
"title": 'Please Verify',
|
|
|
|
"buttons": {
|
|
|
|
"verify": {
|
|
|
|
"label": "Verify",
|
|
|
|
"className": "btn-success",
|
|
|
|
"callback": success
|
|
|
|
},
|
|
|
|
"close": {
|
|
|
|
"label": "Cancel",
|
|
|
|
"className": "btn-default",
|
|
|
|
"callback": canceled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
box.bind('shown.bs.modal', function(){
|
|
|
|
box.find("input").focus();
|
|
|
|
box.find("form").submit(function() {
|
|
|
|
if (!$('#passDialogBox').val()) { return; }
|
|
|
|
box.modal('hide');
|
|
|
|
success();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
return uiService;
|
|
|
|
}]);
|