tvSysBtnBind.v2.1.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. try {
  2. (function(arr) {
  3. arr.forEach(function(item) {
  4. if (item.hasOwnProperty('remove')) {
  5. return
  6. }
  7. Object.defineProperty(item, 'remove', {
  8. configurable: true,
  9. enumerable: true,
  10. writable: true,
  11. value: function remove() {
  12. this.parentNode.removeChild(this)
  13. }
  14. })
  15. })
  16. })([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
  17. if (!("classList" in document.documentElement)) {
  18. Object.defineProperty(HTMLElement.prototype, 'classList', {
  19. get: function() {
  20. var self = this;
  21. function update(fn) {
  22. return function(value) {
  23. var classes = self.className.split(/\s+/g),
  24. index = classes.indexOf(value);
  25. fn(classes, index, value);
  26. self.className = classes.join(" ")
  27. }
  28. }
  29. return {
  30. add: update(function(classes, index, value) {
  31. if (!~index) classes.push(value)
  32. }),
  33. remove: update(function(classes, index) {
  34. if (~index) classes.splice(index, 1)
  35. }),
  36. toggle: update(function(classes, index, value) {
  37. if (~index) classes.splice(index, 1);
  38. else classes.push(value)
  39. }),
  40. contains: function(value) {
  41. return !!~self.className.split(/\s+/g).indexOf(value)
  42. },
  43. item: function(i) {
  44. return self.className.split(/\s+/g)[i] || null
  45. }
  46. }
  47. }
  48. })
  49. }
  50. function fireKeyEvent(el, evtType, keyCode) {
  51. var doc = el.ownerDocument,
  52. win = doc.defaultView || doc.parentWindow,
  53. evtObj;
  54. if (doc.createEvent) {
  55. if (win.KeyEvent) {
  56. evtObj = doc.createEvent('KeyEvents');
  57. evtObj.initKeyEvent(evtType, true, true, win, false, false, false, false, keyCode, 0)
  58. } else {
  59. evtObj = doc.createEvent('UIEvents');
  60. Object.defineProperty(evtObj, 'keyCode', {
  61. get: function() {
  62. return this.keyCodeVal
  63. }
  64. });
  65. Object.defineProperty(evtObj, 'which', {
  66. get: function() {
  67. return this.keyCodeVal
  68. }
  69. });
  70. evtObj.initUIEvent(evtType, true, true, win, 1);
  71. evtObj.keyCodeVal = keyCode;
  72. if (evtObj.keyCode !== keyCode) {}
  73. }
  74. el.dispatchEvent(evtObj)
  75. } else if (doc.createEventObject) {
  76. evtObj = doc.createEventObject();
  77. evtObj.keyCode = keyCode;
  78. el.fireEvent('on' + evtType, evtObj)
  79. }
  80. }
  81. } catch (e) {}
  82. function addClass(ele, cls) {
  83. if (ele.classList) {
  84. ele.classList.add(cls)
  85. } else {
  86. if (!this.hasClass(ele, cls)) ele.className += " " + cls
  87. }
  88. }
  89. function arrIndexOf(arr, v) {
  90. for (var i = 0; i < arr.length; i++) {
  91. if (arr[i] == v) {
  92. return i
  93. }
  94. }
  95. return -1
  96. }
  97. function removeClass(ele, cls) {
  98. if (ele.classList) {
  99. ele.classList.remove(cls)
  100. } else {
  101. if (ele.className != '' && hasClass(ele, cls)) {
  102. var arrClassName = ele.className.split(' ');
  103. var classIndex = arrIndexOf(arrClassName, cls);
  104. if (classIndex !== -1) {
  105. arrClassName.splice(classIndex, 1);
  106. ele.className = arrClassName.join(' ')
  107. }
  108. }
  109. }
  110. }
  111. function toggleClass(ele, cls) {
  112. if (hasClass(ele, cls)) {
  113. removeClass(ele, cls)
  114. } else {
  115. addClass(ele, cls)
  116. }
  117. }
  118. function hasClass(element, cls) {
  119. return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1
  120. }
  121. function getClassNames(classStr, target, tagName) {
  122. target = target ? target : document;
  123. tagName = tagName ? tagName : "*";
  124. if (document.getElementsByClassName) {
  125. return target.getElementsByClassName(classStr)
  126. } else {
  127. var nodes = target.getElementsByTagName(tagName),
  128. ret = [];
  129. for (i = 0; i < nodes.length; i++) {
  130. if (hasClass(nodes[i], classStr)) {
  131. ret.push(nodes[i])
  132. }
  133. }
  134. return ret
  135. }
  136. }
  137. function getElementLeft(ele) {
  138. var actualLeft = ele.offsetLeft;
  139. var current = ele.offsetParent;
  140. while (current !== null) {
  141. actualLeft += current.offsetLeft;
  142. current = current.offsetParent
  143. }
  144. return actualLeft
  145. }
  146. function getElementTop(ele) {
  147. var actualTop = ele.offsetTop;
  148. var current = ele.offsetParent;
  149. while (current !== null) {
  150. actualTop += current.offsetTop;
  151. current = current.offsetParent
  152. }
  153. return actualTop
  154. }
  155. var contains = document.documentElement.contains ? function(parent, node) {
  156. if(parent){
  157. return parent !== node && parent.contains(node)
  158. }
  159. } : function(parent, node) {
  160. if(parent){
  161. while (node && (node = node.parentNode))
  162. if (node === parent) return true
  163. return false
  164. }
  165. }
  166. function getBoundingClientRect(ele) {
  167. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  168. var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
  169. if (ele.getBoundingClientRect) {
  170. if (typeof arguments.callee.offset !== 'number') {
  171. var temp = document.createElement('div');
  172. temp.style.cssText = "position:absolute;top:0;left:0";
  173. document.body.appendChild(temp);
  174. arguments.callee.offset = -temp.getBoundingClientRect().top - scrollTop;
  175. document.body.removeChild(temp);
  176. temp = null
  177. }
  178. var rect = ele.getBoundingClientRect();
  179. var offset = arguments.callee.offset;
  180. return {
  181. left: rect.left + offset,
  182. right: rect.right + offset,
  183. top: rect.top + offset,
  184. bottom: rect.bottom + offset,
  185. width: (rect.right + offset) - (rect.left + offset),
  186. height: (rect.bottom + offset) - (rect.top + offset)
  187. }
  188. } else {
  189. var actualLeft = getElementLeft(ele);
  190. var actualTop = getElementTop(ele);
  191. var offsetWidth = ele.offsetWidth;
  192. var offsetHeight = ele.offsetHeight;
  193. return {
  194. left: actualLeft - scrollLeft,
  195. right: actualLeft + offsetWidth - scrollLeft,
  196. top: actualTop - scrollTop,
  197. bottom: actualTop + offsetHeight - scrollTop,
  198. width: (actualLeft + offsetWidth - scrollLeft) - (actualLeft - scrollLeft),
  199. height: (actualTop + offsetHeight - scrollTop) - (actualTop - scrollTop)
  200. }
  201. }
  202. }
  203. (function(window) {
  204. var tvSysBtnBind = function(init) {
  205. var _this = this,
  206. _self = self;
  207. var id = init.id ? init.id : null,
  208. keyRemoveDefault = typeof init.keyRemoveDefault == "undefined" ? false : init.keyRemoveDefault,
  209. currentIndex = init.currentIndex ? parseInt(init.currentIndex) : 0,
  210. btnLeft = init.btnLeft ? init.btnLeft : 37,
  211. btnUp = init.btnUp ? init.btnUp : 38,
  212. btnRight = init.btnRight ? init.btnRight : 39,
  213. btnDown = init.btnDown ? init.btnDown : 40,
  214. btnEnter = init.btnEnter ? init.btnEnter : 13,
  215. btnBack = init.btnBack ? init.btnBack : 27,
  216. history = typeof init.history == "undefined" ? true : init.history,
  217. doublePress = typeof init.doublePress == "undefined" ? false : init.doublePress,
  218. isFloatLast = typeof init.isFloatLast == "undefined" ? false : init.isFloatLast,
  219. isCentered = typeof init.isCentered == "undefined" ? true : init.isCentered,
  220. currentClass = init.currentClass ? init.currentClass : "current",
  221. effect = init.effect ? init.effect : "slide1",
  222. element = new Array(),
  223. rules = init.rules?init.rules:{hotbutton:{}},
  224. direction = "y";
  225. _this.className = init.className ? init.className : "hotbutton";
  226. this.event = {};
  227. var _tempElem;
  228. this.currentIndex = parseInt(currentIndex);
  229. this.defaultIndex = parseInt(currentIndex);
  230. this.currentClass = currentClass;
  231. this.historyFocus = {};
  232. if (!window.focusobj) window.focusobj = document.createElement("span");
  233. (typeof init.onLoad) == "function" ? init.onLoad : init.onLoad = function() {};
  234. (typeof init.onBack) == "function" ? init.onLoad : init.onBack = function() {};
  235. (typeof init.onCommPress) == "function" ? init.onCommPress : init.onCommPress = function() {};
  236. this.onLoad = function() {
  237. 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>';
  238. addClass(focusobj, "focusobj");
  239. addClass(focusobj, "current");
  240. focusobj.style.display = "none";
  241. _this.target = init.id ? document.getElementById(init.id) : document.body;
  242. _this.defaultTarget = _this.target;
  243. _this.currentId = init.id ? init.id : "Jdoc";
  244. self.getCurRule();
  245. _this.reLoad();
  246. self.onPressFn = _this.onPressRules
  247. _this.sourceClassName = _this.className;
  248. _this.sourceLength = element.length;
  249. _this.prev = element[currentIndex];
  250. _this.prevIndex = currentIndex;
  251. _this.current = element[currentIndex];
  252. _this.currentIndex = currentIndex;
  253. _this.target.appendChild(focusobj);
  254. init.onLoad.call(_this);
  255. window.focus();
  256. }
  257. self.getCurRule = function(item) {
  258. var obj = {};
  259. if (rules) {
  260. obj = rules["#" + _this.currentId + ">." + _this.className];
  261. if (typeof obj == "undefined") {
  262. if (rules[_this.className]) {
  263. obj = rules[_this.className]
  264. }
  265. }
  266. if (item && typeof rules[item] == "undefined") {
  267. // rules[item]={};
  268. }
  269. }
  270. return obj
  271. }
  272. this.reSetClass = function(item, index, curClass) {
  273. var obj = _self.getCurRule(item);
  274. if (!obj) {
  275. console.log("初始化" + _this.className + "失败");
  276. return
  277. }
  278. if (typeof obj["history"] == "undefined") obj["history"] = history;
  279. if (obj["history"]) _this.historyFocus[_this.currentId + _this.className] = _this.currentIndex;
  280. _this.prevCurrentClass = _this.currentClass;
  281. if (curClass) {
  282. _this.currentClass = curClass
  283. }
  284. var arr = item.split(">");
  285. for (var i = 0; i < arr.length; i++) {
  286. if (arr[i] == "") arr.splice(i)
  287. }
  288. for (var i = 0; i < arr.length; i++) {
  289. if (arr[i].indexOf("#") != -1) {
  290. _this.currentId = arr[i].replace("#", "");
  291. if (document.getElementById(_this.currentId)) _this.target = document.getElementById(_this.currentId)
  292. }
  293. if (arr[i].indexOf(".") != -1) {
  294. item = arr[i].replace(".", "")
  295. }
  296. if (arr[i].indexOf(".") == -1 && arr[i].indexOf("#") == -1) {
  297. item = arr[i]
  298. }
  299. }
  300. if (getClassNames(item, _this.target).length > 0) {
  301. _self.newItem(item, index)
  302. } else {
  303. _this.target = _this.defaultTarget;
  304. if (getClassNames(item, _this.target).length > 0) _self.newItem(item, index)
  305. }
  306. }
  307. this.onCommPress = function(e) {
  308. init.onCommPress.call(_this)
  309. }
  310. this.tsClose = function () {
  311. self.onPressFn = function(e){
  312. e.preventDefault()
  313. return
  314. }
  315. }
  316. this.tsOpen = function () {
  317. self.onPressFn = this.onPressRules
  318. }
  319. self.newItem = function(item, index) {
  320. var obj = _self.getCurRule();
  321. if (_this.prev) removeClass(_this.prev, _this.currentClass);
  322. if (typeof _this.historyFocus[_this.currentId + item] == "undefined") _this.historyFocus[_this.currentId + item] = 0;
  323. _this.className = item;
  324. var idx = obj["savelastIndex"] ? _this.currentIndex : 0;
  325. index = index > getClassNames(item, _this.target).length - 1 ? getClassNames(item, _this.target).length - 1 : index;
  326. idx = _this.historyFocus[_this.currentId + item] ? _this.historyFocus[_this.currentId + item] : idx;
  327. _this.currentIndex = typeof index != "undefined" ? index : idx;
  328. _this.prevIndex = _this.currentIndex;
  329. _this.reLoad()
  330. }
  331. self.readFn = function() {
  332. if ((typeof init.onEnterPress) == "function") {
  333. init.onEnterPress
  334. } else {
  335. init.onEnterPress = function() {}
  336. }
  337. if ((typeof init.onPress) == "function") {
  338. init.onPress
  339. } else {
  340. init.onPress = function() {}
  341. }
  342. }
  343. this.reLoad = function() {
  344. self.readFn();
  345. element = getClassNames(_this.className, _this.target);
  346. if (element.length <= 0) {
  347. console.log("初始化" + _this.className + "失败");
  348. return
  349. }
  350. this.hotbtn = element;
  351. if (element.length <= 0) return false;
  352. if (_this.currentIndex || _this.currentIndex == 0) {
  353. _this.currentIndex = parseInt(_this.currentIndex)
  354. } else {
  355. _this.currentIndex = parseInt(init.currentIndex)
  356. }
  357. if (isload >= 2 && _this.sourceClass == _this.className && _this.sourceLength != element.length && !isSet) {
  358. if (hasClass(_this.prev, _this.className) && _this.sourceLength > element.length) {
  359. _this.currentIndex = _this.currentIndex - (_this.sourceLength - element.length);
  360. _this.prevIndex = _this.prevIndex - (_this.sourceLength - element.length);
  361. _this.sourceLength = element.length
  362. } else if (hasClass(_this.prev, _this.className) && _this.sourceLength < element.length) {
  363. _this.currentIndex = _this.currentIndex + (element.length - _this.sourceLength);
  364. _this.prevIndex = _this.prevIndex + (element.length - _this.sourceLength);
  365. _this.sourceLength = element.length
  366. }
  367. }
  368. isSet = false;
  369. self.overIndex();
  370. _this.current = element[_this.currentIndex];
  371. _this.currentIndex = _this.currentIndex;
  372. _self.render(_this.currentIndex);
  373. for (var i = 0; i < this.hotbtn.length; i++) {
  374. this.hotbtn[i].setAttribute("data-id", i)
  375. }
  376. }
  377. function keydefault(e) {
  378. try {
  379. e.preventDefault()
  380. } catch (e) {}
  381. }
  382. var isSet = false;
  383. this.setCurrentIndex = function(index) {
  384. isSet = true;
  385. index = parseInt(index);
  386. _this.currentIndex = index;
  387. _this.current = element[index]
  388. }
  389. this.pageScroll = function(ele, Scroll, direction) {
  390. if (!ele["data-sum"]) ele["data-sum"] = 0;
  391. if (!ele["data-sumx"]) ele["data-sumx"] = 0;
  392. ele.style.position = "relative";
  393. ele.children[0].style.position = "absolute";
  394. if (direction == "up") {
  395. if (ele["data-sum"] - Scroll > 0) {
  396. ele["data-sum"] = parseInt(ele["data-sum"]) - Scroll
  397. } else {
  398. ele["data-sum"] = 0
  399. }
  400. ele.children[0].style.left = "0";
  401. ele.children[0].style.top = "-" + ele["data-sum"] + "px"
  402. } else if (direction == "down") {
  403. var clientHeight = ele.children[0].clientHeight;
  404. if (ele["data-sum"] + Scroll < clientHeight - ele.clientHeight / 2) {
  405. ele["data-sum"] = parseInt(ele["data-sum"]) + Scroll
  406. } else {
  407. ele["data-sum"] = clientHeight - ele.clientHeight
  408. }
  409. ele.children[0].style.left = "0";
  410. ele.children[0].style.top = "-" + ele["data-sum"] + "px"
  411. } else if (direction == "left") {
  412. if (ele["data-sumx"] - Scroll > 0) {
  413. ele["data-sumx"] = parseInt(ele["data-sumx"]) - Scroll
  414. } else {
  415. ele["data-sumx"] = clientWidth - ele.clientWidth
  416. }
  417. ele.children[0].style.top = "0";
  418. ele.children[0].style.left = "-" + ele["data-sumx"] + "px"
  419. } else if (direction == "right") {
  420. var clientWidth = ele.children[0].clientWidth;
  421. if (ele["data-sumx"] + Scroll < clientWidth - ele.clientWidth / 2) {
  422. ele["data-sumx"] = parseInt(ele["data-sum"]) + Scroll
  423. } else {
  424. ele["data-sumx"] = 0;
  425. }
  426. ele.children[0].style.top = "0";
  427. ele.children[0].style.left = "-" + ele["data-sumx"] + "px"
  428. }
  429. }
  430. this.viewScrollY = function(y, view) {
  431. var maxTop = view.children[0].clientHeight - view.clientHeight;
  432. var obj = self.getCurRule();
  433. var sumtop = view.children[0]["ScrollY"] ? view.children[0]["ScrollY"] : 0;
  434. var top = sumtop + y;
  435. middle = getBoundingClientRect(view).height / 2 - getBoundingClientRect(_this.current).height / 2;
  436. top = top + middle;
  437. if (top > 0 || maxTop < 0) top = 0;
  438. else if (Math.abs(top) > Math.abs(maxTop)){
  439. top = -maxTop;
  440. //view.children[0]["ScrollY"] == 0 这个句忘记干嘛的了 留着
  441. }
  442. top = obj["customTop"] != undefined ? obj["customTop"] : top;
  443. view.children[0].style.top = top + "px";
  444. view.children[0]["ScrollY"] = top
  445. }
  446. var maxLeft = 0;
  447. this.viewScrollX = function(x, view, viewWidth) {
  448. var maxLeft = viewWidth - view.clientWidth;
  449. var obj = self.getCurRule();
  450. var sumleft = view.children[0]["ScrollX"] ? view.children[0]["ScrollX"] : 0;
  451. var left = sumleft + x;
  452. middle = getBoundingClientRect(view).width / 2 - getBoundingClientRect(_this.current).width / 2;
  453. left = left + middle;
  454. if (left > 0 || maxLeft < 0) left = 0;
  455. else if (Math.abs(left) > Math.abs(maxLeft)){
  456. left = -maxLeft;
  457. }
  458. left = obj["customLeft"] != undefined ? obj["customLeft"] : left;
  459. view.children[0].style.left = left + "px";
  460. view.children[0]["ScrollX"] = left
  461. }
  462. this.onPress = function(e) {
  463. init.onPress.call(_this)
  464. }
  465. this.onEnterPress = function() {
  466. init.onEnterPress.call(_this)
  467. }
  468. this.onBack = function() {
  469. init.onBack.call(_this)
  470. }
  471. self.getScrollTop = function() {
  472. var scrollTop = 0;
  473. if (document.documentElement && document.documentElement.scrollTop) {
  474. scrollTop = document.documentElement.scrollTop
  475. } else if (document.body) {
  476. scrollTop = document.body.scrollTop
  477. }
  478. return scrollTop
  479. }
  480. this.scroll = function(view,direction) {
  481. var obj = self.getCurRule();
  482. if (obj == undefined || !obj["direction"]) return;
  483. direction=direction?direction:obj["direction"];
  484. var viewX=view?view:obj["directionParentX"]?obj["directionParentX"]:obj["directionParent"]?obj["directionParent"]:_this.current.parentNode.parentNode;
  485. var viewY=view?view:obj["directionParentY"]?obj["directionParentY"]:obj["directionParent"]?obj["directionParent"]:_this.current.parentNode.parentNode;
  486. var sum = 0;
  487. if (direction.indexOf("x") > -1) {
  488. if(viewX instanceof Array){
  489. for (var i = 0; i < viewX.length; i++) {
  490. if(contains(viewX[i],_this.current)){
  491. viewX=viewX[i];
  492. }
  493. }
  494. }
  495. if(viewX instanceof Array||!contains(viewX,_this.current))return;
  496. for (var i = 0; i < _this.hotbtn.length; i++) {
  497. sum = getBoundingClientRect(_this.hotbtn[i]).width + sum
  498. }
  499. if (_this.hotbtn.length > 1) {
  500. if(!obj["scrollWidth"])
  501. sum = sum + Math.ceil(getBoundingClientRect(_this.hotbtn[1]).left - getBoundingClientRect(_this.hotbtn[0]).right) * (_this.hotbtn.length);
  502. else{
  503. sum = _this.hotbtn.length*obj["scrollWidth"];//因为样式多样性,宽度获取并不准确 所以可以自定义
  504. }
  505. } else {
  506. sum = sum;
  507. }
  508. viewX.style.position = "relative";
  509. viewX.children[0].style.position = "absolute";
  510. viewX.children[0].style.width = (sum) + "px";
  511. _this.viewScrollX(-(getBoundingClientRect(_this.current).left - getBoundingClientRect(viewX).left), viewX, sum)
  512. }
  513. if (direction.indexOf("y") > -1) {
  514. if(viewY instanceof Array ){
  515. for (var i = 0; i < viewY.length; i++) {
  516. if(contains(viewY[i],_this.current)){
  517. viewY=viewY[i];
  518. }
  519. }
  520. }
  521. if(viewY instanceof Array||!contains(viewY,_this.current))return;
  522. viewY.style.position = "relative";
  523. viewY.children[0].style.position = "absolute";
  524. _this.viewScrollY(-(getBoundingClientRect(_this.current).top - getBoundingClientRect(viewY).top), viewY)
  525. }
  526. }
  527. var isload = 0;
  528. self.render = function(index) {
  529. if (_this.renderType == "self") return;
  530. isload = isload + 1;
  531. if (isload == 2) {
  532. _this.sourceLength = element.length
  533. }
  534. if (_this.prevCurrentClass) {
  535. var ele = getClassNames(_this.prevCurrentClass, _this.target);
  536. for (var i = 0; i < ele.length; i++) {
  537. if (hasClass(ele[i], _this.prevCurrentClass)) {
  538. removeClass(ele[i], _this.prevCurrentClass)
  539. }
  540. }
  541. }
  542. if (element[index]) addClass(element[index], _this.currentClass);
  543. else return;
  544. for (var i = 0; i < element.length; i++) {
  545. if (i != index && hasClass(element[i], _this.currentClass)) {
  546. removeClass(element[i], _this.currentClass)
  547. }
  548. }
  549. _this.scroll();
  550. var effect = element[index].getAttribute("data-effect");
  551. if (effect) {
  552. 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;");
  553. focusobj.setAttribute("class", "focusobj current " + effect);
  554. focusobj.style.display = "list-item"
  555. } else {
  556. focusobj.setAttribute("style", "");
  557. focusobj.style.display = "none"
  558. }
  559. }
  560. self.EventUtil = {
  561. add: function(obj, callback) {
  562. if (typeof(obj.onkeypress) == "null") {
  563. obj.onkeypress = function(e) {
  564. callback && callback(e)
  565. }
  566. } else {
  567. obj.onkeydown = function(e) {
  568. callback && callback(e)
  569. }
  570. }
  571. }
  572. }
  573. var _t,sum=0;
  574. EventUtil.add(document, function(e) {
  575. _this.renderType = "self";
  576. if(doublePress){
  577. if(sum<2)
  578. clearTimeout(_t);
  579. sum++;
  580. _t =setTimeout(function(){
  581. _self.onPressFn(e)
  582. sum=0;
  583. },100)
  584. }else{
  585. _self.onPressFn(e);
  586. }
  587. });
  588. self.overIndex = function() {
  589. if (_this.currentIndex >= element.length - 1) {
  590. _this.currentIndex = element.length - 1
  591. }
  592. if (_this.currentIndex < 0) {
  593. _this.currentIndex = 0
  594. }
  595. }
  596. self.isNumber = function(val) {
  597. var regPos = /^\d+(\.\d+)?$/;
  598. var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
  599. if (regPos.test(val) || regNeg.test(val)) {
  600. return true
  601. } else {
  602. return false
  603. }
  604. }
  605. var distance = function(pointA, pointB, type) {
  606. var P1 = getBoundingClientRect(pointA),
  607. P2 = getBoundingClientRect(pointB);
  608. var dist = 0;
  609. switch (type) {
  610. case "AA":
  611. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.top - P2.top) * (P1.top - P2.top)))
  612. break;
  613. case "AB":
  614. dist = Math.sqrt(((P1.left - P2.right) * (P1.left - P2.right)) + ((P1.top - P2.top) * (P1.top - P2.top)))
  615. break;
  616. case "AC":
  617. dist = Math.sqrt(((P1.left - P2.right) * (P1.left - P2.right)) + ((P1.top - P2.bottom) * (P1.top - P2.bottom)))
  618. break;
  619. case "AD":
  620. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.top - P2.bottom) * (P1.top - P2.bottom)))
  621. break;
  622. case "BA":
  623. dist = Math.sqrt(((P1.right - P2.left) * (P1.right - P2.left)) + ((P1.top - P2.top) * (P1.top - P2.top)))
  624. break;
  625. case "BB":
  626. dist = Math.sqrt(((P1.right - P2.right) * (P1.right - P2.right)) + ((P1.top - P2.top) * (P1.top - P2.top)))
  627. break;
  628. case "BC":
  629. dist = Math.sqrt(((P1.right - P2.right) * (P1.right - P2.right)) + ((P1.top - P2.bottom) * (P1.top - P2.bottom)))
  630. break;
  631. case "BD":
  632. dist = Math.sqrt(((P1.right - P2.left) * (P1.right - P2.left)) + ((P1.top - P2.bottom) * (P1.top - P2.bottom)))
  633. break;
  634. case "CA":
  635. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.bottom - P2.top) * (P1.bottom - P2.top)))
  636. break;
  637. case "CB":
  638. dist = Math.sqrt(((P1.left - P2.right) * (P1.left - P2.right)) + ((P1.bottom - P2.top) * (P1.bottom - P2.top)))
  639. break;
  640. case "CC":
  641. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.bottom - P2.bottom) * (P1.bottom - P2.bottom)))
  642. break;
  643. case "CD":
  644. dist = Math.sqrt(((P1.right - P2.left) * (P1.right - P2.left)) + ((P1.bottom - P2.bottom) * (P1.bottom - P2.bottom)))
  645. break;
  646. case "DA":
  647. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.bottom - P2.top) * (P1.bottom - P2.top)))
  648. break;
  649. case "DB":
  650. dist = Math.sqrt(((P1.right - P2.right) * (P1.right - P2.right)) + ((P1.bottom - P2.top) * (P1.bottom - P2.top)))
  651. break;
  652. case "DC":
  653. dist = Math.sqrt(((P1.right - P2.left) * (P1.right - P2.left)) + ((P1.bottom - P2.bottom) * (P1.bottom - P2.bottom)))
  654. break;
  655. case "DD":
  656. dist = Math.sqrt(((P1.right - P2.right) * (P1.right - P2.right)) + ((P1.bottom - P2.bottom) * (P1.bottom - P2.bottom)))
  657. break
  658. }
  659. return dist
  660. }
  661. self.ruleFn = function(index, direction) {
  662. var obj = {};
  663. if (rules) {
  664. obj = _self.getCurRule()
  665. }
  666. if (obj && typeof obj == "object" && typeof obj["line"] != "undefined") {
  667. var line = obj["line"]
  668. } else {
  669. var line = _this.hotbtn.length
  670. }
  671. line = parseInt(line);
  672. if (obj && typeof obj == "object" && typeof obj[_this.currentIndex] != "undefined" && typeof obj[_this.currentIndex][index] != "undefined") {
  673. var objRules = obj[_this.currentIndex];
  674. if (self.isNumber(objRules[index])) {
  675. _this.currentIndex = parseInt(_this.currentIndex) + parseInt(objRules[index])
  676. } else if (Array.isArray(objRules[index])) {
  677. _this.reSetClass(objRules[index][0], objRules[index][1], objRules[index][2])
  678. } else if (typeof objRules[index] == "function") {
  679. objRules[index].call(_this)
  680. } else if (typeof obj["line"] != "undefined") {
  681. _this.currentIndex = _this.currentIndex + line
  682. }
  683. } else {
  684. var jump = element[_this.currentIndex].getAttribute("data-" + direction);
  685. jump = parseInt(jump);
  686. var max=Math.sqrt(window.screen.width*window.screen.width+window.screen.height*window.screen.height);
  687. if (direction == "up") {
  688. if (_this.currentIndex > line - 1&&!obj["autoMove"]) _this.currentIndex = jump ? _this.currentIndex - jump : _this.currentIndex - line;
  689. else if (obj && typeof obj["up"] == "object") _this.reSetClass(obj["up"][0], obj["up"][1], obj["up"][2]);
  690. else if (obj && typeof obj["up"] == "function") obj["up"].call(_this)
  691. else {
  692. var min1 = max,min2 = max,
  693. curIndex = _this.currentIndex, curIndex2 = _this.currentIndex;
  694. for (var i = 0; i < _this.hotbtn.length; i++) {
  695. var cur1 = distance(_this.current, _this.hotbtn[i], "AD");
  696. var cur2 = distance(_this.current, _this.hotbtn[i], "BC");
  697. if (cur1 < min1 && cur1 > 0 && getBoundingClientRect(_this.current).top > getBoundingClientRect(_this.hotbtn[i]).top && _this.currentIndex > i) {
  698. min1 = cur1;
  699. curIndex = i;
  700. }
  701. if (cur2 < min2 && cur2 > 0 && getBoundingClientRect(_this.current).top > getBoundingClientRect(_this.hotbtn[i]).top && _this.currentIndex > i) {
  702. min2 = cur2;
  703. curIndex2 = i;
  704. }
  705. }
  706. curIndex=min1<min2?curIndex:curIndex2;
  707. _this.currentIndex = curIndex;
  708. }
  709. } else if (direction == "left") {
  710. if ((_this.currentIndex) % line != 0&&!obj["autoMove"]) _this.currentIndex = jump ? _this.currentIndex - jump : _this.currentIndex - 1;
  711. else if (obj && typeof obj["left"] == "object") _this.reSetClass(obj["left"][0], obj["left"][1], obj["left"][2]);
  712. else if (obj && typeof obj["left"] == "function") obj["left"].call(_this)
  713. else {
  714. var min1 = max,min2 = max
  715. curIndex = _this.currentIndex,curIndex2 = _this.currentIndex;
  716. for (var i = 0; i < _this.hotbtn.length; i++) {
  717. var cur1 = distance(_this.current, _this.hotbtn[i], "AB");
  718. var cur2 = distance(_this.current, _this.hotbtn[i], "DC");
  719. if (cur1 < min1 && cur1 > 0 && getBoundingClientRect(_this.current).left > getBoundingClientRect(_this.hotbtn[i]).left && _this.currentIndex > i) {
  720. min1 = cur1;
  721. curIndex = i;
  722. }
  723. if (cur2 < min2 && cur2 > 0 && getBoundingClientRect(_this.current).left > getBoundingClientRect(_this.hotbtn[i]).left && _this.currentIndex > i) {
  724. min2 = cur2;
  725. curIndex2 = i;
  726. }
  727. }
  728. curIndex=min1<min2?curIndex:curIndex2;
  729. _this.currentIndex = curIndex
  730. }
  731. } else if (direction == "right") {
  732. if ((_this.currentIndex + 1) % line != 0&&!obj["autoMove"]) _this.currentIndex = jump ? _this.currentIndex + jump : _this.currentIndex + 1;
  733. else if (obj && typeof obj["right"] == "object") _this.reSetClass(obj["right"][0], obj["right"][1], obj["right"][2]);
  734. else if (obj && typeof obj["right"] == "function") obj["right"].call(_this)
  735. else {
  736. var min1 = max,min2 = max,
  737. curIndex = _this.currentIndex,curIndex2 = _this.currentIndex;
  738. for (var i = 0; i < _this.hotbtn.length; i++) {
  739. var cur1 = distance(_this.current, _this.hotbtn[i], "BA");
  740. var cur2 = distance(_this.current, _this.hotbtn[i], "CD");
  741. if (cur1 < min1 && cur1 > 0 && getBoundingClientRect(_this.current).right< getBoundingClientRect(_this.hotbtn[i]).right && _this.currentIndex < i) {
  742. min1 = cur1;
  743. curIndex = i;
  744. }
  745. if (cur2 < min2 && cur2 > 0 && getBoundingClientRect(_this.current).right < getBoundingClientRect(_this.hotbtn[i]).right && _this.currentIndex < i) {
  746. min2 = cur2;
  747. curIndex2 = i;
  748. }
  749. }
  750. curIndex=min1<min2?curIndex:curIndex2;
  751. _this.currentIndex = curIndex;
  752. }
  753. } else if (direction == "down") {
  754. if (_this.hotbtn.length - line > _this.currentIndex&&!obj["autoMove"]) _this.currentIndex = jump ? _this.currentIndex + jump : _this.currentIndex + line;
  755. else if (obj && typeof obj["down"] == "object") _this.reSetClass(obj["down"][0], obj["down"][1], obj["down"][2]);
  756. else if (obj && typeof obj["down"] == "function") obj["down"].call(_this);
  757. 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) {
  758. _this.currentIndex = _this.currentIndex + line;
  759. self.overIndex()
  760. } else {
  761. var min1 = max,min2=max,
  762. curIndex = _this.currentIndex,curIndex2=_this.currentIndex;
  763. for (var i = 0; i < _this.hotbtn.length; i++) {
  764. var cur1 = distance(_this.current, _this.hotbtn[i], "DA");
  765. var cur2 = distance(_this.current, _this.hotbtn[i], "CB");
  766. if (cur1 < min1 && cur1>0&&getBoundingClientRect(_this.current).bottom < getBoundingClientRect(_this.hotbtn[i]).bottom&&_this.currentIndex < i) {
  767. min1 = cur1;
  768. curIndex = i;
  769. }
  770. if (cur2 < min2 &&cur2>0&& getBoundingClientRect(_this.current).bottom < getBoundingClientRect(_this.hotbtn[i]).bottom&&_this.currentIndex < i) {
  771. min2 = cur2;
  772. curIndex2 = i;
  773. }
  774. }
  775. curIndex=min1<min2?curIndex:curIndex2;
  776. _this.currentIndex = curIndex;
  777. }
  778. }
  779. }
  780. }
  781. self.rule = function(keyCode) {
  782. if (element.length <= 0) {
  783. console.log("初始化" + _this.className + "失败");
  784. return
  785. }
  786. self.overIndex();
  787. if (keyCode == btnLeft) {
  788. self.ruleFn(0, "left")
  789. } else if (keyCode == btnRight) {
  790. self.ruleFn(2, "right")
  791. } else if (keyCode == btnUp) {
  792. self.ruleFn(1, "up")
  793. } else if (keyCode == btnDown) {
  794. self.ruleFn(3, "down")
  795. }
  796. self.overIndex()
  797. }
  798. this.BACK = function() {
  799. self.onPressFn("back")
  800. }
  801. this.LEFT = function() {
  802. self.onPressFn("left")
  803. }
  804. this.RIGHT = function() {
  805. self.onPressFn("right")
  806. }
  807. this.UP = function() {
  808. self.onPressFn("up")
  809. }
  810. this.DOWN = function() {
  811. self.onPressFn("down")
  812. }
  813. this.onPressRules = function(e) {
  814. var keyCode = "";
  815. if (typeof e == "object") {
  816. keyCode = e.keyCode
  817. } else {
  818. if (e == "left") {
  819. keyCode = btnLeft
  820. } else if (e == "right") {
  821. keyCode = btnRight
  822. } else if (e == "up") {
  823. keyCode = btnUp
  824. } else if (e == "down") {
  825. keyCode = btnDown
  826. } else if (e == "back") {
  827. keyCode = btnBack
  828. }
  829. }
  830. _this.event = e;
  831. _this.currentIndex = _this.currentIndex >= element.length - 1 ? element.length - 1 : _this.currentIndex;
  832. _this.prev = element[_this.currentIndex];
  833. _this.prevIndex = _this.currentIndex;
  834. self.rule(keyCode);
  835. _this.current = element[_this.currentIndex];
  836. _this.currentIndex = _this.currentIndex;
  837. _this.className = _this.className;
  838. _this.onCommPress.call(_this);
  839. if (keyCode == 8 || keyCode == btnBack) {
  840. if (rules["#" + _this.currentId + ">." + _this.className] && (typeof rules["#" + _this.currentId + ">." + _this.className]["onBack"]) == "function") rules["#" + _this.currentId + ">." + _this.className]["onBack"].call(_this);
  841. else if (rules[_this.className] && (typeof rules[_this.className]["onBack"]) == "function") rules[_this.className]["onBack"].call(_this);
  842. else _this.onBack.call(_this)
  843. }
  844. if (rules && rules["#" + _this.currentId + ">." + _this.className] && (typeof rules["#" + _this.currentId + ">." + _this.className]["onPress"]) == "function") init.rules["#" + _this.currentId + ">." + _this.className]["onPress"].call(_this);
  845. else if (rules && rules[_this.className] && (typeof rules[_this.className]["onPress"]) == "function") init.rules[_this.className]["onPress"].call(_this);
  846. else _this.onPress.call(_this);
  847. if (keyCode == btnEnter) {
  848. if (rules["#" + _this.currentId + ">." + _this.className] && (typeof rules["#" + _this.currentId + ">." + _this.className]["onEnterPress"]) == "function") rules["#" + _this.currentId + ">." + _this.className]["onEnterPress"].call(_this);
  849. else if (rules[_this.className] && (typeof rules[_this.className]["onEnterPress"]) == "function") rules[_this.className]["onEnterPress"].call(_this);
  850. else _this.onEnterPress.call(_this)
  851. }
  852. _this.renderType = "";
  853. _self.render(_this.currentIndex);
  854. keydefault(e)
  855. }
  856. this.onLoad()
  857. }
  858. window.tvSysBtnBind = tvSysBtnBind
  859. })(window)