MWF.require("MWF.widget.O2Identity", null, false);
MWF.xDesktop.requireApp("Org", "$Explorer", null, false);
MWF.xDesktop.requireApp("Template", "MTooltips", null, false);
MWF.xApplication.Org.UnitExplorer = new Class({
Extends: MWF.xApplication.Org.$Explorer,
Implements: [Options, Events],
options: {
"style": "default",
"lp": {
},
"creator": false
},
_loadLp: function(){
this.options.lp = {
"search": this.app.lp.search,
"searchText": this.app.lp.searchText,
"elementSave": this.app.lp.organizationSave,
"deleteElements": this.app.lp.deleteOrganization,
"deleteElementsCancel": this.app.lp.deleteElementsCancel,
"deleteElementsTitle": this.app.lp.deleteOrganizationTitle,
"deleteElementsConfirm": this.app.lp.deleteOrganizationSubConfirm,
"noSignature": this.app.lp.noSignature,
"edit": this.app.lp.edit,
"cancel": this.app.lp.cancel,
"save": this.app.lp.save,
"add": this.app.lp.add
}
},
loadElements: function(addToNext){
if (!this.isElementLoaded){
if (!this.loaddingElement){
this.loaddingElement = true;
this._listElementNext(function(json){
if (json.data.length){
this.loadListContent(json.data);
}else{
if (!this.elements.length){
this.setNoElementNoticeArea();
}
}
this.loadElementQueue = 0;
this.isElementLoaded = true;
this.loaddingElement = false;
}.bind(this));
}else{
if (addToNext) this.loadElementQueue++;
}
}
},
loadListContent: function(data){
data.each(function(itemData, i){
var item = this._newElement(itemData, this, i);
this.elements.push(item);
item.load();
if (this.elements.length===1){
item.selected();
if (!item.isExpand) item.expand();
}
}.bind(this));
},
_listElementNext: function(callback){
if (MWF.AC.isOrganizationManager() || MWF.AC.isSecurityManager()){
this.actions.listTopUnit(function(json){
if (callback) callback.apply(this, [json]);
}.bind(this));
}else{
if (layout.session.user.identityList.length){
var json = {"data": []};
var unitNames = [];
layout.session.user.identityList.each(function(id){
var idFlag = (id.distinguishedName || id.id || id.unique || id.name);
o2.Actions.get("x_organization_assemble_express").getUnitWithIdentityAndLevel({"identity": idFlag, "level": 1}, function(o){
if (o.data){
this.actions.getUnit(o.data.distinguishedName, function(u){
if (unitNames.indexOf(u.data.distinguishedName)==-1){
unitNames.push(u.data.distinguishedName);
json.data.push(u.data);
}
}.bind(this),null, false);
}
}.bind(this), null, false);
}.bind(this));
if (callback) callback.apply(this, [json]);
}
}
},
_newElement: function(data, explorer){
return new MWF.xApplication.Org.UnitExplorer.Unit(data, explorer, this.isEditor, 0);
},
_listElementByKey: function(callback, failure, key){
this.actions.listUnitByKey(function(json){
if (callback) callback.apply(this, [json]);
}.bind(this), failure, key);
},
_getAddElementData: function(){
return {
"name": "",
"unique": "",
"typeList": ["company"],
"description": "",
"shortName": "",
"superior": "",
"orderNumber": "",
"controllerList": [],
"control": {
"allowEdit": true,
"allowDelete": true
},
"woSubDirectIdentityList": [],
"woUnitAttributeList": [],
"woUnitDutyList": []
};
},
deleteSelectedElements: function(e){
var _self = this;
this.app.confirm("infor", e, this.options.lp.deleteElementsTitle, {"html": this.options.lp.deleteElementsConfirm}, 500, 260, function(){
var deleted = [];
var doCount = 0;
var readyCount = _self.deleteElements.length;
var errorText = "";
var complete;
complete = function () {
if (doCount === readyCount) {
if (errorText) {
_self.app.notice(errorText, "error", _self.propertyContentNode, {x: "left", y: "top"});
}
}
};
_self.deleteElements.each(function(element){
element["delete"](function(){
deleted.push(element);
doCount++;
if (_self.deleteElements.length===doCount){
_self.deleteElements = _self.deleteElements.filter(function (item) {
return !deleted.contains(item);
});
_self.checkDeleteElements();
}
complete();
}, function(error){
errorText = (errorText) ? errorText+"
"+error : error;
doCount++;
if (_self.deleteElements.length !== doCount) {
} else {
_self.deleteElements = _self.deleteElements.filter(function (item) {
return !deleted.contains(item);
});
_self.checkDeleteElements();
}
complete();
});
});
this.close();
}, function(){
this.close();
});
}
});
MWF.xApplication.Org.UnitExplorer.Unit = new Class({
Extends: MWF.xApplication.Org.$Explorer.Item,
initialize: function(data, explorer, isEditor, i, listNode, parent){
this.i = i;
this.level = i;
this.parent = parent;
this.data = data;
this.explorer = explorer;
this.listNode = listNode || this.explorer.listNode;
this.propertyContentNode = this.explorer.propertyContentNode;
this.initStyle();
this.selectedAttributes = [];
this.isEdit = false;
this.isEditor = isEditor;
this.deleteSelected = false;
this.subUnits = [];
},
refresh: function(){
this._loadTextNode();
if (this.content){
if (this.content.titleInfor) this.content.titleInfor.refresh();
if (this.content.bottomInfor) this.content.bottomInfor.refresh();
}
this.addActions();
},
initStyle: function(){
var css = Object.clone(this.explorer.css.item);
this.style = Object.merge(css, this.explorer.css.unitItem);
},
_loadTextNode: function(){
this.textNode.set({"text": this.data.name+((this.data.subDirectUnitCount) ? " ("+(this.data.subDirectUnitCount)+")" : "")});
},
load: function(){
this.node = new Element("div", {"styles": this.style.node}).inject(this.listNode);
this.contentNode = new Element("div", {"styles": this.style.contentNode}).inject(this.node);
var left = (10*this.level);
this.contentNode.setStyle("padding-left", ""+left+"px");
if ((this.level%2)===1){
this.node.setStyle("background-color", "#ffffff");
this.contentNode.setStyle("background-color", "#ffffff");
}
this.childNode = new Element("div", {"styles": this.style.childNode}).inject(this.node);
this.toggleIconNode = new Element("div", {"styles": this.style.unitToggleIconNode}).inject(this.contentNode);
this.setToggleIconNode();
this.setToggleAction();
this.iconNode = new Element("div", {"styles": this.style.unitIconNode}).inject(this.contentNode);
var src = this._getIcon();
this.iconNode.setStyle("background-image", "url("+src+")");
this.actionNode = new Element("div", {"styles": this.style.actionNode}).inject(this.contentNode);
this.textNode = new Element("div", {"styles": this.style.unitTextNode}).inject(this.contentNode);
this._loadTextNode();
this.setNewItem();
this.node.inject(this.listNode);
this.addActions();
this.setEvent();
},
addActions: function(){
//if (this.isEditor){
if (this.data.id){
if (this.data.control.allowDelete){
if (!this.deleteNode){
this.deleteNode = new Element("div.o2icon-delete", {"styles": this.style.actionDeleteNode}).inject(this.actionNode);
this.deleteNode.addEvent("click", function(e){
if (!this.notDelete){
if (!this.deleteSelected){
this.setDelete();
}else{
this.setUndelete();
}
}
e.stopPropagation();
}.bind(this));
}
}
if (this.data.control.allowEdit){
if (!this.addNode){
this.addNode = new Element("div.o2icon-add", {"styles": this.style.actionAddNode}).inject(this.actionNode);
this.addNode.addEvent("click", function(e){
if (!this.notDelete){
this.addSubUnit();
}
e.stopPropagation();
}.bind(this));
}
}
if (this.explorer.currentItem===this){
if (this.deleteNode) this.deleteNode.setStyles(this.style.actionDeleteNode_selected);
if (this.addNode) this.addNode.setStyles(this.style.actionAddNode_selected);
}
}
// }
},
addSubUnit: function(){
this.expand(function(){
var isNewElement = true;
if (this.explorer.currentItem) isNewElement = this.explorer.currentItem.unSelected();
if (isNewElement){
var newElementData = this.explorer._getAddElementData();
newElementData.superior = this.data.id;
var item = new MWF.xApplication.Org.UnitExplorer.Unit(newElementData, this.explorer, this.isEditor, this.level+1, this.childNode, this);
item.load();
item.selected();
item.editBaseInfor();
(new Fx.Scroll(this.explorer.listScrollNode)).toElementCenter(item.node);
}else{
this.app.notice(this.explorer.options.lp.elementSave, "error", this.explorer.propertyContentNode);
}
}.bind(this));
},
setDeleteFromP: function(){
this.notDelete = true;
this.subUnits.each(function(unit){
unit.setDeleteFromP();
});
this.deleteNode.setStyles(this.style.actionDeleteNode_delete);
this.contentNode.setStyles(this.style.contentNode_delete);
this.textNode.setStyles(this.style.unitTextNode);
//this.explorer.deleteElements.push(this);
this.deleteSelected = true;
this.explorer.checkDeleteElements(this);
},
setDelete: function(){
//this.actionNode.fade("in");
this.subUnits.each(function(unit){
unit.setDeleteFromP();
});
this.deleteNode.setStyles(this.style.actionDeleteNode_delete);
if (this.addNode) this.addNode.setStyles(this.style.actionAddNode_delete);
this.contentNode.setStyles(this.style.contentNode_delete);
this.contentNode.removeClass("mainColor_bg");
this.textNode.setStyles(this.style.unitTextNode);
this.explorer.deleteElements.push(this);
this.deleteSelected = true;
this.deleteNode.removeClass("o2icon-delete").addClass("o2icon-off");
this.explorer.checkDeleteElements(this);
},
setUndelete: function(){
//this.actionNode.fade("out");
this.notDelete = false;
this.subUnits.each(function(unit){
unit.setUndelete();
});
if (this.explorer.currentItem!==this){
if (this.deleteNode) this.deleteNode.setStyles(this.style.actionDeleteNode);
if (this.addNode) this.addNode.setStyles(this.style.actionAddNode);
this.contentNode.setStyles(this.style.contentNode);
this.contentNode.removeClass("mainColor_bg");
this.textNode.setStyles(this.style.unitTextNode);
}else{
this.contentNode.setStyles(this.style.contentNode_selected);
this.contentNode.addClass("mainColor_bg");
this.textNode.setStyles(this.style.textNode_selected);
this.actionNode.setStyles(this.style.actionNode_selected);
if (this.deleteNode) this.deleteNode.setStyles(this.style.actionDeleteNode_selected);
if (this.addNode) this.addNode.setStyles(this.style.actionAddNode_selected);
if (this.addNode) this.addNode.setStyles(this.style.actionAddNode_selected);
}
this.deleteNode.removeClass("o2icon-off").addClass("o2icon-delete");
this.explorer.deleteElements.erase(this);
this.deleteSelected = false;
this.explorer.checkDeleteElements(this);
},
setToggleIconNode: function(){
if (this.data.subDirectUnitCount>0){
var toggle_on = (this.explorer.currentItem===this) ? "toggle_current_on" : "toggle_on";
var toggle_off = (this.explorer.currentItem===this) ? "toggle_current_off" : "toggle_off";
if (this.isExpand){
this.toggleIconNode.setStyle("background-image", "url(../x_component_Org/$Explorer/"+this.explorer.app.options.style+"/icon/"+toggle_on+".png)");
}else{
this.toggleIconNode.setStyle("background-image", "url(../x_component_Org/$Explorer/"+this.explorer.app.options.style+"/icon/"+toggle_off+".png)");
}
}else{
this.toggleIconNode.setStyle("background-image", "");
}
},
setToggleAction: function(){
this.toggleIconNode.addEvent("click", function(e){
this.expandOrCollapse();
e.stopPropagation();
}.bind(this));
},
expandOrCollapse: function(){
if (this.isExpand){
this.collapse();
}else{
this.expand();
}
},
listSubUnit: function(callback){
this.node.mask();
this.explorer.actions.listSubUnitDirect(function(json){
if (json.data.length){
json.data.each(function(itemData){
var item = new MWF.xApplication.Org.UnitExplorer.Unit(itemData, this.explorer, this.isEditor, this.level+1, this.childNode, this);
this.explorer.elements.push(item);
item.load();
this.subUnits.push(item);
}.bind(this));
}
this.isLoadSub = true;
this.node.unmask();
if (callback) callback();
}.bind(this), null, this.data.id);
},
expand: function(callback){
this.childNode.setStyle("display", "block");
this.isExpand = true;
this.setToggleIconNode();
if (!this.isLoadSub){
this.listSubUnit(callback);
}else{
if (callback) callback();
}
},
collapse: function(){
this.childNode.setStyle("display", "none");
this.isExpand = false;
this.setToggleIconNode();
},
unSelected: function(){
if (this.content.baseInfor.mode==="edit") return false;
this.explorer.currentItem = null;
this.contentNode.setStyles(this.style.contentNode);
this.textNode.setStyles(this.style.unitTextNode);
this.actionNode.setStyles(this.style.actionNode);
if (this.deleteNode) this.deleteNode.setStyles(this.style.actionDeleteNode);
if (this.addNode) this.addNode.setStyles(this.style.actionAddNode);
if (this.deleteNode)this.deleteNode.removeClass("mainColor_color");
if (this.addNode)this.addNode.removeClass("mainColor_color");
this.iconNode.setStyle("background-image", "url("+this._getIcon()+")");
this.contentNode.removeClass("mainColor_bg");
this.contentNode.removeClass("mainColor_bg_opacity");
this.setToggleIconNode();
this.clearItemProperty();
return true;
},
selected: function(){
this.explorer.currentItem = this;
this.contentNode.setStyles(this.style.contentNode_selected);
this.textNode.setStyles(this.style.textNode_selected);
this.actionNode.setStyles(this.style.actionNode_selected);
if (this.deleteNode) this.deleteNode.setStyles(this.style.actionDeleteNode_selected);
if (this.addNode) this.addNode.setStyles(this.style.actionAddNode_selected);
if (this.deleteNode)this.deleteNode.removeClass("mainColor_color");
if (this.addNode)this.addNode.removeClass("mainColor_color");
this.iconNode.setStyle("background-image", "url("+this._getIcon()+")");
this.contentNode.addClass("mainColor_bg");
this.contentNode.removeClass("mainColor_bg_opacity");
this.setToggleIconNode();
this.showItemProperty();
},
showItemProperty: function(){
this.content = new MWF.xApplication.Org.UnitExplorer.UnitContent(this);
},
"delete": function(success, failure){
this.explorer.actions.deleteUnit(this.data.id, function(){
this.destroy();
if (success) success();
}.bind(this), function(xhr, text, error){
var errorText = error;
if (xhr) errorText = xhr.responseText;
MWF.xDesktop.notice("error", {x: "right", y:"top"}, "request json error: "+errorText);
if (failure) failure();
});
},
_getIcon: function(){
return (this.explorer.currentItem===this) ? "../x_component_Org/$Explorer/default/icon/unit_current.png" : "../x_component_Org/$Explorer/default/icon/unit.png";
},
_isActionManager: function(){
return (MWF.AC.isOrganizationManager() || MWF.AC.isUnitManager());
}
});
MWF.xApplication.Org.UnitExplorer.UnitContent = new Class({
Extends: MWF.xApplication.Org.$Explorer.ItemContent,
_getData: function(callback){
if (this.item.data.id){
this.explorer.actions.getUnit(function(json){
this.data = json.data;
this.item.data = json.data;
if (callback) callback();
}.bind(this), null, this.item.data.id);
}else{
this.data = this.item.data;
if (callback) callback();
}
},
_showItemPropertyTitle: function(){
this.titleInfor = new MWF.xApplication.Org.UnitExplorer.UnitContent.TitleInfor(this);
},
_showItemPropertyBottom: function(){
this.bottomInfor = new MWF.xApplication.Org.UnitExplorer.UnitContent.BottomInfor(this);
},
loadItemPropertyTab: function(callback){
this.propertyTabContainerNode = new Element("div", {"styles": this.item.style.tabTitleNode}).inject(this.propertyContentNode, "top");
MWF.require("MWF.widget.Tab", function(){
this.propertyTab = new MWF.widget.Tab(this.propertyContentNode, {"style": "unit", "useMainColor":true});
this.propertyTab.load();
this.propertyTab.tabNodeContainer.inject(this.propertyTabContainerNode);
if (callback) callback();
}.bind(this));
},
_loadTabs: function(){
this.baseContentNode = new Element("div", {"styles": this.item.style.tabContentNode});
this.basePage = this.propertyTab.addTab(this.baseContentNode, this.explorer.app.lp.unitBaseText);
this.personMemberContentNode = new Element("div", {"styles": this.item.style.tabContentNode});
this.personMemberPage = this.propertyTab.addTab(this.personMemberContentNode, this.explorer.app.lp.unitPersonMembers);
this.dutyContentNode = new Element("div", {"styles": this.item.style.tabContentNode});
this.dutyPage = this.propertyTab.addTab(this.dutyContentNode, this.explorer.app.lp.unitDutys);
if (this.data.control.allowEdit){
this.attributeContentNode = new Element("div", {"styles": this.item.style.tabContentNode});
this.attributePage = this.propertyTab.addTab(this.attributeContentNode, this.explorer.app.lp.unitAttribute);
}
},
_loadContent: function(){
this._listBaseInfor();
this.loadListCount();
this._listIdentityMembers();
this._listDutys();
if (this.data.control.allowEdit) this._listAttributes();
//var _self = this;
// this.personMemberList = this._listMembers("personList", "woSubDirectIdentityList", this.personMemberContentNode, [{
// "get": function(){
// var src = _self.explorer.actions.getPersonIcon(this.person);
// return "