This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/app/scripts/core/root.js
2016-06-02 08:31:29 +08:00

168 lines
5.3 KiB
JavaScript

(function () {
'use strict';
angular.module('ariaNg').run(['$rootScope', '$location', '$document', 'SweetAlert', function ($rootScope, $location, $document, SweetAlert) {
var isUrlMatchUrl2 = function (url, url2) {
if (url === url2) {
return true;
}
var index = url2.indexOf(url);
if (index !== 0) {
return false;
}
var lastPart = url2.substring(url.length);
if (lastPart.indexOf('/') == 0) {
return true;
}
return false;
};
var setNavbarSelected = function (location) {
angular.element('section.sidebar > ul li').removeClass('active');
angular.element('section.sidebar > ul > li[data-href-match]').each(function (index, element) {
var match = angular.element(element).attr('data-href-match');
if (isUrlMatchUrl2(match, location)) {
angular.element(element).addClass('active');
}
});
angular.element('section.sidebar > ul > li.treeview > ul.treeview-menu > li[data-href-match]').each(function (index, element) {
var match = angular.element(element).attr('data-href-match');
if (isUrlMatchUrl2(match, location)) {
angular.element(element).addClass('active').parent().parent().addClass('active');
}
});
};
var showSidebar = function () {
angular.element('body').removeClass('sidebar-collapse').addClass('sidebar-open');
};
var hideSidebar = function () {
angular.element('body').addClass('sidebar-collapse').removeClass('sidebar-open');
};
var isSidebarShowInSmallScreen = function () {
return angular.element('body').hasClass('sidebar-open');
};
$rootScope.searchContext = {
text: ''
};
$rootScope.taskContext = {
list: [],
selected: {},
enableSelectAll: false,
getSelectedTaskIds: function () {
var result = [];
if (!this.list || !this.selected || this.list.length < 1) {
return result;
}
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (this.selected[task.gid]) {
result.push(task.gid);
}
}
return result;
},
getSelectedTasks: function () {
var result = [];
if (!this.list || !this.selected || this.list.length < 1) {
return result;
}
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (this.selected[task.gid]) {
result.push(task);
}
}
return result;
},
selectAll: function () {
if (!this.list || !this.selected || this.list.length < 1) {
return result;
}
var isAllSelected = true;
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
if (!this.selected[task.gid]) {
isAllSelected = false;
break;
}
}
for (var i = 0; i < this.list.length; i++) {
var task = this.list[i];
this.selected[task.gid] = !isAllSelected;
}
}
};
$rootScope.swipeActions = {
leftSwipe: function () {
if (isSidebarShowInSmallScreen()) {
hideSidebar();
return;
}
if (!this.extentLeftSwipe ||
(angular.isFunction(this.extentLeftSwipe) && !this.extentLeftSwipe())) {
hideSidebar();
}
},
rightSwipe: function () {
if (!this.extentRightSwipe ||
(angular.isFunction(this.extentRightSwipe) && !this.extentRightSwipe())) {
showSidebar();
}
}
};
$rootScope.$on('$locationChangeStart', function (event) {
SweetAlert.close();
$rootScope.loadPromise = null;
delete $rootScope.swipeActions.extentLeftSwipe;
delete $rootScope.swipeActions.extentRightSwipe;
if (angular.isArray($rootScope.taskContext.list) && $rootScope.taskContext.list.length > 0) {
$rootScope.taskContext.list.length = 0;
}
if (angular.isObject($rootScope.taskContext.selected)) {
$rootScope.taskContext.selected = {};
}
$rootScope.taskContext.enableSelectAll = false;
});
$rootScope.$on('$routeChangeStart', function (event, next, current) {
var location = $location.path();
setNavbarSelected(location);
$document.unbind('keypress');
});
}]);
})();