2015-02-19 21:21:54 +00:00
|
|
|
/**
|
|
|
|
* Service which provides helper methods for performing some simple UI operations.
|
|
|
|
*/
|
2016-04-11 21:28:49 +00:00
|
|
|
angular.module('quay').factory('UIService', ['$timeout', '$rootScope', '$location', 'ApiService', function($timeout, $rootScope, $location, ApiService) {
|
2015-07-31 20:31:29 +00:00
|
|
|
var CheckStateController = function(items, itemKey) {
|
2015-03-10 05:03:39 +00:00
|
|
|
this.items = items;
|
2015-07-31 20:31:29 +00:00
|
|
|
this.checked = [];
|
|
|
|
|
|
|
|
this.allItems_ = items;
|
|
|
|
this.allCheckedMap_ = {};
|
2015-06-24 21:43:57 +00:00
|
|
|
|
2015-07-31 20:31:29 +00:00
|
|
|
this.itemKey_ = itemKey;
|
|
|
|
this.listeners_ = [];
|
|
|
|
this.page_ = null;
|
2016-04-20 19:25:20 +00:00
|
|
|
this.lastChanged_ = null;
|
|
|
|
|
2016-04-11 21:28:49 +00:00
|
|
|
this.ApiService = ApiService
|
2015-03-12 19:22:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.listen = function(callback) {
|
|
|
|
this.listeners_.push(callback);
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.isChecked = function(item) {
|
2015-07-31 20:31:29 +00:00
|
|
|
return !!this.allCheckedMap_[item[this.itemKey_]];
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
2016-04-20 19:25:20 +00:00
|
|
|
CheckStateController.prototype.toggleItem = function(item, opt_shift) {
|
|
|
|
if (opt_shift && this.lastChanged_) {
|
|
|
|
var itemIndex = $.inArray(item, this.items);
|
|
|
|
var lastIndex = $.inArray(this.lastChanged_, this.items);
|
|
|
|
|
|
|
|
if (itemIndex >= 0 && lastIndex >= 0) {
|
|
|
|
var startIndex = Math.min(itemIndex, lastIndex);
|
|
|
|
var endIndex = Math.max(itemIndex, lastIndex);
|
|
|
|
var shouldCheck = this.isChecked(this.lastChanged_);
|
|
|
|
|
|
|
|
for (var i = startIndex; i <= endIndex; ++i) {
|
|
|
|
if (shouldCheck) {
|
|
|
|
this.checkItem(this.items[i]);
|
|
|
|
} else {
|
|
|
|
this.uncheckItem(this.items[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 05:03:39 +00:00
|
|
|
if (this.isChecked(item)) {
|
|
|
|
this.uncheckItem(item);
|
|
|
|
} else {
|
|
|
|
this.checkItem(item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.toggleItems = function() {
|
2016-04-20 19:25:20 +00:00
|
|
|
this.lastChanged_= null;
|
2015-07-31 20:31:29 +00:00
|
|
|
this.updateMap_(this.checked, false);
|
|
|
|
|
2015-03-10 05:03:39 +00:00
|
|
|
if (this.checked.length) {
|
|
|
|
this.checked = [];
|
|
|
|
} else {
|
|
|
|
this.checked = this.items.slice();
|
|
|
|
}
|
2015-07-31 20:31:29 +00:00
|
|
|
|
|
|
|
this.updateMap_(this.checked, true);
|
2015-03-12 19:22:47 +00:00
|
|
|
this.callListeners_();
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
2015-07-31 20:31:29 +00:00
|
|
|
CheckStateController.prototype.setPage = function(page, pageSize) {
|
|
|
|
this.items = this.allItems_.slice(page * pageSize, (page + 1) * pageSize);
|
|
|
|
this.rebuildCheckedList_();
|
|
|
|
};
|
|
|
|
|
2015-06-24 21:43:57 +00:00
|
|
|
CheckStateController.prototype.setChecked = function(items) {
|
2015-07-31 20:31:29 +00:00
|
|
|
this.allCheckedMap_ = {};
|
|
|
|
this.updateMap_(items, true);
|
|
|
|
this.rebuildCheckedList_();
|
2015-06-24 21:43:57 +00:00
|
|
|
};
|
|
|
|
|
2015-07-31 20:31:29 +00:00
|
|
|
CheckStateController.prototype.rebuildCheckedList_ = function() {
|
2015-06-24 21:43:57 +00:00
|
|
|
var that = this;
|
2015-07-31 20:31:29 +00:00
|
|
|
this.checked = [];
|
2016-05-12 21:59:49 +00:00
|
|
|
this.allItems_.forEach(function(item) {
|
2015-07-31 20:31:29 +00:00
|
|
|
if (that.allCheckedMap_[item[that.itemKey_]]) {
|
|
|
|
that.checked.push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.callListeners_();
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.updateMap_ = function(items, is_checked) {
|
|
|
|
var that = this;
|
|
|
|
items.forEach(function(item) {
|
2015-07-13 09:20:51 +00:00
|
|
|
if (item == null) { return; }
|
2015-07-31 20:31:29 +00:00
|
|
|
that.allCheckedMap_[item[that.itemKey_]] = is_checked;
|
2015-06-24 21:43:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-10 05:03:39 +00:00
|
|
|
CheckStateController.prototype.checkByFilter = function(filter) {
|
2015-07-31 20:31:29 +00:00
|
|
|
this.updateMap_(this.checked, false);
|
2015-03-10 05:03:39 +00:00
|
|
|
this.checked = $.grep(this.items, filter);
|
2015-07-31 20:31:29 +00:00
|
|
|
this.updateMap_(this.checked, true);
|
2015-03-12 19:22:47 +00:00
|
|
|
this.callListeners_();
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.checkItem = function(item) {
|
2016-04-20 19:25:20 +00:00
|
|
|
if (this.isChecked(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastChanged_ = item;
|
2015-03-10 05:03:39 +00:00
|
|
|
this.checked.push(item);
|
2015-07-31 20:31:29 +00:00
|
|
|
this.allCheckedMap_[item[this.itemKey_]] = true;
|
2015-03-12 19:22:47 +00:00
|
|
|
this.callListeners_();
|
2015-03-10 05:03:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CheckStateController.prototype.uncheckItem = function(item) {
|
2016-04-20 19:25:20 +00:00
|
|
|
if (!this.isChecked(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastChanged_ = item;
|
2015-03-10 05:03:39 +00:00
|
|
|
this.checked = $.grep(this.checked, function(cItem) {
|
|
|
|
return cItem != item;
|
|
|
|
});
|
2015-07-31 20:31:29 +00:00
|
|
|
|
|
|
|
this.allCheckedMap_[item[this.itemKey_]] = false;
|
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() {
|
2015-07-31 20:31:29 +00:00
|
|
|
var that = this;
|
|
|
|
var allCheckedMap = this.allCheckedMap_;
|
|
|
|
var allChecked = [];
|
|
|
|
|
|
|
|
this.allItems_.forEach(function(item) {
|
|
|
|
var key = item[that.itemKey_];
|
|
|
|
if (!!allCheckedMap[key]) {
|
|
|
|
allChecked.push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-12 19:22:47 +00:00
|
|
|
this.listeners_.map(function(listener) {
|
2015-07-31 20:31:29 +00:00
|
|
|
listener(allChecked, that.checked);
|
2015-03-12 19:22:47 +00:00
|
|
|
});
|
|
|
|
};
|
2015-03-10 05:03:39 +00:00
|
|
|
|
2015-07-31 20:31:29 +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) {
|
2016-04-11 21:28:49 +00:00
|
|
|
var message = ApiService.getErrorMessage(result, 'error');
|
2015-02-19 21:21:54 +00:00
|
|
|
if (message) {
|
2015-07-14 10:35:10 +00:00
|
|
|
uiService.showPopover(elem, message, opt_placement || 'bottom');
|
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-04-22 17:16:10 +00:00
|
|
|
uiService.clickElement = function(el) {
|
|
|
|
// From: http://stackoverflow.com/questions/16802795/click-not-working-in-mocha-phantomjs-on-certain-elements
|
|
|
|
var ev = document.createEvent("MouseEvent");
|
|
|
|
ev.initMouseEvent(
|
|
|
|
"click",
|
|
|
|
true /* bubble */, true /* cancelable */,
|
|
|
|
window, null,
|
|
|
|
0, 0, 0, 0, /* coordinates */
|
|
|
|
false, false, false, false, /* modifier keys */
|
|
|
|
0 /*left*/, null);
|
|
|
|
el.dispatchEvent(ev);
|
|
|
|
};
|
|
|
|
|
2016-04-29 23:37:35 +00:00
|
|
|
uiService.initializeTabs = function(scope, element, opt_clickCallback, opt_rememberCookie) {
|
2015-04-22 17:16:10 +00:00
|
|
|
var locationListener = null;
|
|
|
|
var disposed = false;
|
|
|
|
|
|
|
|
var changeTab = function(activeTab) {
|
|
|
|
if (disposed) { return; }
|
|
|
|
|
|
|
|
$('a[data-toggle="tab"]').each(function(index) {
|
|
|
|
var tabName = this.getAttribute('data-target').substr(1);
|
|
|
|
if (tabName != activeTab) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($(this).parent().hasClass('active')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.clientWidth == 0) {
|
|
|
|
setTimeout(function() {
|
|
|
|
changeTab(activeTab);
|
|
|
|
}, 100);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:58:05 +00:00
|
|
|
var elem = this;
|
|
|
|
setTimeout(function() {
|
|
|
|
uiService.clickElement(elem);
|
|
|
|
}, 0);
|
2015-04-22 17:16:10 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var resetDefaultTab = function() {
|
|
|
|
if (disposed) { return; }
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
element.find('a[data-toggle="tab"]').each(function(index) {
|
|
|
|
if (index == 0) {
|
2015-04-22 19:58:05 +00:00
|
|
|
var elem = this;
|
|
|
|
setTimeout(function() {
|
|
|
|
uiService.clickElement(elem);
|
|
|
|
}, 0);
|
2015-04-22 17:16:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var checkTabs = function() {
|
|
|
|
if (disposed) { return; }
|
|
|
|
|
|
|
|
// Poll until we find the tabs.
|
|
|
|
var tabs = element.find('a[data-toggle="tab"]');
|
|
|
|
if (tabs.length == 0) {
|
|
|
|
$timeout(checkTabs, 50);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register listeners.
|
|
|
|
registerListeners(tabs);
|
|
|
|
|
|
|
|
// Set the active tab (if any).
|
|
|
|
var activeTab = $location.search()['tab'];
|
|
|
|
if (activeTab) {
|
|
|
|
changeTab(activeTab);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var registerListeners = function(tabs) {
|
|
|
|
// Listen for scope destruction.
|
|
|
|
scope.$on('$destroy', function() {
|
2015-04-23 18:40:01 +00:00
|
|
|
disposed = true;
|
2015-04-22 17:16:10 +00:00
|
|
|
locationListener && locationListener();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Listen for route changes and update the tabs accordingly.
|
2016-04-29 23:37:35 +00:00
|
|
|
if (!opt_rememberCookie) {
|
|
|
|
locationListener = $rootScope.$on('$routeUpdate', function(){
|
|
|
|
if ($location.search()['tab']) {
|
|
|
|
changeTab($location.search()['tab']);
|
|
|
|
} else {
|
|
|
|
resetDefaultTab();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-04-22 17:16:10 +00:00
|
|
|
|
|
|
|
// Listen for tab changes.
|
|
|
|
tabs.on('shown.bs.tab', function (e) {
|
|
|
|
// Invoke the callback, if any.
|
|
|
|
opt_clickCallback && opt_clickCallback();
|
|
|
|
|
2016-04-29 23:37:35 +00:00
|
|
|
// Update the search location or cookie.
|
|
|
|
if (opt_rememberCookie) {
|
|
|
|
// TODO: this
|
|
|
|
} else {
|
|
|
|
var tabName = e.target.getAttribute('data-target').substr(1);
|
|
|
|
$rootScope.$apply(function() {
|
|
|
|
var isDefaultTab = tabs[0] == e.target;
|
|
|
|
var newSearch = $.extend($location.search(), {});
|
|
|
|
if (isDefaultTab) {
|
|
|
|
delete newSearch['tab'];
|
|
|
|
} else {
|
|
|
|
newSearch['tab'] = tabName;
|
|
|
|
}
|
|
|
|
|
|
|
|
$location.search(newSearch);
|
|
|
|
});
|
|
|
|
}
|
2015-04-22 17:16:10 +00:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Start the checkTabs timer.
|
|
|
|
checkTabs();
|
|
|
|
};
|
|
|
|
|
2015-02-19 21:21:54 +00:00
|
|
|
return uiService;
|
|
|
|
}]);
|