| | 121 | |
|---|
| | 122 | var AutoKana=Class.create(); |
|---|
| | 123 | Object.extend(AutoKana,{kana_extraction_pattern:new RegExp(decodeURIComponent("%5B%5E%20%E3%80%80%E3%81%81%E3%81%82-%E3%82%93%E3%83%BC%5D"),"g"),kana_compacting_pattern:new RegExp(decodeURIComponent("%5B%E3%81%81%E3%81%83%E3%81%85%E3%81%87%E3%81%89%E3%81%A3%E3%82%83%E3%82%85%E3%82%87%5D"),"g"),label_string_hiragana:decodeURIComponent("%E3%81%8B%E3%81%AA%E8%87%AA%E5%8B%95%E8%A3%9C%E5%AE%8C"),label_string_katakana:decodeURIComponent("%E3%82%AB%E3%83%8A%E8%87%AA%E5%8B%95%E8%A3%9C%E5%AE%8C")}); |
|---|
| | 124 | Object.extend(AutoKana.prototype,{initialize:function(_1,_2){ |
|---|
| | 125 | this.elName=$(_1); |
|---|
| | 126 | this.elKana=$(_2); |
|---|
| | 127 | var _3={build:true,katakana:false,toggle:true}; |
|---|
| | 128 | this.options=Object.extend(_3,arguments[2]||{}); |
|---|
| | 129 | this.active=true; |
|---|
| | 130 | this._stateClear(); |
|---|
| | 131 | this._build(); |
|---|
| | 132 | },start:function(){ |
|---|
| | 133 | this.active=true; |
|---|
| | 134 | },stop:function(){ |
|---|
| | 135 | this.active=false; |
|---|
| | 136 | },toggle:function(_4){ |
|---|
| | 137 | var ev=_4||window.event; |
|---|
| | 138 | if(_4){ |
|---|
| | 139 | var el=Event.element(_4); |
|---|
| | 140 | if(el.checked){ |
|---|
| | 141 | this.active=true; |
|---|
| | 142 | }else{ |
|---|
| | 143 | this.active=false; |
|---|
| | 144 | } |
|---|
| | 145 | }else{ |
|---|
| | 146 | this.active=!this.active; |
|---|
| | 147 | } |
|---|
| | 148 | },_addEvents:function(){ |
|---|
| | 149 | Event.observe(this.elName,"blur",this._eventBlur.bindAsEventListener(this)); |
|---|
| | 150 | Event.observe(this.elName,"focus",this._eventFocus.bindAsEventListener(this)); |
|---|
| | 151 | Event.observe(this.elName,"keydown",this._eventKeyDown.bindAsEventListener(this)); |
|---|
| | 152 | },_build:function(){ |
|---|
| | 153 | if(this.options.build){ |
|---|
| | 154 | this._addEvents(); |
|---|
| | 155 | } |
|---|
| | 156 | if(this.options.toggle){ |
|---|
| | 157 | this._buildToggle(); |
|---|
| | 158 | } |
|---|
| | 159 | },_buildToggle:function(){ |
|---|
| | 160 | var _7=this.elName.parentNode; |
|---|
| | 161 | var _8=document.createElement("div"); |
|---|
| | 162 | var _9=this._buildToggleCheckBox(); |
|---|
| | 163 | var _a=this._buildToggleLabel(); |
|---|
| | 164 | _7.replaceChild(_8,this.elName); |
|---|
| | 165 | _8.appendChild(this.elName); |
|---|
| | 166 | _8.appendChild(_9); |
|---|
| | 167 | _8.appendChild(_a); |
|---|
| | 168 | },_buildToggleCheckBox:function(){ |
|---|
| | 169 | var _b=this; |
|---|
| | 170 | var el=document.createElement("input"); |
|---|
| | 171 | el.type="checkbox"; |
|---|
| | 172 | el.id=this.elName.id+"_toggle"; |
|---|
| | 173 | el.checked=true; |
|---|
| | 174 | el.style.border="none"; |
|---|
| | 175 | el.style.background="transparent"; |
|---|
| | 176 | el.style.cursor="pointer"; |
|---|
| | 177 | Event.observe(el,"click",this.toggle.bindAsEventListener(this)); |
|---|
| | 178 | return el; |
|---|
| | 179 | },_buildToggleLabel:function(){ |
|---|
| | 180 | var el=document.createElement("label"); |
|---|
| | 181 | el.htmlFor=this.elName.id+"_toggle"; |
|---|
| | 182 | el.style.cursor="pointer"; |
|---|
| | 183 | el.innerHTML=((this.options.katakana)?AutoKana.label_string_katakana:AutoKana.label_string_hiragana); |
|---|
| | 184 | return el; |
|---|
| | 185 | },_checkConvert:function(_e){ |
|---|
| | 186 | if(!this.flagConvert){ |
|---|
| | 187 | if(Math.abs(this.values.length-_e.length)>1){ |
|---|
| | 188 | var _f=_e.join("").replace(AutoKana.kana_compacting_pattern,"").split(""); |
|---|
| | 189 | if(Math.abs(this.values.length-_f.length)>1){ |
|---|
| | 190 | this._stateConvert(); |
|---|
| | 191 | } |
|---|
| | 192 | }else{ |
|---|
| | 193 | if(this.values.length==this.input.length&&this.values.join("")!=this.input){ |
|---|
| | 194 | this._stateConvert(); |
|---|
| | 195 | } |
|---|
| | 196 | } |
|---|
| | 197 | } |
|---|
| | 198 | },_checkValue:function(){ |
|---|
| | 199 | var _10,_11; |
|---|
| | 200 | _10=this.elName.value; |
|---|
| | 201 | if(_10==""){ |
|---|
| | 202 | this._stateClear(); |
|---|
| | 203 | this._setKana(); |
|---|
| | 204 | }else{ |
|---|
| | 205 | _10=this._removeString(_10); |
|---|
| | 206 | if(this.input==_10){ |
|---|
| | 207 | return; |
|---|
| | 208 | }else{ |
|---|
| | 209 | this.input=_10; |
|---|
| | 210 | if(!this.flagConvert){ |
|---|
| | 211 | _11=_10.replace(AutoKana.kana_extraction_pattern,"").split(""); |
|---|
| | 212 | this._checkConvert(_11); |
|---|
| | 213 | this._setKana(_11); |
|---|
| | 214 | } |
|---|
| | 215 | } |
|---|
| | 216 | } |
|---|
| | 217 | },_clearInterval:function(){ |
|---|
| | 218 | clearInterval(this.timer); |
|---|
| | 219 | },_eventBlur:function(_12){ |
|---|
| | 220 | this._clearInterval(); |
|---|
| | 221 | },_eventFocus:function(_13){ |
|---|
| | 222 | this._stateInput(); |
|---|
| | 223 | this._setInterval(); |
|---|
| | 224 | },_eventKeyDown:function(_14){ |
|---|
| | 225 | if(this.flagConvert){ |
|---|
| | 226 | this._stateInput(); |
|---|
| | 227 | } |
|---|
| | 228 | },_isHiragana:function(c){ |
|---|
| | 229 | return ((c>=12353&&c<=12435)||c==12445||c==12446); |
|---|
| | 230 | },_removeString:function(_16){ |
|---|
| | 231 | if(_16.match(this.ignoreString)){ |
|---|
| | 232 | return _16.replace(this.ignoreString,""); |
|---|
| | 233 | }else{ |
|---|
| | 234 | var i,_18,_19; |
|---|
| | 235 | _18=this.ignoreString.split(""); |
|---|
| | 236 | _19=_16.split(""); |
|---|
| | 237 | for(i=0;i<_18.length;i++){ |
|---|
| | 238 | if(_18[i]==_19[i]){ |
|---|
| | 239 | _19[i]=""; |
|---|
| | 240 | } |
|---|
| | 241 | } |
|---|
| | 242 | return _19.join(""); |
|---|
| | 243 | } |
|---|
| | 244 | },_setInterval:function(){ |
|---|
| | 245 | var _1a=this; |
|---|
| | 246 | this.timer=setInterval(function(){ |
|---|
| | 247 | _1a._checkValue(); |
|---|
| | 248 | },30); |
|---|
| | 249 | },_setKana:function(_1b){ |
|---|
| | 250 | if(!this.flagConvert){ |
|---|
| | 251 | if(_1b){ |
|---|
| | 252 | this.values=_1b; |
|---|
| | 253 | } |
|---|
| | 254 | if(this.active){ |
|---|
| | 255 | this.elKana.value=this._toKatakana(this.baseKana+this.values.join("")); |
|---|
| | 256 | } |
|---|
| | 257 | } |
|---|
| | 258 | },_stateClear:function(){ |
|---|
| | 259 | this.baseKana=""; |
|---|
| | 260 | this.flagConvert=false; |
|---|
| | 261 | this.ignoreString=""; |
|---|
| | 262 | this.input=""; |
|---|
| | 263 | this.values=[]; |
|---|
| | 264 | },_stateInput:function(){ |
|---|
| | 265 | this.baseKana=this.elKana.value; |
|---|
| | 266 | this.flagConvert=false; |
|---|
| | 267 | this.ignoreString=this.elName.value; |
|---|
| | 268 | },_stateConvert:function(){ |
|---|
| | 269 | this.baseKana=this.baseKana+this.values.join(""); |
|---|
| | 270 | this.flagConvert=true; |
|---|
| | 271 | this.values=[]; |
|---|
| | 272 | },_toKatakana:function(src){ |
|---|
| | 273 | if(this.options.katakana){ |
|---|
| | 274 | var c,i,str; |
|---|
| | 275 | str=new String; |
|---|
| | 276 | for(i=0;i<src.length;i++){ |
|---|
| | 277 | c=src.charCodeAt(i); |
|---|
| | 278 | if(this._isHiragana(c)){ |
|---|
| | 279 | str+=String.fromCharCode(c+96); |
|---|
| | 280 | }else{ |
|---|
| | 281 | str+=src.charAt(i); |
|---|
| | 282 | } |
|---|
| | 283 | } |
|---|
| | 284 | return str; |
|---|
| | 285 | }else{ |
|---|
| | 286 | return src; |
|---|
| | 287 | } |
|---|
| | 288 | }}); |
|---|