js面向对象开发互联网机顶盒应用头端之二


/**
* Dare Movie Object.
* @constructor
*/

//声明构造函数 构造函数初始化变量
Dare.Movie = function() {
  this.parent = new Dare.Util();
  this.className = "Dare.Movie";

  //----类Dare.Movie全局属性变量-----//
  this.stylePath = dareStyle.getStylePath();
  this.pageIndex = 0; //当前页索引
  this.pageTotal = 0; //总页数;
  this.pageSize = 8; //每页显示记录数
  this.recordCount = 0; //总记录数
  this.dataSource = null; //数据源
  this.tempdataSource; //数据源

  this.orderBy = 'sortdate'; //默认排序字段
  this.sortDirection = 'asc'; //默认升序方式
  this.searchValue = '';
  this.currentFocus = 'searchvalue'; //初始化焦点
  this.bookMovies = { movies: [] };
  //----类Dare.Movie全局属性变量-----//

  this.selectImages = { "normal": this.stylePath + "selectall.gif", "focus": this.stylePath + "selectallover.gif", "on": this.stylePath + "selectallon.gif" };
  this.unselectImages = { "normal": this.stylePath + "unselectall.gif", "focus": this.stylePath + "unselectallover.gif", "on": this.stylePath + "unselectallon.gif" };
  this.Allselected = false; //全选状态,按下后变为true ,在按为false
  this.hasGetChspell = false; //获取拼音的状态,第一次查询后获取
};
Dare.Movie.prototype = new Dare.Util(); //继承父类 Dare.Util是系统架构工具类
Dare.Movie.prototype.constructor = Dare.Movie; //引用构造函数
/**
* @function movieInit
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.movieInit = function() {
  sortimg.innerText = DareLang.DATETIME;
  searchimg.innerText = DareLang.SEARCH;
  sortdate.innerText = DareLang.DATETIME;
  sortname.innerText = DareLang.NAME;
  sorttype.innerText = DareLang.TYPE;
  sortsize.innerText = DareLang.SIZE;
  this.$('title').src = this.stylePath + DareLang.IMAGE.MY_MOVIE_TITLE; //'movie_title.gif';
  this.$('imgtop').src = this.stylePath + 'movie_no.png';
  this.$('selectAllimg').src = this.unselectImages.normal;
  this.$('freshimg').src = this.stylePath + 'no.gif';
  var json = new Dare.JSObject.JSON();
  var cmd = new Dare.Business.Command();
  this.dataSource = cmd.getFiles('movie', 'date', '');

  var request = new Dare.Request();
  var index = request.QueryString('pageIndex');
  this.pageIndex = index == null ? 0 : parseInt(index);
 
  this.tempdataSource = this.dataSource;
  this.recordCount = this.dataSource.length;
  this.pageTotal = this.recordCount % this.pageSize == 0 ? this.recordCount / this.pageSize : parseInt(this.recordCount / this.pageSize + 1);
  this.movieDataBind();

};
Dare.Movie.prototype.sort = function() {
  var sortname = 'date';

  switch (this.orderBy) {
    case 'sortname':
      sortname = 'name';
      if (this.hasGetChspell == false) {
        for (var j in this.tempdataSource) {
          this.tempdataSource[j].chname = chineseSpell.getpy(dareUtil.getFileName(this.tempdataSource[j]["name"]), 4).toString().toLowerCase();
        }
        this.dataSource = this.tempdataSource;
        this.hasGetChspell = true;
      }
      break;
    case 'sorttype':
      sortname = 'type';
      break;
    case 'sortsize':
      sortname = 'size';
      break;
    case 'sortdate':
      sortname = 'date';
      break;
    default:
      sortname = 'date';
      break;
  }
  dareUtil.sortBy(this.dataSource, sortname, this.sortDirection);
  this.movieDataBind();
}
Dare.Movie.prototype.loading = function() {
  this.$('freshimg').src = this.stylePath + 'loading.gif';
};
Dare.Movie.prototype.closeLoading = function() {
  this.$('freshimg').src = this.stylePath + 'no.gif';
};
/**
* @function movieDataBind
* @author
* @return this.$('maincontent').innerHTML
*/
Dare.Movie.prototype.movieDataBind = function() {
  this.loading();
  var start = parseInt(this.pageIndex * this.pageSize);
  var end = parseInt(this.pageIndex * this.pageSize + this.pageSize);
  if (end > this.recordCount) end = this.recordCount;

  var item = '';
  var i = 0;

  for (var j = start; j < end; j++) {
    if (this.dataSource[j] == null) break;
    var itemId = 'item' + i;
    var selectId = 'select' + i;
    var movieId = 'movie' + i;
    var name = dareUtil.getFileName(this.dataSource[j]['name']);
    var subName = dareUtil.trimstr(name, 20);
    var movieObj = '{name:"' + this.dataSource[j]['name'] + '",type:"' + this.dataSource[j]['type'] + '",size:"' + this.dataSource[j]['size'] + '",time:"' + this.secondsToMMSS(this.dataSource[j]['time']) + '",scale:"' + this.dataSource[j]['scale'] + '",date:"' + this.dataSource[j]['date'] + '"}';
    item += '<div  class="itemnone" id="' + itemId + '"><div id="' + movieId + '" style="display:none;">' + movieObj + '</div><div class="selectnone" id="' + selectId + '" ></div>' +
      '<div class="menufont">' + subName + '</div>' +
      '<div class="menusize">' + this.secondsToMMSS(this.dataSource[j]['time']) + '</div>'
    '</div>';

    i++;

  }

  this.$('movies').innerHTML = item;
  if (this.$('item0')) {
    this.$('item0').className = 'item';
    this.currentFocus = 'item0';
    this.movieDetail('movie0');
    this.$('pagetext').innerHTML = this.pageTotal == 0 ? '' : (this.pageIndex + 1) + "/" + this.pageTotal;
  }
  this.bookMovieBind();
  this.closeLoading();
};
/**
* @function movieDetail
* @author
* @return this.$('detail').innerHTML
*/
Dare.Movie.prototype.movieDetail = function(id) {
  var movie = this.$(id).innerText;
  var json = new Dare.JSObject.JSON();
  var movieArray = json.toObject(movie);
  var detailContent = '';

  detailContent += DareLang.NAME + ': ' + dareUtil.getFileName(movieArray["name"]) + '<br>';
  detailContent += DareLang.TYPE + ': ' + movieArray["type"] + '<br>';
  detailContent += DareLang.SIZE + ': ' + dareUtil.fileSizeInt2Str(movieArray["size"]) + '<br>';
  detailContent += DareLang.DURATION + ': ' + movieArray["time"] + '<br>';
  detailContent += DareLang.RESOLUTION + ': ' + movieArray["scale"] + '<br>';
  detailContent += DareLang.DATETIME + ': ' + movieArray["date"];

  this.$('detail').innerHTML = detailContent;
};
/**
* @function previousPage
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.previousPage = function() {
  this.pageIndex--;
  if (this.pageIndex < 0) { this.pageIndex = 0; return; }
  if (this.pageIndex <= this.pageTotal - 1 && this.pageIndex >= 0) {
    this.movieDataBind();
    this.$('item0').className = 'item';
  }
};
/**
* @function nextPage
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.nextPage = function() {
  this.pageIndex++;
  if (this.pageIndex >= this.pageTotal) { this.pageIndex = this.pageTotal - 1; return; }
  if (this.pageIndex <= this.pageTotal - 1) {
    this.movieDataBind();
  }
};
Dare.Movie.prototype.searchLostFocus = function() {
  document.getElementById('searchvalue').blur();
  document.getElementById('fresh').focus();
}
/**
* @function keypressHandler
* @author
* @param event
* @return null
*/
Dare.Movie.prototype.keypressHandler = function(event) {
  var keyValue = Dare.isiPanel ? event.which : window.event.keyCode;
  //document.getElementById("divDebug").innerText += "keyValue:" + keyValue+"</br>";
  showUSBInfo(keyValue);
  this.searchLostFocus();
  switch (keyValue) {
    case KeyMap.STB_KEY_DOWN: //向下
      this.keydownHandler();
      break;
    case KeyMap.STB_KEY_UP: //向上
      this.keyupHandler();
      break;
    case KeyMap.STB_KEY_ENTER: //确定
      this.keyenterHandler();
      break;
    case KeyMap.STB_KEY_LEFT: //向左
      this.keyleftHandler();
      break;
    case KeyMap.STB_KEY_RIGHT: //向右
      this.keyrightHandler();
      break;
    case KeyMap.STB_KEY_PAGEUP: //上一页
      this.keypageupHandler();
      break;
    case KeyMap.STB_KEY_PAGEDOWN: //下一页
      this.keypagedownHandler();
      break;
    case KeyMap.STB_KEY_HOME:
      break;
    case KeyMap.STB_KEY_STOP:
      window.location.href = '../main/index.html?focus=1';
      break;
    case KeyMap.STB_KEY_FULLSCREEN:
      this.focusToolbar();
      break;
    default:
      break;
  }
};
/**
* 回到菜单
*/
Dare.Movie.prototype.focusToolbar = function () {
  document.getElementById('searchvalue').focus();
  this.currentFocus = 'searchvalue';
  for (var i = 0; i < 8; i++) {
    document.getElementById("item" + i).className = "itemnone";
  }
}
/**
* @function keyupHandler
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.keyupHandler = function() {
  if (this.currentFocus == "searchvalue") {
    this.$('searchvalue').focus();
    return;
  }
  switch (this.currentFocus) {
    case 'item0':
      if (this.pageIndex == 0) {
        this.currentFocus = 'searchvalue';
        this.$('item0').className = 'itemnone';
        searchvalue.focus();
      }
      break;
    case 'sortsize': //大小
      this.$('sortsize').className = 'sortnone';
      this.$('sorttype').className = 'sortselected';
      this.currentFocus = 'sorttype';
      break;
    case 'sorttype': //类型
      this.$('sorttype').className = 'sortnone';
      this.$('sortname').className = 'sortselected';
      this.currentFocus = 'sortname';
      break;
    case 'sortname': //名称
      this.$('sortname').className = 'sortnone';
      this.$('sortdate').className = 'sortselected';
      this.currentFocus = 'sortdate';
    default:
      break;
  }

  this.pageEvent = false;
  var foucusId = this.currentFocus.substr(4, 1);
  if ('0' == foucusId) {
    this.previousPage();
    this.pageEvent = true;
  }
  if (parseInt(foucusId) <= 7 && this.pageEvent == false) {
    this.listUpFocus(foucusId);
  }
};
/**
* @function listDownFocus
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.listDownFocus = function(i) {
  this.pageEvent = false;
  var id = parseInt(i) + 1;
  if (this.$('item' + (id))) {
    this.$('item' + i).className = 'itemnone';
    this.$('item' + (id)).className = 'item';
    this.currentFocus = 'item' + (id);
    this.rightPressCount = 0;
    this.movieDetail('movie' + (id));
  }
};
/**
* @function listUpFocus
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.listUpFocus = function(i) {
  this.pageEvent = false;
  this.$('item' + i).className = 'itemnone';
  var id = parseInt(i) - 1;
  this.$('item' + (id)).className = 'item';
  this.currentFocus = 'item' + (id);
  this.rightPressCount = 0;
  this.movieDetail('movie' + (id));
};
/**
* @function keydownHandler
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.keydownHandler = function() {
  if (this.currentFocus.indexOf('item') < 0
   && this.currentFocus.indexOf('sort') < 0
   && this.$('item0')
   && this.$('sortlist').style.display == 'none') {
    this.$('searchimg').className = 'search';
    this.$('sortimg').className = 'search';
    if (this.sortDirection == 'asc') this.$('arrowimg').className = 'arrowup';
    else this.$('arrowimg').className = 'arrowdown';
    this.currentFocus = 'item0';
    this.$('item0').className = 'item';
    return;
  }
  if (sortlist.style.display != "none") {
    switch (this.currentFocus) {
      case 'sortimg':
        this.$('sortdate').className = 'sortselected';
        this.currentFocus = 'sortdate';
        break;
      case 'sortdate': //日期
        this.$('sortdate').className = 'sortnone';
        this.$('sortname').className = 'sortselected';
        this.currentFocus = 'sortname';
        break;
      case 'sortname': //名称
        this.$('sortname').className = 'sortnone';
        this.$('sorttype').className = 'sortselected';
        this.currentFocus = 'sorttype';
        break;
      case 'sorttype': //类型
        this.$('sorttype').className = 'sortnone';
        this.$('sortsize').className = 'sortselected';
        this.currentFocus = 'sortsize';
      default:
        break;
    }
  }
  this.pageEvent = false;
  var foucusId = this.currentFocus.substr(4, 1);
  if ('7' == foucusId) {
    this.nextPage();
    this.pageEvent = true;
    return;
  }
  if (parseInt(foucusId) < 7 && this.pageEvent == false) {
    this.listDownFocus(foucusId);
  }
};
/**
* @function keyenterHandler
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.keyenterHandler = function() {
  if (this.currentFocus == 'sortimg') {
    this.$('sortlist').style.display = 'block';
    return;
  }

  if (this.orderBy != this.currentFocus && (this.currentFocus == 'sortdate' || this.currentFocus == 'sortname' || this.currentFocus == 'sorttype' || this.currentFocus == 'sortsize')) {
    this.$('sortlist').style.display = "none";
    this.$('sortimg').innerText = this.$(this.currentFocus).innerText;
    this.orderBy = this.currentFocus;

    this.$(this.currentFocus).className = 'sortnone';
    this.$('sortimg').className = 'search';
    this.currentFocus = 'arrowimg';

  }

  switch (this.currentFocus) {
    case 'arrowimg':
      if (this.sortDirection == 'asc') {
        this.sortDirection = 'desc';
        this.$('arrowimg').className = 'arrowdownon';
      }
      else {
        this.sortDirection = 'asc';
        this.$('arrowimg').className = 'arrowupon';
      }
      this.sort();
      return; case 'searchimg':
      this.$('searchimg').className = 'searchon';
      this.movieSeacher();
      return; case 'sortimg':
      this.$('sortimg').className = 'searchon';
      break;
    default:
      break;
  }
  if (this.currentFocus == 'selectAllimg') {
    this.Allselected = !this.Allselected; //取反
    if (this.Allselected) {
      this.selectAll();
    }
    else {
      this.unSelectAll();
    }
  }
  var hasMovie = false;
  if (this.currentFocus.indexOf('item') >= 0) {
    var movies = this.bookMovies.movies;
    var json = new Dare.JSObject.JSON();
    if (movies.length != 0) {
      var movieList = json.parse(movies);
      new Dare.Business.Command().setGlobalVar("movieBookList", movieList);
      hasMovie = true;
    }
    else {
      var focusId = this.currentFocus.substr(4, 1);
      var moviestr = this.$('movie' + focusId).innerText;
      var json = new Dare.JSObject.JSON();
      var movie = json.toObject(moviestr);
      movies.push(movie);
      var movieList = json.parse(movies);
      new Dare.Business.Command().setGlobalVar('movieBookList', movieList);
      //选片对象有实例 按确定转向播放页面
      hasMovie = true;
    }
    if (hasMovie) {
      var currentMedia = new Dare.Business.Command().getGlobalVar('currentPlayMedia');
      var json = new Dare.JSObject.JSON();
      var mp = json.toObject(currentMedia);
      var currentPlayMovieInPlayList = false;
      for (var k in movies) {
        if (movies[k].name == mp.playPath) {
          currentPlayMovieInPlayList = true;
          break;
        }
      }
      window.location.href = '../movie/movieplay.html?appendback=true&currentPlayMovieInPlayList=' + currentPlayMovieInPlayList + '&pageIndex=' + this.pageIndex;
    }
  }
};
Dare.Movie.prototype.selectAll = function() {
  var movies = dareMovie.bookMovies.movies;
  for (var f in dareMovie.dataSource) {
    var movie = dareMovie.dataSource[f];
    movies.push(movie);
  }
  for (var i = 0; i < 8; i++) {
    if (this.$('item' + i)) {
      this.$('select' + i).className = 'select';
    }
  }
  this.$('selectAllimg').src = this.selectImages.on;
};
/**
* 全不选
*/
Dare.Movie.prototype.unSelectAll = function() {
  this.$('selectAllimg').src = this.unselectImages.on;
  this.bookMovies.movies = [];
  for (var i = 0; i < 8; i++) {
    if (this.$('item' + i)) {
      this.$('select' + i).className = 'selectnone';
    }
  }
};
Dare.Movie.prototype.arrowImageFocus = function() {
  if (this.sortDirection == "asc") {
    document.getElementById('arrowimg').className = 'arrowupover';
  } else {
    document.getElementById('arrowimg').className = 'arrowdownover';
  }
};
Dare.Movie.prototype.arrowImageLostFocus = function() {
  if (this.sortDirection == "asc") {
    document.getElementById('arrowimg').className = 'arrowup';
  }
  else {
    document.getElementById('arrowimg').className = 'arrowdown';
  }
};
/**
* @function keyleftHandler
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.keyleftHandler = function() {
  this.$('sortlist').style.display = 'none';
  if (this.currentFocus.indexOf('sort') == 0) {
    this.currentFocus = 'searchimg';
    this.$('searchimg').className = 'searchover';
    this.$('sortimg').className = 'search';
    return;
  }
  switch (this.currentFocus) {
    case 'arrowimg':
      if (this.sortDirection == 'asc') {
        this.$('arrowimg').className = 'arrowup';
      } else {
        this.$('arrowimg').className = 'arrowdown';
      }
      this.$('sortimg').className = 'searchover';
      this.currentFocus = 'sortimg';
      break;
    case 'sortimg':
      this.$('sortimg').className = 'search';
      this.$('searchimg').className = 'searchover';
      this.currentFocus = 'searchimg';
      break;
    case 'searchimg':
      this.currentFocus = 'searchvalue';
      document.getElementById('searchimg').className = 'search';
      searchvalue.focus();
      break;
    case 'selectAllimg':
      if (this.Allselected) { this.$('selectAllimg').src = this.selectImages.normal; }
      else this.$('selectAllimg').src = this.unselectImages.normal;
      this.arrowImageFocus();
      this.currentFocus = 'arrowimg';
      break;
    default:
      break;
  }
};
/**
* @function keyrightHandler
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.keyrightHandler = function() {
  this.$('sortlist').style.display = 'none';
  if (this.currentFocus.indexOf('sort') == 0) {
    this.currentFocus = 'arrowimg';
    this.$('sortimg').className = 'search';
    if (this.sortDirection == "asc") {
      this.$('arrowimg').className = 'arrowupover';
    } else {
      this.$('arrowimg').className = 'arrowdownover';
    }
    return;
  }
  switch (this.currentFocus) {
    case 'searchvalue':
      this.$('searchimg').className = 'searchover';
      this.currentFocus = 'searchimg';
      break;
    case 'searchimg':
      this.$('searchimg').className = 'search';
      this.$('sortimg').className = 'searchover';
      this.currentFocus = 'sortimg';
      break;
    case 'sortimg':
      this.$('sortimg').className = 'search';
      if (this.sortDirection == "asc") {
        this.$('arrowimg').className = 'arrowupover';
      } else {
        this.$('arrowimg').className = 'arrowdownover';
      }
      this.currentFocus = 'arrowimg';
      break;
    case "arrowimg":
      if (this.Allselected) { this.$('selectAllimg').src = this.selectImages.focus; }
      else this.$('selectAllimg').src = this.unselectImages.focus;
      this.arrowImageLostFocus();
      this.currentFocus = 'selectAllimg';
      break;
    default:
      break;
  }

  if (this.currentFocus.indexOf('item') >= 0) {
    var focusId = this.currentFocus.substr(4, 1);
    var selectObj = this.$('select' + focusId);
    var movieString = this.$('movie' + focusId).innerText;
    var json = new Dare.JSObject.JSON();
    var movie = json.toObject(movieString);

    if (selectObj.className == 'selectnone') {
      selectObj.className = 'select';
      this.selectMovie(movie, 0);
    }
    else {
      selectObj.className = 'selectnone';
      this.selectMovie(movie, 1);
    }

  }
};
/**
* @function keypageupHandler
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.keypageupHandler = function() {
  this.previousPage();
};
/**
* @function keypagedownHandler
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.keypagedownHandler = function() {
  this.nextPage();
};

/**
* @function selectMovie 选择
* @author
* @param movie type
* @return null
*/
Dare.Movie.prototype.selectMovie = function(movie, type) {
  var movies = this.bookMovies.movies;
  switch (type) {
    //添加对象                                                                          
    case 0:
      movies.push(movie);
      break;
    //移除对象                                                                 
    case 1:
      for (var i in movies) {
        if (movies[i].name == movie.name) {
          movies.splice(i, 1);
          break;
        }
      }
      break;
    default:
      break;
  }
};
/**
* @function bookMovieBind
* @author
* @return null
*/
Dare.Movie.prototype.bookMovieBind = function() {
  var start = parseInt(this.pageIndex * this.pageSize);
  var end = parseInt(this.pageIndex * this.pageSize + this.pageSize);
  if (end > this.dataSource.length) end = this.dataSource.length;

  var movies = this.bookMovies.movies;
  var json = new Dare.JSObject.JSON();
  var movieBookList = new Dare.Business.Command().getGlobalVar("movieBookList");
  if (movieBookList.length != 0) {
    var movieBooks = json.toObject(movieBookList);
    for (var key in movieBooks) {
      movies.push(movieBooks[key]);
    }
    new Dare.Business.Command().setGlobalVar('movieBookList', '');
  }

  var i = 0;
  for (var j = start; j < end; j++) {
    var selectObj = this.$('select' + i);
    var movieString = this.$('movie' + i).innerText;
    var json = new Dare.JSObject.JSON();
    var movie = json.toObject(movieString);
    for (var key in movies) {
      if (movies[key].name == movie.name) {
        selectObj.className = 'select';
        break;
      }
    }
    i++;
  }

};
/**
* @function styleInt
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.styleInt = function() {
  this.$('searchimg').className = 'search';
  this.$('sortimg').className = 'search';
  this.$('arrowimg').className = 'arrowup';
};
/**
* @function movieSeacher 查询
* @author
* @param null
* @return null
*/
Dare.Movie.prototype.movieSeacher = function() {
  if (this.hasGetChspell == false) {
    for (var j in this.tempdataSource) {
      this.tempdataSource[j].chname = chineseSpell.getpy(dareUtil.getFileName(this.tempdataSource[j]["name"]), 4).toString().toLowerCase();
    }
    this.dataSource = this.tempdataSource;

    this.hasGetChspell = true;
  }
  var keyWord = this.$('searchvalue').value;
  var resultData = []; //交换数组
  var i = 0;
  for (var key in this.tempdataSource) {
    if (dareUtil.getFileName(this.tempdataSource[key].name).indexOf(keyWord) != -1 || this.tempdataSource[key].chname.indexOf(keyWord) == 0) {

      resultData.push(this.tempdataSource[key]); //从缓存数据库原读取,便于二次查询
    }
  }
  this.dataSource = resultData;
  this.recordCount = this.dataSource.length;

  this.pageTotal = this.recordCount % this.pageSize == 0 ? this.recordCount / this.pageSize : parseInt(this.recordCount / this.pageSize + 1);
  this.movieDataBind();
};
var dareMovie = new Dare.Movie();
document.onkeypress = function(event) {
  dareMovie.keypressHandler(event);
};

原文地址:https://www.cnblogs.com/fx2008/p/2147239.html