创新实训项目分析(十五)

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;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值