123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
-
- var SimpleInputMethod =
- {
- hanzi: '',
- pinyin: '',
- result: [],
- pageCurrent: 1,
- pageSize: 5,
- pageCount: 0,
-
- initDict: function()
- {
- var dict = pinyinUtil.dict;
- if(!dict.py2hz) throw '未找到合适的字典文件!';
-
-
- dict.py2hz2 = {};
- dict.py2hz2['i'] = 'i';
- for(var i=97; i<=123; i++)
- {
- var ch = String.fromCharCode(i);
- if(!dict.py2hz[ch])
- {
- for(var j in dict.py2hz)
- {
- if(j.indexOf(ch) == 0)
- {
- dict.py2hz2[ch] = dict.py2hz[j];
- break;
- }
- }
- }
- }
- },
-
- initDom: function()
- {
- var temp = `<div class="pinyin"></div>
- <div class="result">
- <ol></ol>
- <div class="page-up-down"><span class="page-up">▲</span><span class="page-down">▼</span></div>
- </div>`;
- var dom = document.createElement('div');
- dom.id = 'simle_input_method';
- dom.className = 'simple-input-method';
- dom.innerHTML = temp;
- var that = this;
-
- dom.addEventListener('click', function(e)
- {
- var target = e.target;
- if(target.nodeName == 'LI') that.selectHanzi(parseInt(target.dataset.idx));
- else if(target.nodeName == 'SPAN')
- {
- if(target.className == 'page-up' && that.pageCurrent > 1)
- {
- that.pageCurrent--;
- that.refreshPage();
- }
- else if(target.className == 'page-down' && that.pageCurrent < that.pageCount)
- {
- that.pageCurrent++;
- that.refreshPage();
- }
- }
- })
- document.body.appendChild(dom);
- },
-
- init: function(selector)
- {
- this.initDict();
- this.initDom();
- obj = document.querySelectorAll(selector);
- this._target = document.querySelector('#simle_input_method');
- this._pinyinTarget = document.querySelector('#simle_input_method .pinyin');
- this._resultTarget = document.querySelector('#simle_input_method .result ol');
- var that = this;
- for(var i=0; i<obj.length; i++)
- {
- obj[i].addEventListener('keydown', function(e)
- {
- var keyCode = e.keyCode;
- var preventDefault = false;
- if(keyCode >= 65 && keyCode <= 90)
- {
- that.addChar(String.fromCharCode(keyCode+32), this);
- preventDefault = true;
- }
- else if(keyCode == 8 && that.pinyin)
- {
- that.delChar();
- preventDefault = true;
- }
- else if(keyCode >= 48 && keyCode <= 57 && !e.shiftKey && that.pinyin)
- {
- that.selectHanzi(keyCode-48);
- preventDefault = true;
- }
- else if(keyCode == 32 && that.pinyin)
- {
- that.selectHanzi(1);
- preventDefault = true;
- }
- else if(keyCode == 33 && that.pageCount > 0 && that.pageCurrent > 1)
- {
- that.pageCurrent--;
- that.refreshPage();
- preventDefault = true;
- }
- else if(keyCode == 34 && that.pageCount > 0 && that.pageCurrent < that.pageCount)
- {
- that.pageCurrent++;
- that.refreshPage();
- preventDefault = true;
- }
- if(preventDefault) e.preventDefault();
- });
- obj[i].addEventListener('focus', function()
- {
-
- if(that._input !== this) that.hide();
- });
- }
- },
-
- getSingleHanzi: function(pinyin)
- {
- return pinyinUtil.dict.py2hz2[pinyin] || pinyinUtil.dict.py2hz[pinyin] || '';
- },
-
- getHanzi: function(pinyin)
- {
- var result = this.getSingleHanzi(pinyin);
- if(result) return [result.split(''), pinyin];
- var temp = '';
- for(var i=0, len = pinyin.length; i<len; i++)
- {
- temp += pinyin[i];
- result = this.getSingleHanzi(temp);
- if(!result) continue;
-
- var flag = false;
- if((i+1) < pinyin.length)
- {
- for(var j=1, len = pinyin.length; j<=5 && (i+j)<len; j++)
- {
- if(this.getSingleHanzi(pinyin.substr(0, i+j+1)))
- {
- flag = true;
- break;
- }
- }
- }
- if(!flag) return [result.split(''), pinyin.substr(0, i+1) + "'" + pinyin.substr(i+1)];
- }
- return [[], ''];
- },
-
- selectHanzi: function(i)
- {
- var hz = this.result[(this.pageCurrent - 1) * this.pageSize + i - 1];
- if(!hz) return;
- this.hanzi += hz;
- var idx = this.pinyin.indexOf("'");
- if(idx > 0)
- {
- this.pinyin = this.pinyin.substr(idx+1);
- this.refresh();
- }
- else
- {
- this._input.value += this.hanzi;
- this.hide();
- }
- },
-
- refresh: function()
- {
- var temp = this.getHanzi(this.pinyin.replace(/'/g, ''));
- this.result = temp[0];
- this.pinyin = temp[1];
- var count = this.result.length;
- this.pageCurrent = 1;
- this.pageCount = Math.ceil(count / this.pageSize);
- this._pinyinTarget.innerHTML = this.hanzi + this.pinyin;
- this.refreshPage();
- },
- refreshPage: function()
- {
- var temp = this.result.slice((this.pageCurrent-1)*this.pageSize, this.pageCurrent*this.pageSize);
- var html = '';
- var i = 0;
- temp.forEach(function(val)
- {
- html += '<li data-idx="'+(++i)+'">' + val + '</li>';
- });
- this._target.querySelector('.page-up').style.opacity = this.pageCurrent > 1 ? '1' : '.3';
- this._target.querySelector('.page-down').style.opacity = this.pageCurrent < this.pageCount ? '1' : '.3';
- this._resultTarget.innerHTML = html;
- },
- addChar: function(ch, obj)
- {
- if(this.pinyin.length == 0)
- {
- this.show(obj);
- }
- this.pinyin += ch;
- this.refresh();
- },
- delChar: function()
- {
- if(this.pinyin.length <= 1)
- {
- this.hide();
- return;
- }
- this.pinyin = this.pinyin.substr(0, this.pinyin.length-1);
- this.refresh();
- },
- show: function(obj)
- {
- var pos = obj.getBoundingClientRect();
- this._target.style.left = pos.left + 'px';
- this._target.style.top = pos.top + pos.height + document.body.scrollTop + 'px';
- this._input = obj;
- this._target.style.display = 'block';
- },
- hide: function()
- {
- this.reset();
- this._target.style.display = 'none';
- },
- reset: function()
- {
- this.hanzi = '';
- this.pinyin = '';
- this.result = [];
- this.pageCurrent = 1;
- this.pageCount = 0;
- this._pinyinTarget.innerHTML = '';
- }
- };
|