try{ (function(arr) { //当前元素删除 arr.forEach(function(item) { if (item.hasOwnProperty('remove')) { return } Object.defineProperty(item, 'remove', { configurable: true, enumerable: true, writable: true, value: function remove() { this.parentNode.removeChild(this) } }) }) })([Element.prototype, CharacterData.prototype, DocumentType.prototype]); if (!("classList" in document.documentElement)) { // classList 兼容 Object.defineProperty(HTMLElement.prototype, 'classList', { get: function() { var self = this; function update(fn) { return function(value) { var classes = self.className.split(/\s+/g), index = classes.indexOf(value); fn(classes, index, value); self.className = classes.join(" ") } } return { add: update(function(classes, index, value) { if (!~index) classes.push(value) }), remove: update(function(classes, index) { if (~index) classes.splice(index, 1) }), toggle: update(function(classes, index, value) { if (~index) classes.splice(index, 1); else classes.push(value) }), contains: function(value) { return !!~self.className.split(/\s+/g).indexOf(value) }, item: function(i) { return self.className.split(/\s+/g)[i] || null } } } }) } function fireKeyEvent(el, evtType, keyCode) { //创建事件 var doc = el.ownerDocument, win = doc.defaultView || doc.parentWindow, evtObj; if (doc.createEvent) { if (win.KeyEvent) { evtObj = doc.createEvent('KeyEvents'); evtObj.initKeyEvent(evtType, true, true, win, false, false, false, false, keyCode, 0) } else { evtObj = doc.createEvent('UIEvents'); Object.defineProperty(evtObj, 'keyCode', { get: function() { return this.keyCodeVal } }); Object.defineProperty(evtObj, 'which', { get: function() { return this.keyCodeVal } }); evtObj.initUIEvent(evtType, true, true, win, 1); evtObj.keyCodeVal = keyCode; if (evtObj.keyCode !== keyCode) { // console.log("keyCode " + evtObj.keyCode + " 和 (" + evtObj.which + ") 不匹配") } } el.dispatchEvent(evtObj) } else if (doc.createEventObject) { evtObj = doc.createEventObject(); evtObj.keyCode = keyCode; el.fireEvent('on' + evtType, evtObj) } } }catch(e){ //版本很低 } function addClass(ele, cls) { if(ele.classList){ ele.classList.add(cls); }else{ if (!this.hasClass(ele, cls)) ele.className += " " + cls; } } function arrIndexOf(arr, v) { for (var i = 0; i < arr.length; i++) { if (arr[i] == v) { return i; } } return -1; } //删除指定dom元素的样式 function removeClass(ele, cls) { if(ele.classList){ ele.classList.remove(cls); }else{ if (ele.className != '' && hasClass(ele, cls)) { var arrClassName = ele.className.split(' '); var classIndex = arrIndexOf(arrClassName, cls); if (classIndex!==-1) { arrClassName.splice(classIndex, 1); ele.className = arrClassName.join(' '); } } } } //如果存在(不存在),就删除(添加)一个样式 function toggleClass(ele,cls){ if(hasClass(ele,cls)){ removeClass(ele, cls); }else{ addClass(ele, cls); } } function hasClass(element, cls) { return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1; } // function hasClass(tagStr,classStr){ // if(tagStr.classList){ // return tagStr.classList.contains(classStr); // } else{ // var arr=tagStr.className.split(/\s+/ ); //这个正则表达式是因为class可以有多个,判断是否包含 // for (var i=0;i." + _this.className]; if (typeof obj == "undefined") obj = rules[_this.className] } return obj } this.reSetClass = function(item, index, curClass) { var obj =_self.getCurRule(); if(typeof obj["history"]=="undefined") obj["history"]=history; //默认历史记录开关 if (obj["history"]) _this.historyFocus[_this.currentId+_this.className] = _this.currentIndex; //当前历史开关 //index = index ? index : 0; _this.prevCurrentClass = _this.currentClass; if (curClass) { _this.currentClass = curClass } var arr = item.split(">"); for (var i = 0; i < arr.length; i++) { if (arr[i] == "") arr.splice(i) } for (var i = 0; i < arr.length; i++) { if (arr[i].indexOf("#") != -1) { _this.currentId=arr[i].replace("#", ""); if (document.getElementById( _this.currentId)) _this.target = document.getElementById( _this.currentId); } if (arr[i].indexOf(".") != -1) { item = arr[i].replace(".", "") } if (arr[i].indexOf(".") == -1 && arr[i].indexOf("#") == -1) { item = arr[i] } } if (getClassNames(item,_this.target).length > 0) { _self.newItem(item, index) } else { _this.target = _this.defaultTarget; if (getClassNames(item,_this.target).length > 0) _self.newItem(item, index) } } self.newItem = function(item, index) { if (_this.prev) removeClass(_this.prev,_this.currentClass); //_this.target=document.getElementById(_this.currentId); if(typeof _this.historyFocus[_this.currentId+item]=="undefined") _this.historyFocus[_this.currentId+item]=0; _this.className = item; _this.prevIndex = _this.currentIndex = typeof index!="undefined" ?index: _this.historyFocus[_this.currentId+item]; _this.reLoad(); } self.readFn = function() { if ((typeof init.onEnterPress) == "function") { init.onEnterPress } else { init.onEnterPress = function() {} } if ((typeof init.onPress) == "function") { init.onPress } else { init.onPress = function() {} } } this.reLoad = function() { self.readFn(); element =getClassNames(_this.className,_this.target); this.hotbtn = element; if (element.length <= 0) return false; if (_this.currentIndex || _this.currentIndex == 0) { _this.currentIndex = parseInt(_this.currentIndex) } else { _this.currentIndex = parseInt(init.currentIndex) } if (isload >= 2 && _this.sourceClass == _this.className && _this.sourceLength != element.length && !isSet) { if (hasClass(_this.prev,_this.className) && _this.sourceLength > element.length) { _this.currentIndex = _this.currentIndex - (_this.sourceLength - element.length); _this.prevIndex = _this.prevIndex - (_this.sourceLength - element.length); _this.sourceLength = element.length } else if (hasClass(_this.prev,_this.className) && _this.sourceLength < element.length) { _this.currentIndex = _this.currentIndex + (element.length - _this.sourceLength); _this.prevIndex = _this.prevIndex + (element.length - _this.sourceLength); _this.sourceLength = element.length } } isSet = false; _this.current = element[_this.currentIndex]; _this.currentIndex = _this.currentIndex; _self.classDo(_this.currentIndex); for (var i = 0; i < this.hotbtn.length; i++) { this.hotbtn[i].setAttribute("data-id", i) } } function keydefault(e) { try { if (keyRemoveDefault) window.event ? window.event.returnValue = false : e.preventDefault() } catch (e) {} } var isSet = false; this.setCurrentIndex = function(index) { isSet = true; index = parseInt(index); _this.currentIndex = index; _this.current = element[index] } var maxTop = 0; this.viewScrollY = function(y,view) { var obj = self.getCurRule(); // var view = _this.current.parentNode.parentNode; var sumtop = obj["directionY"] ? parseInt(obj["directionY"]) : 0; var top = sumtop + y + view.clientHeight / 2 - Math.ceil(_this.current.clientHeight / 2); if (top > 0) top = 0; obj["directionY"]=top; view.children[0].style.top = top + "px" } var maxLeft = 0; this.viewScrollX = function(x,view) { var obj = self.getCurRule(); // var view = _this.current.parentNode.parentNode; var sumleft = obj["directionX"] ? parseInt(obj["directionX"]) : 0; var left = sumleft + x + view.clientWidth / 2 - Math.ceil(_this.current.clientWidth / 2); if (isCentered) { if (left > 0) left = 0; if (obj["maxLeft"]==undefined||obj["maxLeft"]==null) { maxLeft = getBoundingClientRect(_this.hotbtn[_this.hotbtn.length - 1]).right - getBoundingClientRect(view).right; obj["maxLeft"]=maxLeft; } if (left < sumleft && left < -maxLeft) { left = -maxLeft } // if(maxLeft document.body.clientHeight || getBoundingClientRect(_this.current).top < 0) { var y = getBoundingClientRect(_this.current).top - (document.body.clientHeight / 2 - _this.current.clientHeight / 2); window.scrollTo(0, y) } var view = _this.current.parentNode.parentNode; if(obj["directionParent"])view=obj["directionParent"]; // var direction = view.getAttribute("data-scroll-direction"); // if (!direction) { // view = _this.current.parentNode.parentNode.parentNode; // direction = view.getAttribute("data-scroll-direction"); // } if (obj == undefined || !obj["direction"] )return; view.style.position = "relative"; view.children[0].style.position = "absolute"; var sumleft = obj["directionX"] ? parseInt(obj["directionX"]) : 0; if (obj["direction"] == "x") { view.children[0].style.width = _this.hotbtn.length * _this.current.clientWidth * 2 + "px"; var scroll_left = getBoundingClientRect(view).left - getBoundingClientRect(_this.current).left; if( _this.hotbtn.length * _this.current.clientWidth>view.clientWidth) _this.viewScrollX(scroll_left,view); } var sumleft = obj["directionY"] ? parseInt(obj["directionY"]) : 0; if (obj["direction"] == "y") { var scroll_top = getBoundingClientRect(view).top - getBoundingClientRect(_this.current).top; _this.viewScrollY(scroll_top,view) } } var isload = 0; self.classDo = function(index) { isload = isload + 1; if (isload == 2) { _this.sourceLength = element.length } if (_this.prevCurrentClass) { var ele = getClassNames(_this.prevCurrentClass,_this.target) ; for (var i = 0; i < ele.length; i++) { if (hasClass(ele[i],_this.prevCurrentClass)) { removeClass(ele[i],_this.prevCurrentClass); } } } if (element[index]) addClass(element[index],_this.currentClass); else return; for (var i = 0; i < element.length; i++) { if (i != index && hasClass(element[i],_this.currentClass)) { removeClass(element[i],_this.currentClass); } } _this.scroll(); var effect = element[index].getAttribute("data-effect"); if (effect) { 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;"); focusobj.setAttribute("class", "focusobj current " + effect); focusobj.style.display = "list-item" } else { focusobj.setAttribute("style", ""); focusobj.style.display = "none" } } self.EventUtil = { add: function(obj, callback) { if (typeof(obj.onkeypress) == "null") { obj.onkeypress = function(e) { callback && callback(e) } } else { obj.onkeydown = function(e) { callback && callback(e) } } } } try{ Webview.requestFocus(); Webview.setKeyEventHandler(function (action, keyCode, keyName, metaState){ var e = this; e.keyCode = keyName; _self.onPressdo(e); // _this.makepress(keyName) }) }catch(err){ self.EventUtil.add(document, function (e) { _self.onPressdo(e); }); } // EventUtil.add(document, function(e) { // _self.onPressdo(e) // }); self.overIndex = function() { if (_this.currentIndex >= element.length - 1) { _this.currentIndex = element.length - 1 } if (_this.currentIndex < 0) { _this.currentIndex = 0 } } self.isNumber = function(val) { var regPos = /^\d+(\.\d+)?$/; var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; if (regPos.test(val) || regNeg.test(val)) { return true } else { return false } } self.ruleFn = function(index, direction) { var obj = {}; if (rules) { obj =_self.getCurRule(); } if (obj && typeof obj == "object" && typeof obj["line"] != "undefined") { var line = obj["line"] } else { var line = _this.hotbtn.length } line = parseInt(line); if (obj && typeof obj == "object" && typeof obj[_this.currentIndex] != "undefined" && typeof obj[_this.currentIndex][index] != "undefined") { var objRules = obj[_this.currentIndex]; if (self.isNumber(objRules[index])) { _this.currentIndex = parseInt(_this.currentIndex) + parseInt(objRules[index]) } else if (Array.isArray(objRules[index])) { _this.reSetClass(objRules[index][0], objRules[index][1]) } else if (typeof obj["line"] != "undefined") { _this.currentIndex = _this.currentIndex + line } } else { var jump = element[_this.currentIndex].getAttribute("data-" + direction); jump = parseInt(jump); if (direction == "up") { if (_this.currentIndex > line - 1) _this.currentIndex = jump ? _this.currentIndex - jump : _this.currentIndex - line; else if (obj && typeof obj["up"] == "object") _this.reSetClass(obj[direction][0], obj["up"][1], obj["up"][2], obj["up"][3]); else if (obj && typeof obj["up"] == "function") obj["up"].call(_this) } else if (direction == "left") { if ((_this.currentIndex) % line != 0) _this.currentIndex = jump ? _this.currentIndex - jump : _this.currentIndex - 1; else if (obj && typeof obj["left"] == "object") _this.reSetClass(obj["left"][0], obj["left"][1], obj["left"][2], obj["left"][3]); else if (obj && typeof obj["left"] == "function") obj["left"].call(_this) } else if (direction == "right") { if ((_this.currentIndex + 1) % line != 0) _this.currentIndex = jump ? _this.currentIndex + jump : _this.currentIndex + 1; else if (obj && typeof obj["right"] == "object") _this.reSetClass(obj["right"][0], obj["right"][1], obj["right"][2], obj["right"][3]); else if (obj && typeof obj["right"] == "function") obj["right"].call(_this) } else if (direction == "down") { if (_this.hotbtn.length - line > _this.currentIndex) _this.currentIndex = jump ? _this.currentIndex + jump : _this.currentIndex + line; else if (obj && typeof obj["down"] == "object") _this.reSetClass(obj["down"][0], obj["down"][1], obj["down"][2], obj["down"][3]); else if (obj && typeof obj["down"] == "function") obj["down"].call(_this); 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) { _this.currentIndex = _this.currentIndex + line; self.overIndex() } } } } self.rule = function() { self.overIndex(); if (_this.event.keyCode == btnLeft || _this.event.keyCode == "LEFT") { self.ruleFn(0, "left") } else if (_this.event.keyCode == btnRight || _this.event.keyCode == "RIGHT") { self.ruleFn(2, "right") } else if (_this.event.keyCode == btnUp || _this.event.keyCode == "UP") { self.ruleFn(1, "up") } else if (_this.event.keyCode == btnDown || _this.event.keyCode == "DOWN") { self.ruleFn(3, "down") } self.overIndex() } this.back=function(){ _this.event.keyCode=8; self.onPressdo(_this.event); } // this.makepress=function(k){ // if(k==""){ // _this.event.keyCode=btnLeft; // }else if(k=="RIGHT"){ // _this.event.keyCode=btnRight; // }else if(k=="UP"){ // _this.event.keyCode=btnUp; // }else if(k=="DOWN"){ // _this.event.keyCode=btnDown; // }else if(k=="ENTER"){ // _this.event.keyCode=btnEnter; // } // self.onPressdo(_this.event); // } self.onPressdo = function(e) { _this.event = e; _this.currentIndex = _this.currentIndex >= element.length - 1 ? element.length - 1 : _this.currentIndex; _this.prev = element[_this.currentIndex]; _this.prevIndex = _this.currentIndex; self.rule(); _this.current = element[_this.currentIndex]; _this.currentIndex = _this.currentIndex; _this.className = _this.className; //$.tips(e.keyCode) if (e.keyCode == 8 || e.keyCode == 27 || e.keyCode == "BACK") { if (rules["#" + _this.currentId + ">." +_this.className] && (typeof rules["#" + _this.currentId + ">." +_this.className]["onBack"]) == "function") rules["#" + _this.currentId + ">." +_this.className]["onBack"].call(_this); else if (rules[_this.className] && (typeof rules[_this.className]["onBack"]) == "function") rules[_this.className]["onBack"].call(_this); else _this.onBack.call(_this) } if (rules && rules["#" + _this.currentId + ">." + _this.className] && (typeof rules["#" + _this.currentId + ">." + _this.className]["onPress"]) == "function") init.rules["#" + _this.currentId + ">." + _this.className]["onPress"].call(_this); else if (rules && rules[_this.className] && (typeof rules[_this.className]["onPress"]) == "function") init.rules[_this.className]["onPress"].call(_this); else _this.onPress.call(_this); if (e.keyCode == btnEnter || e.keyCode == "ENTER") { if (rules["#" + _this.currentId + ">." +_this.className] && (typeof rules["#" + _this.currentId + ">." +_this.className]["onEnterPress"]) == "function") rules["#" + _this.currentId + ">." +_this.className]["onEnterPress"].call(_this); else if (rules[_this.className] && (typeof rules[_this.className]["onEnterPress"]) == "function") rules[_this.className]["onEnterPress"].call(_this); else _this.onEnterPress.call(_this) } _self.classDo(_this.currentIndex); keydefault(e) } this.onLoad(); } window.tvSysBtnBind = tvSysBtnBind })(window)