tvSysBtnBind.v2.1.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. _this.sourceClassName = _this.className;
  247. _this.sourceLength = element.length;
  248. _this.prev = element[currentIndex];
  249. _this.prevIndex = currentIndex;
  250. _this.current = element[currentIndex];
  251. _this.currentIndex = currentIndex;
  252. _this.target.appendChild(focusobj);
  253. init.onLoad.call(_this);
  254. window.focus();
  255. }
  256. self.getCurRule = function(item) {
  257. var obj = {};
  258. if (rules) {
  259. obj = rules["#" + _this.currentId + ">." + _this.className];
  260. if (typeof obj == "undefined") {
  261. if (rules[_this.className]) {
  262. obj = rules[_this.className]
  263. }
  264. }
  265. if (item && typeof rules[item] == "undefined") {
  266. // rules[item]={};
  267. }
  268. }
  269. return obj
  270. }
  271. this.reSetClass = function(item, index, curClass) {
  272. var obj = _self.getCurRule(item);
  273. if (!obj) {
  274. console.log("初始化" + _this.className + "失败");
  275. return
  276. }
  277. if (typeof obj["history"] == "undefined") obj["history"] = history;
  278. if (obj["history"]) _this.historyFocus[_this.currentId + _this.className] = _this.currentIndex;
  279. _this.prevCurrentClass = _this.currentClass;
  280. if (curClass) {
  281. _this.currentClass = curClass
  282. }
  283. var arr = item.split(">");
  284. for (var i = 0; i < arr.length; i++) {
  285. if (arr[i] == "") arr.splice(i)
  286. }
  287. for (var i = 0; i < arr.length; i++) {
  288. if (arr[i].indexOf("#") != -1) {
  289. _this.currentId = arr[i].replace("#", "");
  290. if (document.getElementById(_this.currentId)) _this.target = document.getElementById(_this.currentId)
  291. }
  292. if (arr[i].indexOf(".") != -1) {
  293. item = arr[i].replace(".", "")
  294. }
  295. if (arr[i].indexOf(".") == -1 && arr[i].indexOf("#") == -1) {
  296. item = arr[i]
  297. }
  298. }
  299. if (getClassNames(item, _this.target).length > 0) {
  300. _self.newItem(item, index)
  301. } else {
  302. _this.target = _this.defaultTarget;
  303. if (getClassNames(item, _this.target).length > 0) _self.newItem(item, index)
  304. }
  305. }
  306. this.onCommPress = function(e) {
  307. init.onCommPress.call(_this)
  308. }
  309. self.newItem = function(item, index) {
  310. var obj = _self.getCurRule();
  311. if (_this.prev) removeClass(_this.prev, _this.currentClass);
  312. if (typeof _this.historyFocus[_this.currentId + item] == "undefined") _this.historyFocus[_this.currentId + item] = 0;
  313. _this.className = item;
  314. var idx = obj["savelastIndex"] ? _this.currentIndex : 0;
  315. index = index > getClassNames(item, _this.target).length - 1 ? getClassNames(item, _this.target).length - 1 : index;
  316. idx = _this.historyFocus[_this.currentId + item] ? _this.historyFocus[_this.currentId + item] : idx;
  317. _this.currentIndex = typeof index != "undefined" ? index : idx;
  318. _this.prevIndex = _this.currentIndex;
  319. _this.reLoad()
  320. }
  321. self.readFn = function() {
  322. if ((typeof init.onEnterPress) == "function") {
  323. init.onEnterPress
  324. } else {
  325. init.onEnterPress = function() {}
  326. }
  327. if ((typeof init.onPress) == "function") {
  328. init.onPress
  329. } else {
  330. init.onPress = function() {}
  331. }
  332. }
  333. this.reLoad = function() {
  334. self.readFn();
  335. element = getClassNames(_this.className, _this.target);
  336. if (element.length <= 0) {
  337. console.log("初始化" + _this.className + "失败");
  338. return
  339. }
  340. this.hotbtn = element;
  341. if (element.length <= 0) return false;
  342. if (_this.currentIndex || _this.currentIndex == 0) {
  343. _this.currentIndex = parseInt(_this.currentIndex)
  344. } else {
  345. _this.currentIndex = parseInt(init.currentIndex)
  346. }
  347. if (isload >= 2 && _this.sourceClass == _this.className && _this.sourceLength != element.length && !isSet) {
  348. if (hasClass(_this.prev, _this.className) && _this.sourceLength > element.length) {
  349. _this.currentIndex = _this.currentIndex - (_this.sourceLength - element.length);
  350. _this.prevIndex = _this.prevIndex - (_this.sourceLength - element.length);
  351. _this.sourceLength = element.length
  352. } else if (hasClass(_this.prev, _this.className) && _this.sourceLength < element.length) {
  353. _this.currentIndex = _this.currentIndex + (element.length - _this.sourceLength);
  354. _this.prevIndex = _this.prevIndex + (element.length - _this.sourceLength);
  355. _this.sourceLength = element.length
  356. }
  357. }
  358. isSet = false;
  359. self.overIndex();
  360. _this.current = element[_this.currentIndex];
  361. _this.currentIndex = _this.currentIndex;
  362. _self.render(_this.currentIndex);
  363. for (var i = 0; i < this.hotbtn.length; i++) {
  364. this.hotbtn[i].setAttribute("data-id", i)
  365. }
  366. }
  367. function keydefault(e) {
  368. try {
  369. e.preventDefault()
  370. } catch (e) {}
  371. }
  372. var isSet = false;
  373. this.setCurrentIndex = function(index) {
  374. isSet = true;
  375. index = parseInt(index);
  376. _this.currentIndex = index;
  377. _this.current = element[index]
  378. }
  379. this.pageScroll = function(ele, Scroll, direction) {
  380. if (!ele["data-sum"]) ele["data-sum"] = 0;
  381. if (!ele["data-sumx"]) ele["data-sumx"] = 0;
  382. ele.style.position = "relative";
  383. ele.children[0].style.position = "absolute";
  384. if (direction == "up") {
  385. if (ele["data-sum"] - Scroll > 0) {
  386. ele["data-sum"] = parseInt(ele["data-sum"]) - Scroll
  387. } else {
  388. ele["data-sum"] = 0
  389. }
  390. ele.children[0].style.left = "0";
  391. ele.children[0].style.top = "-" + ele["data-sum"] + "px"
  392. } else if (direction == "down") {
  393. var clientHeight = ele.children[0].clientHeight;
  394. if (ele["data-sum"] + Scroll < clientHeight - ele.clientHeight / 2) {
  395. ele["data-sum"] = parseInt(ele["data-sum"]) + Scroll
  396. } else {
  397. ele["data-sum"] = clientHeight - ele.clientHeight
  398. }
  399. ele.children[0].style.left = "0";
  400. ele.children[0].style.top = "-" + ele["data-sum"] + "px"
  401. } else if (direction == "left") {
  402. if (ele["data-sumx"] - Scroll > 0) {
  403. ele["data-sumx"] = parseInt(ele["data-sumx"]) - Scroll
  404. } else {
  405. ele["data-sumx"] = clientWidth - ele.clientWidth
  406. }
  407. ele.children[0].style.top = "0";
  408. ele.children[0].style.left = "-" + ele["data-sumx"] + "px"
  409. } else if (direction == "right") {
  410. var clientWidth = ele.children[0].clientWidth;
  411. if (ele["data-sumx"] + Scroll < clientWidth - ele.clientWidth / 2) {
  412. ele["data-sumx"] = parseInt(ele["data-sum"]) + Scroll
  413. } else {
  414. ele["data-sumx"] = 0;
  415. }
  416. ele.children[0].style.top = "0";
  417. ele.children[0].style.left = "-" + ele["data-sumx"] + "px"
  418. }
  419. }
  420. this.viewScrollY = function(y, view) {
  421. var maxTop = view.children[0].clientHeight - view.clientHeight;
  422. var obj = self.getCurRule();
  423. var sumtop = view.children[0]["ScrollY"] ? view.children[0]["ScrollY"] : 0;
  424. var top = sumtop + y;
  425. middle = getBoundingClientRect(view).height / 2 - getBoundingClientRect(_this.current).height / 2;
  426. top = top + middle;
  427. if (top > 0 || maxTop < 0) top = 0;
  428. else if (Math.abs(top) > Math.abs(maxTop)) {
  429. top = -maxTop;
  430. //view.children[0]["ScrollY"] == 0 这个句忘记干嘛的了 留着
  431. }
  432. top = obj["customTop"] != undefined ? obj["customTop"] : top;
  433. view.children[0].style.top = top + "px";
  434. view.children[0]["ScrollY"] = top
  435. }
  436. var maxLeft = 0;
  437. this.viewScrollX = function(x, view, viewWidth) {
  438. var maxLeft = viewWidth - view.clientWidth;
  439. var obj = self.getCurRule();
  440. var sumleft = view.children[0]["ScrollX"] ? view.children[0]["ScrollX"] : 0;
  441. var left = sumleft + x;
  442. middle = getBoundingClientRect(view).width / 2 - getBoundingClientRect(_this.current).width / 2;
  443. left = left + middle;
  444. if (left > 0 || maxLeft < 0) left = 0;
  445. else if (Math.abs(left) > Math.abs(maxLeft)) {
  446. left = -maxLeft;
  447. }
  448. left = obj["customLeft"] != undefined ? obj["customLeft"] : left;
  449. view.children[0].style.left = left + "px";
  450. view.children[0]["ScrollX"] = left
  451. }
  452. this.onPress = function(e) {
  453. init.onPress.call(_this)
  454. }
  455. this.onEnterPress = function() {
  456. init.onEnterPress.call(_this)
  457. }
  458. window.onBackEvent = this.onBack = function() {
  459. init.onBack.call(_this)
  460. }
  461. self.getScrollTop = function() {
  462. var scrollTop = 0;
  463. if (document.documentElement && document.documentElement.scrollTop) {
  464. scrollTop = document.documentElement.scrollTop
  465. } else if (document.body) {
  466. scrollTop = document.body.scrollTop
  467. }
  468. return scrollTop
  469. }
  470. this.scroll = function(view, direction) {
  471. var obj = self.getCurRule();
  472. if (obj == undefined || !obj["direction"]) return;
  473. direction = direction ? direction : obj["direction"];
  474. var viewX = view ? view : obj["directionParentX"] ? obj["directionParentX"] : obj["directionParent"] ? obj["directionParent"] : _this.current.parentNode.parentNode;
  475. var viewY = view ? view : obj["directionParentY"] ? obj["directionParentY"] : obj["directionParent"] ? obj["directionParent"] : _this.current.parentNode.parentNode;
  476. var sum = 0;
  477. if (direction.indexOf("x") > -1) {
  478. if (viewX instanceof Array) {
  479. for (var i = 0; i < viewX.length; i++) {
  480. if (contains(viewX[i], _this.current)) {
  481. viewX = viewX[i];
  482. }
  483. }
  484. }
  485. if (viewX instanceof Array || !contains(viewX, _this.current)) return;
  486. for (var i = 0; i < _this.hotbtn.length; i++) {
  487. sum = getBoundingClientRect(_this.hotbtn[i]).width + sum
  488. }
  489. if (_this.hotbtn.length > 1) {
  490. if (!obj["scrollWidth"])
  491. sum = sum + Math.ceil(getBoundingClientRect(_this.hotbtn[1]).left - getBoundingClientRect(_this.hotbtn[0]).right) * (_this.hotbtn.length);
  492. else {
  493. sum = _this.hotbtn.length * obj["scrollWidth"]; //因为样式多样性,宽度获取并不准确 所以可以自定义
  494. }
  495. } else {
  496. sum = sum;
  497. }
  498. viewX.style.position = "relative";
  499. viewX.children[0].style.position = "absolute";
  500. viewX.children[0].style.width = (sum + 30) + "px";
  501. _this.viewScrollX(-(getBoundingClientRect(_this.current).left - getBoundingClientRect(viewX).left), viewX, sum)
  502. }
  503. if (direction.indexOf("y") > -1) {
  504. if (viewY instanceof Array) {
  505. for (var i = 0; i < viewY.length; i++) {
  506. if (contains(viewY[i], _this.current)) {
  507. viewY = viewY[i];
  508. }
  509. }
  510. }
  511. if (viewY instanceof Array || !contains(viewY, _this.current)) return;
  512. viewY.style.position = "relative";
  513. viewY.children[0].style.position = "absolute";
  514. _this.viewScrollY(-(getBoundingClientRect(_this.current).top - getBoundingClientRect(viewY).top), viewY)
  515. }
  516. }
  517. var isload = 0;
  518. self.render = function(index) {
  519. if (_this.renderType == "self") return;
  520. isload = isload + 1;
  521. if (isload == 2) {
  522. _this.sourceLength = element.length
  523. }
  524. if (_this.prevCurrentClass) {
  525. var ele = getClassNames(_this.prevCurrentClass, _this.target);
  526. for (var i = 0; i < ele.length; i++) {
  527. if (hasClass(ele[i], _this.prevCurrentClass)) {
  528. removeClass(ele[i], _this.prevCurrentClass)
  529. }
  530. }
  531. }
  532. if (element[index]) addClass(element[index], _this.currentClass);
  533. else return;
  534. for (var i = 0; i < element.length; i++) {
  535. if (i != index && hasClass(element[i], _this.currentClass)) {
  536. removeClass(element[i], _this.currentClass)
  537. }
  538. }
  539. _this.scroll();
  540. var effect = element[index].getAttribute("data-effect");
  541. if (effect) {
  542. 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;");
  543. focusobj.setAttribute("class", "focusobj current " + effect);
  544. focusobj.style.display = "list-item"
  545. } else {
  546. focusobj.setAttribute("style", "");
  547. focusobj.style.display = "none"
  548. }
  549. }
  550. self.EventUtil = {
  551. add: function(obj, callback) {
  552. if (typeof(obj.onkeypress) == "null") {
  553. obj.onkeypress = function(e) {
  554. callback && callback(e)
  555. }
  556. } else {
  557. obj.onkeydown = function(e) {
  558. callback && callback(e)
  559. }
  560. }
  561. }
  562. }
  563. var _t, sum = 0;
  564. EventUtil.add(document, function(e) {
  565. _this.renderType = "self";
  566. if (doublePress) {
  567. if (sum < 2)
  568. clearTimeout(_t);
  569. sum++;
  570. _t = setTimeout(function() {
  571. _self.onPressFn(e)
  572. sum = 0;
  573. }, 100)
  574. } else {
  575. _self.onPressFn(e);
  576. }
  577. });
  578. self.overIndex = function() {
  579. if (_this.currentIndex >= element.length - 1) {
  580. _this.currentIndex = element.length - 1
  581. }
  582. if (_this.currentIndex < 0) {
  583. _this.currentIndex = 0
  584. }
  585. }
  586. self.isNumber = function(val) {
  587. var regPos = /^\d+(\.\d+)?$/;
  588. var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
  589. if (regPos.test(val) || regNeg.test(val)) {
  590. return true
  591. } else {
  592. return false
  593. }
  594. }
  595. var distance = function(pointA, pointB, type) {
  596. var P1 = getBoundingClientRect(pointA),
  597. P2 = getBoundingClientRect(pointB);
  598. var dist = 0;
  599. switch (type) {
  600. case "AA":
  601. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.top - P2.top) * (P1.top - P2.top)))
  602. break;
  603. case "AB":
  604. dist = Math.sqrt(((P1.left - P2.right) * (P1.left - P2.right)) + ((P1.top - P2.top) * (P1.top - P2.top)))
  605. break;
  606. case "AC":
  607. dist = Math.sqrt(((P1.left - P2.right) * (P1.left - P2.right)) + ((P1.top - P2.bottom) * (P1.top - P2.bottom)))
  608. break;
  609. case "AD":
  610. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.top - P2.bottom) * (P1.top - P2.bottom)))
  611. break;
  612. case "BA":
  613. dist = Math.sqrt(((P1.right - P2.left) * (P1.right - P2.left)) + ((P1.top - P2.top) * (P1.top - P2.top)))
  614. break;
  615. case "BB":
  616. dist = Math.sqrt(((P1.right - P2.right) * (P1.right - P2.right)) + ((P1.top - P2.top) * (P1.top - P2.top)))
  617. break;
  618. case "BC":
  619. dist = Math.sqrt(((P1.right - P2.right) * (P1.right - P2.right)) + ((P1.top - P2.bottom) * (P1.top - P2.bottom)))
  620. break;
  621. case "BD":
  622. dist = Math.sqrt(((P1.right - P2.left) * (P1.right - P2.left)) + ((P1.top - P2.bottom) * (P1.top - P2.bottom)))
  623. break;
  624. case "CA":
  625. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.bottom - P2.top) * (P1.bottom - P2.top)))
  626. break;
  627. case "CB":
  628. dist = Math.sqrt(((P1.left - P2.right) * (P1.left - P2.right)) + ((P1.bottom - P2.top) * (P1.bottom - P2.top)))
  629. break;
  630. case "CC":
  631. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.bottom - P2.bottom) * (P1.bottom - P2.bottom)))
  632. break;
  633. case "CD":
  634. dist = Math.sqrt(((P1.right - P2.left) * (P1.right - P2.left)) + ((P1.bottom - P2.bottom) * (P1.bottom - P2.bottom)))
  635. break;
  636. case "DA":
  637. dist = Math.sqrt(((P1.left - P2.left) * (P1.left - P2.left)) + ((P1.bottom - P2.top) * (P1.bottom - P2.top)))
  638. break;
  639. case "DB":
  640. dist = Math.sqrt(((P1.right - P2.right) * (P1.right - P2.right)) + ((P1.bottom - P2.top) * (P1.bottom - P2.top)))
  641. break;
  642. case "DC":
  643. dist = Math.sqrt(((P1.right - P2.left) * (P1.right - P2.left)) + ((P1.bottom - P2.bottom) * (P1.bottom - P2.bottom)))
  644. break;
  645. case "DD":
  646. dist = Math.sqrt(((P1.right - P2.right) * (P1.right - P2.right)) + ((P1.bottom - P2.bottom) * (P1.bottom - P2.bottom)))
  647. break
  648. }
  649. return dist
  650. }
  651. self.ruleFn = function(index, direction) {
  652. var obj = {};
  653. if (rules) {
  654. obj = _self.getCurRule()
  655. }
  656. if (obj && typeof obj == "object" && typeof obj["line"] != "undefined") {
  657. var line = obj["line"]
  658. } else {
  659. var line = _this.hotbtn.length
  660. }
  661. line = parseInt(line);
  662. if (obj && typeof obj == "object" && typeof obj[_this.currentIndex] != "undefined" && typeof obj[_this.currentIndex][index] != "undefined") {
  663. var objRules = obj[_this.currentIndex];
  664. if (self.isNumber(objRules[index])) {
  665. _this.currentIndex = parseInt(_this.currentIndex) + parseInt(objRules[index])
  666. } else if (Array.isArray(objRules[index])) {
  667. _this.reSetClass(objRules[index][0], objRules[index][1], objRules[index][2])
  668. } else if (typeof objRules[index] == "function") {
  669. objRules[index].call(_this)
  670. } else if (typeof obj["line"] != "undefined") {
  671. _this.currentIndex = _this.currentIndex + line
  672. }
  673. } else {
  674. var jump = element[_this.currentIndex].getAttribute("data-" + direction);
  675. jump = parseInt(jump);
  676. var max = Math.sqrt(window.screen.width * window.screen.width + window.screen.height * window.screen.height);
  677. if (direction == "up") {
  678. if (_this.currentIndex > line - 1 && !obj["autoMove"]) _this.currentIndex = jump ? _this.currentIndex - jump : _this.currentIndex - line;
  679. else if (obj && typeof obj["up"] == "object") _this.reSetClass(obj["up"][0], obj["up"][1], obj["up"][2]);
  680. else if (obj && typeof obj["up"] == "function") obj["up"].call(_this)
  681. else {
  682. var min1 = max,
  683. min2 = max,
  684. curIndex = _this.currentIndex,
  685. curIndex2 = _this.currentIndex;
  686. for (var i = 0; i < _this.hotbtn.length; i++) {
  687. var cur1 = distance(_this.current, _this.hotbtn[i], "AD");
  688. var cur2 = distance(_this.current, _this.hotbtn[i], "BC");
  689. if (cur1 < min1 && cur1 > 0 && getBoundingClientRect(_this.current).top > getBoundingClientRect(_this.hotbtn[i]).top && _this.currentIndex > i) {
  690. min1 = cur1;
  691. curIndex = i;
  692. }
  693. if (cur2 < min2 && cur2 > 0 && getBoundingClientRect(_this.current).top > getBoundingClientRect(_this.hotbtn[i]).top && _this.currentIndex > i) {
  694. min2 = cur2;
  695. curIndex2 = i;
  696. }
  697. }
  698. curIndex = min1 < min2 ? curIndex : curIndex2;
  699. _this.currentIndex = curIndex;
  700. }
  701. } else if (direction == "left") {
  702. if ((_this.currentIndex) % line != 0 && !obj["autoMove"]) _this.currentIndex = jump ? _this.currentIndex - jump : _this.currentIndex - 1;
  703. else if (obj && typeof obj["left"] == "object") _this.reSetClass(obj["left"][0], obj["left"][1], obj["left"][2]);
  704. else if (obj && typeof obj["left"] == "function") obj["left"].call(_this)
  705. else {
  706. var min1 = max,
  707. min2 = max
  708. curIndex = _this.currentIndex, curIndex2 = _this.currentIndex;
  709. for (var i = 0; i < _this.hotbtn.length; i++) {
  710. var cur1 = distance(_this.current, _this.hotbtn[i], "AB");
  711. var cur2 = distance(_this.current, _this.hotbtn[i], "DC");
  712. if (cur1 < min1 && cur1 > 0 && getBoundingClientRect(_this.current).left > getBoundingClientRect(_this.hotbtn[i]).left && _this.currentIndex > i) {
  713. min1 = cur1;
  714. curIndex = i;
  715. }
  716. if (cur2 < min2 && cur2 > 0 && getBoundingClientRect(_this.current).left > getBoundingClientRect(_this.hotbtn[i]).left && _this.currentIndex > i) {
  717. min2 = cur2;
  718. curIndex2 = i;
  719. }
  720. }
  721. curIndex = min1 < min2 ? curIndex : curIndex2;
  722. _this.currentIndex = curIndex
  723. }
  724. } else if (direction == "right") {
  725. if ((_this.currentIndex + 1) % line != 0 && !obj["autoMove"]) _this.currentIndex = jump ? _this.currentIndex + jump : _this.currentIndex + 1;
  726. else if (obj && typeof obj["right"] == "object") _this.reSetClass(obj["right"][0], obj["right"][1], obj["right"][2]);
  727. else if (obj && typeof obj["right"] == "function") obj["right"].call(_this)
  728. else {
  729. var min1 = max,
  730. min2 = max,
  731. curIndex = _this.currentIndex,
  732. curIndex2 = _this.currentIndex;
  733. for (var i = 0; i < _this.hotbtn.length; i++) {
  734. var cur1 = distance(_this.current, _this.hotbtn[i], "BA");
  735. var cur2 = distance(_this.current, _this.hotbtn[i], "CD");
  736. if (cur1 < min1 && cur1 > 0 && getBoundingClientRect(_this.current).right < getBoundingClientRect(_this.hotbtn[i]).right && _this.currentIndex < i) {
  737. min1 = cur1;
  738. curIndex = i;
  739. }
  740. if (cur2 < min2 && cur2 > 0 && getBoundingClientRect(_this.current).right < getBoundingClientRect(_this.hotbtn[i]).right && _this.currentIndex < i) {
  741. min2 = cur2;
  742. curIndex2 = i;
  743. }
  744. }
  745. curIndex = min1 < min2 ? curIndex : curIndex2;
  746. _this.currentIndex = curIndex;
  747. }
  748. } else if (direction == "down") {
  749. if (_this.hotbtn.length - line > _this.currentIndex && !obj["autoMove"]) _this.currentIndex = jump ? _this.currentIndex + jump : _this.currentIndex + line;
  750. else if (obj && typeof obj["down"] == "object") _this.reSetClass(obj["down"][0], obj["down"][1], obj["down"][2]);
  751. else if (obj && typeof obj["down"] == "function") obj["down"].call(_this);
  752. 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) {
  753. _this.currentIndex = _this.currentIndex + line;
  754. self.overIndex()
  755. } else {
  756. var min1 = max,
  757. min2 = max,
  758. curIndex = _this.currentIndex,
  759. curIndex2 = _this.currentIndex;
  760. for (var i = 0; i < _this.hotbtn.length; i++) {
  761. var cur1 = distance(_this.current, _this.hotbtn[i], "DA");
  762. var cur2 = distance(_this.current, _this.hotbtn[i], "CB");
  763. if (cur1 < min1 && cur1 > 0 && getBoundingClientRect(_this.current).bottom < getBoundingClientRect(_this.hotbtn[i]).bottom && _this.currentIndex < i) {
  764. min1 = cur1;
  765. curIndex = i;
  766. }
  767. if (cur2 < min2 && cur2 > 0 && getBoundingClientRect(_this.current).bottom < getBoundingClientRect(_this.hotbtn[i]).bottom && _this.currentIndex < i) {
  768. min2 = cur2;
  769. curIndex2 = i;
  770. }
  771. }
  772. curIndex = min1 < min2 ? curIndex : curIndex2;
  773. _this.currentIndex = curIndex;
  774. }
  775. }
  776. }
  777. }
  778. self.rule = function(keyCode) {
  779. if (element.length <= 0) {
  780. console.log("初始化" + _this.className + "失败");
  781. return
  782. }
  783. self.overIndex();
  784. if (keyCode == btnLeft) {
  785. self.ruleFn(0, "left")
  786. } else if (keyCode == btnRight) {
  787. self.ruleFn(2, "right")
  788. } else if (keyCode == btnUp) {
  789. self.ruleFn(1, "up")
  790. } else if (keyCode == btnDown) {
  791. self.ruleFn(3, "down")
  792. }
  793. self.overIndex()
  794. }
  795. this.BACK = function() {
  796. self.onPressFn("back")
  797. }
  798. this.LEFT = function() {
  799. self.onPressFn("left")
  800. }
  801. this.RIGHT = function() {
  802. self.onPressFn("right")
  803. }
  804. this.UP = function() {
  805. self.onPressFn("up")
  806. }
  807. this.DOWN = function() {
  808. self.onPressFn("down")
  809. }
  810. self.onPressFn = function(e) {
  811. var keyCode = "";
  812. if (typeof e == "object") {
  813. keyCode = e.keyCode
  814. } else {
  815. if (e == "left") {
  816. keyCode = btnLeft
  817. } else if (e == "right") {
  818. keyCode = btnRight
  819. } else if (e == "up") {
  820. keyCode = btnUp
  821. } else if (e == "down") {
  822. keyCode = btnDown
  823. } else if (e == "back") {
  824. keyCode = btnBack
  825. }
  826. }
  827. _this.event = e;
  828. _this.currentIndex = _this.currentIndex >= element.length - 1 ? element.length - 1 : _this.currentIndex;
  829. _this.prev = element[_this.currentIndex];
  830. _this.prevIndex = _this.currentIndex;
  831. self.rule(keyCode);
  832. _this.current = element[_this.currentIndex];
  833. _this.currentIndex = _this.currentIndex;
  834. _this.className = _this.className;
  835. _this.onCommPress.call(_this);
  836. if (keyCode == 8 || keyCode == btnBack) {
  837. if (rules["#" + _this.currentId + ">." + _this.className] && (typeof rules["#" + _this.currentId + ">." + _this.className]["onBack"]) == "function") rules["#" + _this.currentId + ">." + _this.className]["onBack"].call(_this);
  838. else if (rules[_this.className] && (typeof rules[_this.className]["onBack"]) == "function") rules[_this.className]["onBack"].call(_this);
  839. else _this.onBack.call(_this)
  840. }
  841. if (rules && rules["#" + _this.currentId + ">." + _this.className] && (typeof rules["#" + _this.currentId + ">." + _this.className]["onPress"]) == "function") init.rules["#" + _this.currentId + ">." + _this.className]["onPress"].call(_this);
  842. else if (rules && rules[_this.className] && (typeof rules[_this.className]["onPress"]) == "function") init.rules[_this.className]["onPress"].call(_this);
  843. else _this.onPress.call(_this);
  844. if (keyCode == btnEnter) {
  845. if (rules["#" + _this.currentId + ">." + _this.className] && (typeof rules["#" + _this.currentId + ">." + _this.className]["onEnterPress"]) == "function") rules["#" + _this.currentId + ">." + _this.className]["onEnterPress"].call(_this);
  846. else if (rules[_this.className] && (typeof rules[_this.className]["onEnterPress"]) == "function") rules[_this.className]["onEnterPress"].call(_this);
  847. else _this.onEnterPress.call(_this)
  848. }
  849. _this.renderType = "";
  850. _self.render(_this.currentIndex);
  851. keydefault(e)
  852. }
  853. this.onLoad()
  854. }
  855. window.tvSysBtnBind = tvSysBtnBind
  856. })(window)