2021SC@SDUSC
接下来分析Page类与PageArray类
Page类中包含着基础参数
并对其进行着初始化与get、set操作
Page.prototype.initialize = function(id, src){
Element.prototype.initialize.call(this, id, []);
this._src = src;
this._width = 0;
this._height = 0;
};
// --------------------------------------------------------------------------------
// * Getter & Setter
// --------------------------------------------------------------------------------
Object.defineProperty(Page.prototype, 'src', {
get: function() {
return this._src;
},
set: function(value) {
this._src = value;
},
configurable: true
});
Object.defineProperty(Page.prototype, 'width', {
get: function() {
return this._width;
},
set: function(value) {
this._width = value;
},
configurable: true
});
Object.defineProperty(Page.prototype, 'height', {
get: function() {
return this._height;
},
set: function(value) {
this._height = value;
},
configurable: true
});
resize方法调整大小:
Page.prototype.setSize = function(width, height){
this._width = width;
this._height = height;
};
Page类的基础增删改方法
Page类对于Json的基本处理
Page.prototype.loadJson = function(json_object){
Element.prototype.loadJson.call(this, json_object);
this._src = json_object._src === undefined ? this._src : json_object._src;
this._width = json_object._width === undefined ? this._width : json_object._width;
this._height = json_object._height === undefined ? this._height : json_object._height;
};
Page.prototype.saveJson = function(){
let output = Element.prototype.saveJson.call(this);
delete output._pages;
output._src = this._src;
output._width = this._width;
output._height = this._height;
return output;
};
Page.prototype.exportJson = function(){
let output = Element.prototype.exportJson.call(this);
delete output.pages;
output.src = this._src;
output.width = this._width;
output.height = this._height;
return output;
};
PageArray中包含着page数组与当前页
初始化与get、set操作
PageArray.prototype.initialize = function(){
this.clear();
};
PageArray.prototype.clear = function(){
this._page_list = [];
this._current_page = 0;
};
// --------------------------------------------------------------------------------
// * Getter & Setter
// --------------------------------------------------------------------------------
Object.defineProperty(PageArray.prototype, 'page_list', {
get: function() {
return this._page_list;
},
configurable: true
});
Object.defineProperty(PageArray.prototype, 'current_page', {
get: function() {
return this._current_page;
},
configurable: true
});
对页面进行添加和删除操作
PageArray.prototype.addAfterPage = function(index, page_id){
this._page_list.splice(index, 0, page_id);
this.setCurrentPage(index + 1);
};
PageArray.prototype.removePage = function(page_id){
let index = this._page_list.indexOf(page_id);
if(page_id < 0) return;
this._page_list.splice(index, 1);
if(this._current_page > this._page_list.length){
this.setCurrentPage(this._current_page);
}
return page_id;
};
对页面进行移动位置操作
PageArray.prototype.moveCurrentPage = function(target){
if(this._page_list.length <= 1) return;
if(this._current_page === target) return;
if(this._current_page <= 0 || this._current_page > this._page_list.length) return;
if(this._current_page === 1 && target <= 1) return;
if(this._current_page === this._page_list.length && target > this._page_list.length) return;
let real_target = Math.max(0, Math.min(target, this._page_list.length));
if(this._current_page === real_target || this._current_page === real_target + 1) return;
let page_id = this._page_list.splice(this._current_page - 1, 1)[0];
if(real_target > this._current_page){
this._current_page = real_target - 1;
}else{
this._current_page = real_target;
}
this.addAfterCurrentPage(page_id);
};
Page相关的Json操作
PageArray.prototype.loadJson = function(json_object){
this._page_list = json_object._page_list;
this._current_page = json_object._current_page;
};
PageArray.prototype.saveJson = function(){
let output = {};
output._page_list = this._page_list;
output._current_page = this._current_page;
return output;
};
PageArray.prototype.exportJson = function(){
return this._page_list;
};