/** * 股票代码搜索框:StockSuggest 精简版 + 页面初始化。 * 来源:证券之星旧版 search.js;保留 JSONP 联想、结果表、键盘导航与表单提交。 * 维护:搜索框结构在 Includes/search.htm,由本站统一加载本文件。 */ (function () { var Class = { create: function () { return function () { this.initialize.apply(this, arguments) } } }; Object.extend = function (destination, source) { for (property in source) { destination[property] = source[property] } return destination; }; //Object.extend(Function.prototype, { // bind: function () { // var __m = this, // object = arguments[0], // args = new Array(); // for (var i = 1; i < arguments.length; i++) { // args.push(arguments[i]) // } // return function () { // return __m.apply(object, args) // } // } //}); var $Base = { $: function (objName) { if (document.getElementById) { return eval('document.getElementById("' + objName + '")') } else { return eval('document.all.' + objName) } }, isIE: navigator.appVersion.indexOf("MSIE") != -1 ? true : false, addEvent: function (l, i, I) { if (l.attachEvent) { l.attachEvent("on" + i, I) } else { l.addEventListener(i, I, false) } }, cE: function (o) { return document.createElement(o) }, getStyle: function (obj, prop) { if (obj.currentStyle) { return obj.currentStyle[prop]; } else if (window.getComputedStyle) { propprop = prop.replace(/([A-Z])/g, "-$1"); propprop = prop.toLowerCase(); return document.defaultView.getComputedStyle(obj, null)[propprop]; } return null; }, offset: function (o) { var x = 0, y = 0, element = o; y += element.offsetTop; x += element.offsetLeft; element = element.offsetParent; while (element.offsetParent) { var position = ($Base.getStyle(element, "position") || "").toLowerCase(); if (!(position == "absolute" || position == "relative")) { y += element.offsetTop; x += element.offsetLeft; } element = element.offsetParent; } return { 'left': x, 'top': y }; }, subString: function (str, len, eps) { eps = eps || false; if (!str || !len) { return ''; } var a = 0, i = 0, temp = ''; for (i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 255) a += 2; else a++; if (a > len) { return eps ? temp + "..." : temp; } temp += str.charAt(i); } return str; } }; var StockSuggest = Class.create(); Object.extend(StockSuggest.prototype, { initialize: function (obj, opt) { var self = this; self.url = (location.protocol.indexOf('s') >= 0 ? 'https:' : 'http:') + "//q.ssajax.cn/info/handler/xsuggesthandler.ashx"; self.input = obj; self.form = null; self.disabled = false; self.columns = { "选项": function (i, s) { var b = /([\^|\$|\.|\*|\+|\?|\=|\!|\:|\\|\/|\(|\)|\[|\]|\{|\}])/gi; var a = s.replace(b, "\\" + "$1"); var reg = new RegExp('^' + a + '.*$', 'im'); for (var sourceIndex = 0; sourceIndex < 3; sourceIndex++) { var content = i[sourceIndex]; if (reg.test(content)) { content = $Base.subString(content, 8); return ((typeof content === "string") ? content.replace(s.toUpperCase(), "" + s.toUpperCase() + "") : content) } } return i[0]; }, "代码": function (i, s) { return i[0]; }, "简称": function (i, s) { return i[1]; }, "拼音": function (i, s) { return i[2]; }, "类型": function (i, s) { return i[3]; } }; self._tableCssText = { thead: { "选项": "width:60px;text-align:center", "代码": "width:60px;text-align:center", "简称": "text-align:left;padding-left:24px", "拼音": "width:60px;text-align:center", "类型": "width:45px;text-align:center" }, tbody: { "选项": "text-align:center", "代码": "text-align:center", "简称": "text-align:left;padding-left:12px", "拼音": "text-align:center", "类型": "text-align:center" } }; self._U = null; self._hidden = false; self._cache = {}; self._enabled = true; self._index = -1; self.copyright = { version: '1.8', author: 'ly.deng' }; self.setOptions(opt); var b = []; for (var i = 0; i < self.options.header.columns.length; i++) { if (self.options.header.columns[i] != "选项") { b.push(self.options.header.columns[i]); } } self.options.header.columns = b; self.init(); }, setOptions: function (opt) { this.options = { width: "220px", opacity: 1, thColor: '#eeeeee', borderstyle: 'dotted', hoverColor: '#FEF175', offset: [0, 0], className: '', text: "代码/拼音/简称", rows: 10, //types: [101, 102, 103, 104, 105, 201, 300, 401, 403, 405, 501, 502, 503, 504, 801, 802, 9001], types: [101], status: [1, 6], orderBy: 2, resultVar: "result", header: { show: true, columns: ["选项", "简称", "类型"] }, autoSubmit: true, evt: false }; Object.extend(this.options, opt || {}); }, init: function () { this.input = typeof (this.input) == "string" ? $Base.$(this.input) : this.input; if (!this.input) { throw new Error("无对象"); } this.input.value = this.options.text; this.input.setAttribute("autocomplete", "off"); this.input.autoComplete = "off"; this.input.style.color = "#c5c5c5"; this.input.autoComplete = "off"; this.input.name = 'code'; if (this.form == null) { var _parent = this.input.parentNode; if (_parent.nodeName.toLowerCase() == "form") { this.form = _parent; } else { this.form = $Base.cE("form"); this.input.parentNode.insertBefore(this.form, this.input); var _i = this.input; this.input.parentNode.removeChild(this.input); this.form.appendChild(_i); } this.form.method = "post"; } if (!this.options.autoSubmit) { this.form.onsubmit = function () { return false; }; } $Base.addEvent(this.input, "focus", this.bind(this.inputFocus)); $Base.addEvent(this.input, "blur", this.bind(this.inputBlur)); $Base.addEvent(this.input, "keyup", this.bind(this.keyupListener)); $Base.addEvent(this.input, "keydown", this.bind(this.keydownListener)); this.evt = null; if (this.options.evt && (this.evt = $Base.$(this.options.evt))) { $Base.addEvent(this.evt, "click", this.bind(this.btnClick)); } }, bind: function (_b, _c) { var _d = this; return function () { var _e = null; if (typeof _c != "undefined") { for (var i = 0; i < arguments.length; i++) { _c.push(arguments[i]) } _e = _c } else { _e = arguments } return _b.apply(_d, _e) }; }, btnClick: function (e) { this.submit(); }, inputBlur: function (e) { if (this.input.value == "") { this.input.value = this.options.text; this._U = ""; this.hiddenWrapper() } else { this.hiddenWrapper(); } }, inputFocus: function (e) { if (this.input.value == this.options.text) { this.input.value = ""; this._U = ""; } this.dataTrigger(false); }, keyupListener: function (e) { var key = (window.event) ? window.event.keyCode : e.keyCode; switch (key) { case 38: if (this.wrapper != null && this.wrapper.innerHTML != "" && this._enabled && this.wrapper.firstChild.tBodies) { var _tbody = this.wrapper.firstChild.tBodies[0]; var _trows = _tbody.rows; var _rows = _trows.length; if (this._index == -1) this._index = _rows - 1; else this._index--; if (this._index < 0 || this._index > _rows - 1) { this._index = _rows - 1; } for (var i = 0; i < _rows; i++) { if (i == this._index) { _trows[i]._over = true; } else { _trows[i]._over = false; } this.setColor(_trows[i]); } this.input.value = (_trows[this._index].getAttribute("ref") || "").split(',')[0]; } break; case 40: if (this.wrapper != null && this.wrapper.innerHTML != "" && this._enabled && this.wrapper.firstChild.tBodies) { var _tbody = this.wrapper.firstChild.tBodies[0]; var _trows = _tbody.rows; var _rows = _trows.length; if (this._index == -1) { this._index = 0; } else { this._index++; } if (this._index > _rows - 1 || this._index < 0) { this._index = 0; } for (var i = 0; i < _rows; i++) { if (i == this._index) { _trows[i]._over = true; } else { _trows[i]._over = false; } this.setColor(_trows[i]); } this.input.value = (_trows[this._index].getAttribute("ref") || "").split(',')[0]; } break; default: var o = this; o.dataTrigger(true); break } }, keydownListener: function (e) { var key = (window.event) ? window.event.keyCode : e.keyCode; if (key == 13) { var self = this; self._hidden = false; self.submit(true); self.hiddenWrapper(); setTimeout(function () { self.input.blur(); }, 10); } }, dataTrigger: function (e) { this._index = -1; var _s = e ? this.input.value : (this._U || this.input.value); if (this._U != _s) { this._U = _s; if (_s != "") { if (("key_" + _s) in this._cache) { this.dataFill() } else { if (this._R == null) { this._R = $Base.cE("div"); this._R.style.display = "none"; document.body.insertBefore(this._R, document.body.lastChild) } var _js = $Base.cE('script'); var self = this; var _name = self.options.resultVar; var _url = this.url + '?' + (['q=' + encodeURIComponent(encodeURIComponent(_s)), 'type=' + this.options.types.join(','), 'n=' + _name, 'ls=' + this.options.status.join(','), 'key=false', 'order=' + this.options.orderBy, 'rows=' + this.options.rows, '_r=' + (new Date()).getTime()].join('&')); _js.setAttribute('charset', 'gb2312'); _js.setAttribute('type', 'text/javascript'); _js.setAttribute('src', _url); this._R.appendChild(_js); _js.onload = _js.onreadystatechange = function () { if (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") { self._cache["key_" + _s] = window[_name]; self.dataFill(); _js.onload = _js.onreadystatechange = null; window[_name] = null; this.parentNode.removeChild(this); } } } } else { this.hiddenWrapper(); } } else if (this._U != "") { this.showWrapper(); } }, hiddenWrapper: function () { if (this._hidden == false) { if (this.wrapper != null) this.wrapper.style.display = "none"; } }, showWrapper: function () { if (this.wrapper != null) this.wrapper.style.display = ""; }, dataFill: function () { var _s = this._U; if (("key_" + _s) in this._cache && this._cache["key_" + _s] != null && this._cache["key_" + _s] != undefined) { if (this.wrapper == null) { this.wrapper = $Base.cE("div"); this.wrapper.id = "ssajax_lib_suggest_result_layer_" + this.input.id; this.wrapper.className = this.options.className; this.input.parentNode.insertBefore(this.wrapper, this.input); } this.wrapper.style.cssText = "z-index:9999;width:" + this.options.width + ";opacity:" + this.options.opacity + ";filter:alpha(opacity:" + (this.options.opacity * 100) + ");position:absolute;display:none;border:1px " + this.options.borderstyle + " #ccc;padding: 5px;background-color: #ffffff;"; this.setPosition(this.wrapper); var _table = $Base.cE("table"); var _tbody = $Base.cE("tbody"); var _thead = $Base.cE("thead"); var _tfoot = $Base.cE("tfoot"); var _thead_tr = $Base.cE("tr"); _table.border = "0"; _table.cellPadding = "0"; _table.cellSpacing = "0"; _table.style.cssText = "line-height:18px;background:#FFF;font-size:12px;text-align:center;color:#666;width:100%;font-weight:normal"; _thead_tr.style.cssText = "background:" + this.options.thColor + ";height:22px;line-height:22px;overflow:hidden;font-weight:normal;color: #999999;"; if (this.options.header != null && this.options.header.show && this.options.header.columns && this.options.header.columns.length > 0) { var _th = $Base.cE("th"); _th.innerHTML = "选项"; _th.style.cssText = this._tableCssText.thead["选项"]; _thead_tr.appendChild(_th); for (var i = 0; i < this.options.header.columns.length; i++) { _th = $Base.cE("th"); _th.innerHTML = this.options.header.columns[i]; _th.style.cssText = this._tableCssText.thead[this.options.header.columns[i]]; _thead_tr.appendChild(_th); } _thead.appendChild(_thead_tr); _table.appendChild(_thead); } var _u = this._cache["key_" + _s] || { summary: { total: 0 }, datas: [] }; var _v = _u.datas.length > this.options.rows ? this.options.rows : _u.datas.length; this._enabled = _v > 0; for (var i = 0; i < _v; i++) { var _tbody_tr = $Base.cE("tr"); _tbody_tr.setAttribute("ref", _u.datas[i].join(",")); _tbody_tr.style.cursor = "pointer"; _tbody_tr.index = i; var self = this; _tbody_tr.onmouseover = function () { if (self.wrapper != null && self.wrapper.innerHTML != "" && self.wrapper.firstChild.tBodies) { var _tbody = self.wrapper.firstChild.tBodies[0]; var _trows = _tbody.rows; var _rows = _trows.length; for (var i = 0; i < _rows; i++) { _trows[i]._over = false; self.setColor(_trows[i]); } } this._over = true; self.setColor(this); }; _tbody_tr.onmouseout = function () { this._over = false; self.setColor(this); }; _tbody_tr.onmousedown = function () { self._index = this.index; self._hidden = true }; _tbody_tr.onclick = function () { self._hidden = false; this._over = false; self.hiddenWrapper(); self.setColor(this); self.submit(); }; var _t_td = $Base.cE("td"); _t_td.style.wordBreak = 'break-all'; _t_td.hidefocus = "true"; _t_td.style.padding = "1px"; _t_td.innerHTML = this.columns["选项"](_u.datas[i], _s); _t_td.style.cssText = this._tableCssText.tbody["选项"]; _tbody_tr.appendChild(_t_td); for (var j = 0; j < this.options.header.columns.length; j++) { _t_td = $Base.cE("td"); _t_td.style.wordBreak = 'break-all'; _t_td.hidefocus = "true"; _t_td.style.padding = "1px"; _t_td.style.cssText = this._tableCssText.tbody[this.options.header.columns[j]]; _t_td.innerHTML = this.columns[this.options.header.columns[j]](_u.datas[i], _s); _tbody_tr.appendChild(_t_td); if (_u.datas[i][5] == 0) { _t_td.colSpan = this.options.header.columns.length; _t_td.innerHTML = ""; _t_td.innerHTML = this.columns["简称"](_u.datas[i], _s); _t_td.style.cssText = this._tableCssText.tbody["简称"]; break; } } _t_td = null; _tbody.appendChild(_tbody_tr); } if (_v == 0) { var _tbody_tr = $Base.cE("tr"); var _t_td = $Base.cE("td"); _tbody_tr.style.cssText = "background:#FFF;height:25px;line-height:25px;overflow:hidden;color:#999"; _t_td.colSpan = this.options.header.columns.length + 1; _t_td.style.cssText = "text-align:center"; _t_td.innerHTML = "未找到符合条件的结果"; _tbody_tr.appendChild(_t_td); _tbody.appendChild(_tbody_tr); } _table.appendChild(_tbody); _table.appendChild(_tfoot); this.wrapper.innerHTML = ""; this.wrapper.appendChild(_table); this.showWrapper(); } else { this.hiddenWrapper() } }, setColor: function (o) { var _Bg = ""; if (o._over) { _Bg = this.options.hoverColor } else { _Bg = "#FFFFFF"; } o.style.backgroundColor = _Bg; }, setPosition: function (target) { var _offset = $Base.offset(this.input); var _j = _offset.top; var _k = _offset.left; var _l = [this.input.parentNode.style.borderTopWidth.replace("px", "") * 1, this.input.parentNode.style.borderLeftWidth.replace("px", "") * 2]; var _o = this.options.offset; if (target.style.top != _j + "px") { target.style.top = _j - _l[0] + _o[0] + "px" } if (target.style.left != _k + "px") { target.style.left = _k - _l[1] + _o[1] + "px" } var _p = this.input.style.borderTopWidth; var _q = this.input.style.borderBottomWidth; var _r = this.input.clientHeight; _r += _p != "" ? _p.replace("px", "") * 1 : 2; _r += _q != "" ? _q.replace("px", "") * 1 : 2; if (target.style.marginTop != _r + "px") { target.style.marginTop = _r + "px" } }, goto: function (url, e, m, t) { var self = this; e = e || false; self.form.action = url; self.form.method = m || "post"; self.form.target = t || "_blank"; self.form.onsubmit = function () { return true; }; if (!e) { self.form.submit(); self.form.onsubmit = function () { return false; }; } }, submit: function (isEnter) { var index = this._index; isEnter = isEnter || false; if (this.wrapper != null && this.wrapper.innerHTML != "" && this.wrapper.firstChild.tBodies) { var _tbody = this.wrapper.firstChild.tBodies[0]; var _trows = _tbody.rows; var _rows = _trows.length; if (index < 0) { var value = this.input.value, isExists = false, findItem = 0; if (_trows.length == 1) { var array = (_trows[0].getAttribute("ref") || "").split(","); if (array.length == 0) return false; if (array.length >= 3 && (array[0] == value || array[1].toLowerCase() == value.toLowerCase() || array[2].toLowerCase() == value.toLowerCase())) { isExists = true; index = 0; } } if (!isExists && this.options.autoSubmit) { var action = "//quote.stockstar.com/stock/exdir.aspx?code=" + value; var method = 'post'; this.goto(action, isEnter); return false; } } if (index < 0) index = 0; if (index > _rows - 1) index = _rows - 1; var _tRow = _trows[index]; _tRow._over = false; this.setColor(_tRow); var _i = (_tRow.getAttribute("ref") || "").split(","); if (_i.length == 0) return false; this.input.value = _i[0]; var action = location.protocol + "//quote.stockstar.com/stock/exdir.aspx?code=" + _i[0] + "&mk=" + _i[4] + "&securtytype=" + _i[5] + "&target=quotesearch"; var method = 'post'; switch (_i[5]) { case "101": case "102": case "103": case "104": case "105": action = "//q.ssajax.cn/info/handler/redirect.ashx?url=" + encodeURIComponent(location.protocol + "//stock.quote.stockstar.com/" + _i[0] + ".shtml"); break; case "201": case "202": case "203": case "204": action = "//q.ssajax.cn/info/handler/redirect.ashx?url=" + encodeURIComponent(location.protocol + "//index.quote.stockstar.com/" + _i[0] + ".shtml"); break; case "300": case "301": action = "//q.ssajax.cn/info/handler/redirect.ashx?url=" + encodeURIComponent(location.protocol + "//if.quote.stockstar.com/" + _i[0] + ".shtml"); break; case "401": case "403": case "404": action = "//q.ssajax.cn/info/handler/redirect.ashx?url=" + encodeURIComponent(location.protocol + "//bond.quote.stockstar.com/" + _i[0] + ".shtml"); break; case "500": case "501": case "502": case "503": case "504": action = "//q.ssajax.cn/info/handler/redirect.ashx?url=" + encodeURIComponent(location.protocol + "//fund.stockstar.com/funds/" + _i[0] + ".shtml"); break; case "9001": if (_i[0] == "000198QX") action = "//q.ssajax.cn/info/handler/redirect.ashx?url=" + encodeURIComponent(location.protocol + "//resource.stockstar.com/DataCenter/StockData/yeb.htm"); break; default: action = "//quote.stockstar.com/stock/exdir.aspx?code=" + _i[0] + "&mk=" + _i[4] + "&securtytype=" + _i[5] + "&target=quotesearch"; break; }; this.goto(action, isEnter, method); } else { var action = "//quote.stockstar.com/stock/exdir.aspx?code=" + this.input.value; this.goto(action, isEnter); return false; } } }); window.StockSuggest = StockSuggest; /* 页面初始化:新版搜索框 #ssType_0 */ (function () { 'use strict'; var input = document.getElementById('ajaxinput'); if (!input || typeof window.StockSuggest !== 'function') { return; } if (input.getAttribute('data-suggest-ready') === '1') { return; } input.setAttribute('data-suggest-ready', '1'); new window.StockSuggest('ajaxinput', { width: '205px', autoSubmit: true, className: 'ssajax_lib_suggest', types: [101, 102, 103, 104, 105, 201, 300, 401, 403, 405, 501, 502, 503, 504], status: [1, 2, 3, 4], opacity: 1, orderBy: 2, text: '代码/名称/拼音', header: { show: true, columns: ['简称', '类型'] }, evt: 'ajaxinputquery' }); function on(el, type, handler) { if (el.addEventListener) { el.addEventListener(type, handler, false); } else if (el.attachEvent) { el.attachEvent('on' + type, handler); } } // 库里默认把输入文字也设成灰色,聚焦时改回正文色。 on(input, 'focus', function () { input.style.color = '#333'; }); on(input, 'blur', function () { if (input.value === '代码/名称/拼音') { input.style.color = '#c5c5c5'; } }); }()); })();