tvSysBtnBind.v2.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. try{
  2. (function(arr) {
  3. //当前元素删除
  4. arr.forEach(function(item) {
  5. if (item.hasOwnProperty('remove')) {
  6. return
  7. }
  8. Object.defineProperty(item, 'remove', {
  9. configurable: true,
  10. enumerable: true,
  11. writable: true,
  12. value: function remove() {
  13. this.parentNode.removeChild(this)
  14. }
  15. })
  16. })
  17. })([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
  18. if (!("classList" in document.documentElement)) {
  19. // classList 兼容
  20. Object.defineProperty(HTMLElement.prototype, 'classList', {
  21. get: function() {
  22. var self = this;
  23. function update(fn) {
  24. return function(value) {
  25. var classes = self.className.split(/\s+/g),
  26. index = classes.indexOf(value);
  27. fn(classes, index, value);
  28. self.className = classes.join(" ")
  29. }
  30. }
  31. return {
  32. add: update(function(classes, index, value) {
  33. if (!~index) classes.push(value)
  34. }),
  35. remove: update(function(classes, index) {
  36. if (~index) classes.splice(index, 1)
  37. }),
  38. toggle: update(function(classes, index, value) {
  39. if (~index) classes.splice(index, 1);
  40. else classes.push(value)
  41. }),
  42. contains: function(value) {
  43. return !!~self.className.split(/\s+/g).indexOf(value)
  44. },
  45. item: function(i) {
  46. return self.className.split(/\s+/g)[i] || null
  47. }
  48. }
  49. }
  50. })
  51. }
  52. function fireKeyEvent(el, evtType, keyCode) {
  53. //创建事件
  54. var doc = el.ownerDocument,
  55. win = doc.defaultView || doc.parentWindow,
  56. evtObj;
  57. if (doc.createEvent) {
  58. if (win.KeyEvent) {
  59. evtObj = doc.createEvent('KeyEvents');
  60. evtObj.initKeyEvent(evtType, true, true, win, false, false, false, false, keyCode, 0)
  61. } else {
  62. evtObj = doc.createEvent('UIEvents');
  63. Object.defineProperty(evtObj, 'keyCode', {
  64. get: function() {
  65. return this.keyCodeVal
  66. }
  67. });
  68. Object.defineProperty(evtObj, 'which', {
  69. get: function() {
  70. return this.keyCodeVal
  71. }
  72. });
  73. evtObj.initUIEvent(evtType, true, true, win, 1);
  74. evtObj.keyCodeVal = keyCode;
  75. if (evtObj.keyCode !== keyCode) {
  76. // console.log("keyCode " + evtObj.keyCode + " 和 (" + evtObj.which + ") 不匹配")
  77. }
  78. }
  79. el.dispatchEvent(evtObj)
  80. } else if (doc.createEventObject) {
  81. evtObj = doc.createEventObject();
  82. evtObj.keyCode = keyCode;
  83. el.fireEvent('on' + evtType, evtObj)
  84. }
  85. }
  86. }catch(e){
  87. //版本很低
  88. }
  89. function addClass(ele, cls) {
  90. if(ele.classList){
  91. ele.classList.add(cls);
  92. }else{
  93. if (!this.hasClass(ele, cls)) ele.className += " " + cls;
  94. }
  95. }
  96. function arrIndexOf(arr, v) {
  97. for (var i = 0; i < arr.length; i++) {
  98. if (arr[i] == v) {
  99. return i;
  100. }
  101. }
  102. return -1;
  103. }
  104. //删除指定dom元素的样式
  105. function removeClass(ele, cls) {
  106. if(ele.classList){
  107. ele.classList.remove(cls);
  108. }else{
  109. if (ele.className != '' && hasClass(ele, cls)) {
  110. var arrClassName = ele.className.split(' ');
  111. var classIndex = arrIndexOf(arrClassName, cls);
  112. if (classIndex!==-1) {
  113. arrClassName.splice(classIndex, 1);
  114. ele.className = arrClassName.join(' ');
  115. }
  116. }
  117. }
  118. }
  119. //如果存在(不存在),就删除(添加)一个样式
  120. function toggleClass(ele,cls){
  121. if(hasClass(ele,cls)){
  122. removeClass(ele, cls);
  123. }else{
  124. addClass(ele, cls);
  125. }
  126. }
  127. function hasClass(element, cls) {
  128. return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
  129. }
  130. // function hasClass(tagStr,classStr){
  131. // if(tagStr.classList){
  132. // return tagStr.classList.contains(classStr);
  133. // } else{
  134. // var arr=tagStr.className.split(/\s+/ ); //这个正则表达式是因为class可以有多个,判断是否包含
  135. // for (var i=0;i<arr.length;i++){
  136. // if (arr[i]==classStr){
  137. // return true ;
  138. // }
  139. // }
  140. // return false
  141. // }
  142. // }
  143. function getClassNames(classStr,target, tagName) {
  144. /* classStr 样式名(必须) 目标元素 标签类型 */
  145. target= target?target:document;
  146. tagName = tagName?tagName:"*";
  147. if (document.getElementsByClassName) {
  148. return target.getElementsByClassName(classStr)
  149. } else {
  150. var nodes = target.getElementsByTagName(tagName),
  151. ret = [];
  152. for (i = 0; i < nodes.length; i++) {
  153. if (hasClass(nodes[i], classStr)) {
  154. ret.push(nodes[i])
  155. }
  156. }
  157. return ret;
  158. }
  159. }
  160. function getElementLeft(ele) {
  161. var actualLeft = ele.offsetLeft;
  162. var current = ele.offsetParent;
  163. // 如果当前元素不是根元素
  164. while (current !== null) {
  165. actualLeft += current.offsetLeft;
  166. current = current.offsetParent;
  167. }
  168. return actualLeft;
  169. }
  170. function getElementTop(ele) {
  171. var actualTop = ele.offsetTop;
  172. var current = ele.offsetParent;
  173. while (current !== null) {
  174. actualTop += current.offsetTop;
  175. current = current.offsetParent;
  176. }
  177. return actualTop;
  178. }
  179. function getBoundingClientRect(ele) {
  180. // 该方法是计算当前元素距离当前视口的距离,所以需要得到页面的滚动距离
  181. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  182. var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
  183. // 如果浏览器支持该方法
  184. if (ele.getBoundingClientRect) {
  185. if (typeof arguments.callee.offset !== 'number') {
  186. //不同浏览器中,元素的默认位置不同。为了统一起见,需要新创建一个元素
  187. var temp = document.createElement('div');
  188. temp.style.cssText = "position:absolute;top:0;left:0";
  189. document.body.appendChild(temp);
  190. arguments.callee.offset = -temp.getBoundingClientRect().top - scrollTop;
  191. document.body.removeChild(temp);
  192. temp = null;
  193. }
  194. var rect = ele.getBoundingClientRect();
  195. var offset = arguments.callee.offset;
  196. return {
  197. left: rect.left + offset,
  198. right: rect.right + offset,
  199. top: rect.top + offset,
  200. bottom: rect.bottom + offset,
  201. width:(rect.right + offset)-(rect.left + offset),
  202. height:(rect.bottom + offset)-(rect.top + offset)
  203. }
  204. } else {
  205. //当前浏览器不支持该方法
  206. var actualLeft = getElementLeft(ele);
  207. var actualTop = getElementTop(ele);
  208. var offsetWidth=ele.offsetWidth;
  209. var offsetHeight=ele.offsetHeight;
  210. return {
  211. left: actualLeft - scrollLeft,
  212. right: actualLeft + offsetWidth - scrollLeft,
  213. top: actualTop - scrollTop,
  214. bottom: actualTop + offsetHeight - scrollTop,
  215. width:(actualLeft + offsetWidth- scrollLeft) -(actualLeft - scrollLeft),
  216. height:(actualTop + offsetHeight - scrollTop)-(actualTop - scrollTop)
  217. }
  218. }
  219. }
  220. (function(window) {
  221. var tvSysBtnBind = function(init) {
  222. var _this = this,
  223. _self = self;
  224. var id = init.id ? init.id : null,
  225. keyRemoveDefault = typeof init.keyRemoveDefault == "undefined" ? false : init.keyRemoveDefault,
  226. currentIndex = init.currentIndex ? parseInt(init.currentIndex) : 0,
  227. btnLeft = init.btnLeft ? init.btnLeft : 37,
  228. btnUp = init.btnUp ? init.btnUp : 38,
  229. btnRight = init.btnRight ? init.btnRight : 39,
  230. btnDown = init.btnDown ? init.btnDown : 40,
  231. btnEnter = init.btnEnter ? init.btnEnter : 13,
  232. history = typeof init.history == "undefined" ? true : init.history,
  233. isFloatLast = typeof init.isFloatLast == "undefined" ? false : init.isFloatLast,
  234. isCentered = typeof init.isCentered == "undefined" ? true : init.isCentered,
  235. currentClass = init.currentClass ? init.currentClass : "current",
  236. effect = init.effect ? init.effect : "slide1",
  237. element = new Array(),
  238. rules = init.rules,
  239. direction = "y";
  240. _this.className = init.className ? init.className : "hotbutton";
  241. this.event = {};
  242. var _tempElem;
  243. this.currentIndex = parseInt(currentIndex);
  244. this.defaultIndex = parseInt(currentIndex);
  245. this.currentClass = currentClass;
  246. this.historyFocus = {};
  247. if (!window.focusobj) window.focusobj = document.createElement("span");
  248. (typeof init.onLoad) == "function" ? init.onLoad : init.onLoad = function() {};
  249. (typeof init.onBack) == "function" ? init.onLoad : init.onBack = function() {};
  250. this.onLoad = function() {
  251. focusobj.innerHTML = '<div class="cssbk"><b class="lt"></b><b class="t"></b><b class="rt"></b><b class="r"></b><b class="rb"></b><b class="b"></b><b class="lb"></b> <b class="l"></b></div>';
  252. addClass(focusobj,"focusobj");
  253. addClass(focusobj,"current");
  254. focusobj.style.display = "none";
  255. _this.target = init.id ? document.getElementById(init.id) : document.body;
  256. _this.defaultTarget = _this.target;
  257. _this.currentId = init.id ? init.id : "Jdoc";
  258. _this.reLoad();
  259. _this.sourceClassName = _this.className;
  260. _this.sourceLength = element.length;
  261. _this.prev = element[currentIndex];
  262. _this.prevIndex = currentIndex;
  263. _this.current = element[currentIndex];
  264. _this.currentIndex = currentIndex;
  265. _this.target.appendChild(focusobj);
  266. init.onLoad.call(_this);
  267. }
  268. self.getCurRule=function(){
  269. var obj = {};
  270. if (rules) {
  271. obj = rules["#" + _this.currentId + ">." + _this.className];
  272. if (typeof obj == "undefined") obj = rules[_this.className]
  273. }
  274. return obj
  275. }
  276. this.reSetClass = function(item, index, curClass) {
  277. var obj =_self.getCurRule();
  278. if(typeof obj["history"]=="undefined") obj["history"]=history; //默认历史记录开关
  279. if (obj["history"]) _this.historyFocus[_this.currentId+_this.className] = _this.currentIndex; //当前历史开关
  280. //index = index ? index : 0;
  281. _this.prevCurrentClass = _this.currentClass;
  282. if (curClass) {
  283. _this.currentClass = curClass
  284. }
  285. var arr = item.split(">");
  286. for (var i = 0; i < arr.length; i++) {
  287. if (arr[i] == "") arr.splice(i)
  288. }
  289. for (var i = 0; i < arr.length; i++) {
  290. if (arr[i].indexOf("#") != -1) {
  291. _this.currentId=arr[i].replace("#", "");
  292. if (document.getElementById( _this.currentId)) _this.target = document.getElementById( _this.currentId);
  293. }
  294. if (arr[i].indexOf(".") != -1) {
  295. item = arr[i].replace(".", "")
  296. }
  297. if (arr[i].indexOf(".") == -1 && arr[i].indexOf("#") == -1) {
  298. item = arr[i]
  299. }
  300. }
  301. if (getClassNames(item,_this.target).length > 0) {
  302. _self.newItem(item, index)
  303. } else {
  304. _this.target = _this.defaultTarget;
  305. if (getClassNames(item,_this.target).length > 0) _self.newItem(item, index)
  306. }
  307. }
  308. self.newItem = function(item, index) {
  309. if (_this.prev) removeClass(_this.prev,_this.currentClass);
  310. //_this.target=document.getElementById(_this.currentId);
  311. if(typeof _this.historyFocus[_this.currentId+item]=="undefined")
  312. _this.historyFocus[_this.currentId+item]=0;
  313. _this.className = item;
  314. _this.prevIndex = _this.currentIndex = typeof index!="undefined" ?index: _this.historyFocus[_this.currentId+item];
  315. _this.reLoad();
  316. }
  317. self.readFn = function() {
  318. if ((typeof init.onEnterPress) == "function") {
  319. alert(this.className+'readFn');
  320. init.onEnterPress
  321. } else {
  322. init.onEnterPress = function() {}
  323. }
  324. if ((typeof init.onPress) == "function") {
  325. init.onPress
  326. } else {
  327. init.onPress = function() {}
  328. }
  329. }
  330. this.reLoad = function() {
  331. self.readFn();
  332. element =getClassNames(_this.className,_this.target);
  333. this.hotbtn = element;
  334. if (element.length <= 0) return false;
  335. if (_this.currentIndex || _this.currentIndex == 0) {
  336. _this.currentIndex = parseInt(_this.currentIndex)
  337. } else {
  338. _this.currentIndex = parseInt(init.currentIndex)
  339. }
  340. if (isload >= 2 && _this.sourceClass == _this.className && _this.sourceLength != element.length && !isSet) {
  341. if (hasClass(_this.prev,_this.className) && _this.sourceLength > element.length) {
  342. _this.currentIndex = _this.currentIndex - (_this.sourceLength - element.length);
  343. _this.prevIndex = _this.prevIndex - (_this.sourceLength - element.length);
  344. _this.sourceLength = element.length
  345. } else if (hasClass(_this.prev,_this.className) && _this.sourceLength < element.length) {
  346. _this.currentIndex = _this.currentIndex + (element.length - _this.sourceLength);
  347. _this.prevIndex = _this.prevIndex + (element.length - _this.sourceLength);
  348. _this.sourceLength = element.length
  349. }
  350. }
  351. isSet = false;
  352. _this.current = element[_this.currentIndex];
  353. _this.currentIndex = _this.currentIndex;
  354. _self.classDo(_this.currentIndex);
  355. for (var i = 0; i < this.hotbtn.length; i++) {
  356. this.hotbtn[i].setAttribute("data-id", i)
  357. }
  358. }
  359. function keydefault(e) {
  360. try {
  361. if (keyRemoveDefault) window.event ? window.event.returnValue = false : e.preventDefault()
  362. } catch (e) {}
  363. }
  364. var isSet = false;
  365. this.setCurrentIndex = function(index) {
  366. isSet = true;
  367. index = parseInt(index);
  368. _this.currentIndex = index;
  369. _this.current = element[index]
  370. }
  371. var maxTop = 0;
  372. this.viewScrollY = function(y,view) {
  373. var obj = self.getCurRule();
  374. // var view = _this.current.parentNode.parentNode;
  375. var sumtop = obj["directionY"] ? parseInt(obj["directionY"]) : 0;
  376. var top = sumtop + y + view.clientHeight / 2 - Math.ceil(_this.current.clientHeight / 2);
  377. if (top > 0) top = 0;
  378. obj["directionY"]=top;
  379. view.children[0].style.top = top + "px"
  380. }
  381. var maxLeft = 0;
  382. this.viewScrollX = function(x,view) {
  383. var obj = self.getCurRule();
  384. // var view = _this.current.parentNode.parentNode;
  385. var sumleft = obj["directionX"] ? parseInt(obj["directionX"]) : 0;
  386. var left = sumleft + x + view.clientWidth / 2 - Math.ceil(_this.current.clientWidth / 2);
  387. if (isCentered) {
  388. if (left > 0) left = 0;
  389. if (obj["maxLeft"]==undefined||obj["maxLeft"]==null) {
  390. maxLeft = getBoundingClientRect(_this.hotbtn[_this.hotbtn.length - 1]).right - getBoundingClientRect(view).right;
  391. obj["maxLeft"]=maxLeft;
  392. }
  393. if (left < sumleft && left < -maxLeft) {
  394. left = -maxLeft
  395. }
  396. // if(maxLeft<view.clientWidth)
  397. // left=0;
  398. }
  399. obj["directionX"]=left;
  400. view.children[0].style.left = left + "px"
  401. }
  402. this.onPress = function(e) {
  403. init.onPress.call(_this)
  404. }
  405. this.onEnterPress = function() {
  406. init.onEnterPress.call(_this)
  407. }
  408. this.onBack = function() {
  409. init.onBack.call(_this)
  410. }
  411. self.getScrollTop = function() {
  412. var scrollTop = 0;
  413. if (document.documentElement && document.documentElement.scrollTop) {
  414. scrollTop = document.documentElement.scrollTop
  415. } else if (document.body) {
  416. scrollTop = document.body.scrollTop
  417. }
  418. return scrollTop
  419. }
  420. this.scroll = function() {
  421. var obj = self.getCurRule();
  422. if (getBoundingClientRect(_this.current).bottom > document.body.clientHeight || getBoundingClientRect(_this.current).top < 0) {
  423. var y = getBoundingClientRect(_this.current).top - (document.body.clientHeight / 2 - _this.current.clientHeight / 2);
  424. window.scrollTo(0, y)
  425. }
  426. var view = _this.current.parentNode.parentNode;
  427. if(obj["directionParent"])view=obj["directionParent"];
  428. // var direction = view.getAttribute("data-scroll-direction");
  429. // if (!direction) {
  430. // view = _this.current.parentNode.parentNode.parentNode;
  431. // direction = view.getAttribute("data-scroll-direction");
  432. // }
  433. if (obj == undefined || !obj["direction"] )return;
  434. view.style.position = "relative";
  435. view.children[0].style.position = "absolute";
  436. var sumleft = obj["directionX"] ? parseInt(obj["directionX"]) : 0;
  437. if (obj["direction"] == "x") {
  438. view.children[0].style.width = _this.hotbtn.length * _this.current.clientWidth * 2 + "px";
  439. var scroll_left = getBoundingClientRect(view).left - getBoundingClientRect(_this.current).left;
  440. if( _this.hotbtn.length * _this.current.clientWidth>view.clientWidth)
  441. _this.viewScrollX(scroll_left,view);
  442. }
  443. var sumleft = obj["directionY"] ? parseInt(obj["directionY"]) : 0;
  444. if (obj["direction"] == "y") {
  445. var scroll_top = getBoundingClientRect(view).top - getBoundingClientRect(_this.current).top;
  446. _this.viewScrollY(scroll_top,view)
  447. }
  448. }
  449. var isload = 0;
  450. self.classDo = function(index) {
  451. isload = isload + 1;
  452. if (isload == 2) {
  453. _this.sourceLength = element.length
  454. }
  455. if (_this.prevCurrentClass) {
  456. var ele = getClassNames(_this.prevCurrentClass,_this.target) ;
  457. for (var i = 0; i < ele.length; i++) {
  458. if (hasClass(ele[i],_this.prevCurrentClass)) {
  459. removeClass(ele[i],_this.prevCurrentClass);
  460. }
  461. }
  462. }
  463. if (element[index]) addClass(element[index],_this.currentClass);
  464. else return;
  465. for (var i = 0; i < element.length; i++) {
  466. if (i != index && hasClass(element[i],_this.currentClass)) {
  467. removeClass(element[i],_this.currentClass);
  468. }
  469. }
  470. _this.scroll();
  471. var effect = element[index].getAttribute("data-effect");
  472. if (effect) {
  473. focusobj.setAttribute("style", " position: fixed; z-index: 19;width:" + (getBoundingClientRect(element[index]).width) + "px ;height:" + (getBoundingClientRect(element[index]).height) + "px; left:" + getBoundingClientRect(element[index]).left + "px;top:" + getBoundingClientRect(element[index]).top + "px;");
  474. focusobj.setAttribute("class", "focusobj current " + effect);
  475. focusobj.style.display = "list-item"
  476. } else {
  477. focusobj.setAttribute("style", "");
  478. focusobj.style.display = "none"
  479. }
  480. }
  481. self.EventUtil = {
  482. add: function(obj, callback) {
  483. if (typeof(obj.onkeypress) == "null") {
  484. obj.onkeypress = function(e) {
  485. callback && callback(e)
  486. }
  487. } else {
  488. obj.onkeydown = function(e) {
  489. callback && callback(e)
  490. }
  491. }
  492. }
  493. }
  494. try{
  495. Webview.requestFocus();
  496. Webview.setKeyEventHandler(function (action, keyCode, keyName, metaState){
  497. var e = this;
  498. e.keyCode = keyName;
  499. _self.onPressdo(e);
  500. // _this.makepress(keyName)
  501. })
  502. }catch(err){
  503. self.EventUtil.add(document, function (e) {
  504. _self.onPressdo(e);
  505. });
  506. }
  507. // EventUtil.add(document, function(e) {
  508. // _self.onPressdo(e)
  509. // });
  510. self.overIndex = function() {
  511. if (_this.currentIndex >= element.length - 1) {
  512. _this.currentIndex = element.length - 1
  513. }
  514. if (_this.currentIndex < 0) {
  515. _this.currentIndex = 0
  516. }
  517. }
  518. self.isNumber = function(val) {
  519. var regPos = /^\d+(\.\d+)?$/;
  520. var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
  521. if (regPos.test(val) || regNeg.test(val)) {
  522. return true
  523. } else {
  524. return false
  525. }
  526. }
  527. self.ruleFn = function(index, direction) {
  528. var obj = {};
  529. if (rules) {
  530. obj =_self.getCurRule();
  531. }
  532. if (obj && typeof obj == "object" && typeof obj["line"] != "undefined") {
  533. var line = obj["line"]
  534. } else {
  535. var line = _this.hotbtn.length
  536. }
  537. line = parseInt(line);
  538. if (obj && typeof obj == "object" && typeof obj[_this.currentIndex] != "undefined" && typeof obj[_this.currentIndex][index] != "undefined") {
  539. var objRules = obj[_this.currentIndex];
  540. if (self.isNumber(objRules[index])) {
  541. _this.currentIndex = parseInt(_this.currentIndex) + parseInt(objRules[index])
  542. } else if (Array.isArray(objRules[index])) {
  543. _this.reSetClass(objRules[index][0], objRules[index][1])
  544. } else if (typeof obj["line"] != "undefined") {
  545. _this.currentIndex = _this.currentIndex + line
  546. }
  547. } else {
  548. var jump = element[_this.currentIndex].getAttribute("data-" + direction);
  549. jump = parseInt(jump);
  550. if (direction == "up") {
  551. if (_this.currentIndex > line - 1) _this.currentIndex = jump ? _this.currentIndex - jump : _this.currentIndex - line;
  552. else if (obj && typeof obj["up"] == "object") _this.reSetClass(obj[direction][0], obj["up"][1], obj["up"][2], obj["up"][3]);
  553. else if (obj && typeof obj["up"] == "function") obj["up"].call(_this)
  554. } else if (direction == "left") {
  555. if ((_this.currentIndex) % line != 0) _this.currentIndex = jump ? _this.currentIndex - jump : _this.currentIndex - 1;
  556. else if (obj && typeof obj["left"] == "object") _this.reSetClass(obj["left"][0], obj["left"][1], obj["left"][2], obj["left"][3]);
  557. else if (obj && typeof obj["left"] == "function") obj["left"].call(_this)
  558. } else if (direction == "right") {
  559. if ((_this.currentIndex + 1) % line != 0) _this.currentIndex = jump ? _this.currentIndex + jump : _this.currentIndex + 1;
  560. else if (obj && typeof obj["right"] == "object") _this.reSetClass(obj["right"][0], obj["right"][1], obj["right"][2], obj["right"][3]);
  561. else if (obj && typeof obj["right"] == "function") obj["right"].call(_this)
  562. } else if (direction == "down") {
  563. if (_this.hotbtn.length - line > _this.currentIndex) _this.currentIndex = jump ? _this.currentIndex + jump : _this.currentIndex + line;
  564. else if (obj && typeof obj["down"] == "object") _this.reSetClass(obj["down"][0], obj["down"][1], obj["down"][2], obj["down"][3]);
  565. else if (obj && typeof obj["down"] == "function") obj["down"].call(_this);
  566. else if (_this.currentIndex + line > _this.hotbtn.length - 1 && _this.currentIndex + line <= (line - _this.hotbtn.length % line) + _this.hotbtn.length - 1 && _this.hotbtn.length % line != 0 && isFloatLast) {
  567. _this.currentIndex = _this.currentIndex + line;
  568. self.overIndex()
  569. }
  570. }
  571. }
  572. }
  573. self.rule = function() {
  574. self.overIndex();
  575. if (_this.event.keyCode == btnLeft || _this.event.keyCode == "LEFT") {
  576. self.ruleFn(0, "left")
  577. } else if (_this.event.keyCode == btnRight || _this.event.keyCode == "RIGHT") {
  578. self.ruleFn(2, "right")
  579. } else if (_this.event.keyCode == btnUp || _this.event.keyCode == "UP") {
  580. self.ruleFn(1, "up")
  581. } else if (_this.event.keyCode == btnDown || _this.event.keyCode == "DOWN") {
  582. self.ruleFn(3, "down")
  583. }
  584. self.overIndex()
  585. }
  586. this.back=function(){
  587. _this.event.keyCode=8;
  588. self.onPressdo(_this.event);
  589. }
  590. // this.makepress=function(k){
  591. // if(k==""){
  592. // _this.event.keyCode=btnLeft;
  593. // }else if(k=="RIGHT"){
  594. // _this.event.keyCode=btnRight;
  595. // }else if(k=="UP"){
  596. // _this.event.keyCode=btnUp;
  597. // }else if(k=="DOWN"){
  598. // _this.event.keyCode=btnDown;
  599. // }else if(k=="ENTER"){
  600. // _this.event.keyCode=btnEnter;
  601. // }
  602. // self.onPressdo(_this.event);
  603. // }
  604. self.onPressdo = function(e) {
  605. _this.event = e;
  606. _this.currentIndex = _this.currentIndex >= element.length - 1 ? element.length - 1 : _this.currentIndex;
  607. _this.prev = element[_this.currentIndex];
  608. _this.prevIndex = _this.currentIndex;
  609. self.rule();
  610. _this.current = element[_this.currentIndex];
  611. _this.currentIndex = _this.currentIndex;
  612. _this.className = _this.className;
  613. //$.tips(e.keyCode)
  614. if (e.keyCode == 8 || e.keyCode == 27 || e.keyCode == "BACK") {
  615. if (rules["#" + _this.currentId + ">." +_this.className] && (typeof rules["#" + _this.currentId + ">." +_this.className]["onBack"]) == "function") rules["#" + _this.currentId + ">." +_this.className]["onBack"].call(_this);
  616. else if (rules[_this.className] && (typeof rules[_this.className]["onBack"]) == "function") rules[_this.className]["onBack"].call(_this);
  617. else _this.onBack.call(_this)
  618. }
  619. if (rules && rules["#" + _this.currentId + ">." + _this.className] && (typeof rules["#" + _this.currentId + ">." + _this.className]["onPress"]) == "function")
  620. init.rules["#" + _this.currentId + ">." + _this.className]["onPress"].call(_this);
  621. else if (rules && rules[_this.className] && (typeof rules[_this.className]["onPress"]) == "function")
  622. init.rules[_this.className]["onPress"].call(_this);
  623. else _this.onPress.call(_this);
  624. if (e.keyCode == btnEnter || e.keyCode == "ENTER") {
  625. alert(_this.className+"ENTER")
  626. if (rules["#" + _this.currentId + ">." +_this.className] && (typeof rules["#" + _this.currentId + ">." +_this.className]["onEnterPress"]) == "function") rules["#" + _this.currentId + ">." +_this.className]["onEnterPress"].call(_this);
  627. else if (rules[_this.className] && (typeof rules[_this.className]["onEnterPress"]) == "function") rules[_this.className]["onEnterPress"].call(_this);
  628. else _this.onEnterPress.call(_this)
  629. }
  630. _self.classDo(_this.currentIndex);
  631. keydefault(e)
  632. }
  633. this.onLoad();
  634. }
  635. window.tvSysBtnBind = tvSysBtnBind
  636. })(window)