UNPKG

iuap-design

Version:
204 lines (175 loc) 5.65 kB
u.Year = u.BaseComponent.extend({ DEFAULTS : { }, init:function(){ var self = this; var element = this.element; this.options = u.extend({}, this.DEFAULTS, this.options); this.panelDiv = null; this.input = this.element.querySelector("input"); //u.addClass(this.element,'u-text'); var d = new Date(); this.year = d.getFullYear(); this.defaultYear = this.year; this.startYear = this.year - this.year % 10 - 1; u.on(this.input, 'blur',function(e){ this.setValue(this.input.value); }.bind(this)); // 添加focus事件 this.focusEvent(); // 添加右侧图标click事件 this.clickEvent(); } }) u.Year.fn = u.Year.prototype; u.Year.fn.createPanel = function(){ if(this.panelDiv){ this._fillYear(); return; } var oThis = this; this.panelDiv = u.makeDOM('<div class="u-date-panel" style="padding:0px;margin:0px;"></div>'); this.panelContentDiv = u.makeDOM('<div class="u-date-content"></div>'); this.panelDiv.appendChild(this.panelContentDiv); // this.preBtn = u.makeDOM('<button class="u-date-pre-button u-button flat floating mini" style="display:none;">&lt;</button>'); // this.nextBtn = u.makeDOM('<button class="u-date-next-button u-button flat floating mini" style="display:none;">&gt;</button>'); this.preBtn = u.makeDOM('<button class="u-date-pre-button u-button mini">&lt;</button>'); this.nextBtn = u.makeDOM('<button class="u-date-next-button u-button mini">&gt;</button>'); u.on(this.preBtn, 'click', function(e){ oThis.startYear -= 10; oThis._fillYear(); }); u.on(this.nextBtn, 'click', function(e){ oThis.startYear += 10; oThis._fillYear(); }); this.panelContentDiv.appendChild(this.preBtn); this.panelContentDiv.appendChild(this.nextBtn); this._fillYear(); this.element.parentNode.appendChild(this.panelDiv); } /** *填充年份选择面板 * @private */ u.Year.fn._fillYear = function(type){ var oldPanel,year,template,yearPage,titleDiv,yearDiv, i,cell; oldPanel = this.panelContentDiv.querySelector('.u-date-content-page'); if(oldPanel) this.panelContentDiv.removeChild(oldPanel); template = ['<div class="u-date-content-page">', '<div class="u-date-content-title"></div>', '<div class="u-date-content-panel"></div>', '</div>'].join(""); yearPage = u.makeDOM(template); titleDiv = yearPage.querySelector('.u-date-content-title'); titleDiv.innerHTML = (this.startYear) + '-' + (this.startYear + 11); yearDiv = yearPage.querySelector('.u-date-content-panel'); for(i = 0; i < 12; i++){ cell = u.makeDOM('<div class="u-date-content-year-cell">'+ (this.startYear + i) +'</div>'); new URipple(cell); if (this.startYear + i == this.year){ u.addClass(cell, 'current'); } cell._value = this.startYear + i; yearDiv.appendChild(cell); } u.on(yearDiv, 'click', function(e){ var _y = e.target._value; this.year = _y; this.setValue(_y); this.hide(); u.stopEvent(e); }.bind(this)); this.preBtn.style.display = 'block'; this.nextBtn.style.display = 'block'; this.panelContentDiv.appendChild(yearPage); this.currentPanel = 'year'; }; u.Year.fn.setValue = function(value) { value = value? value: ''; this.value = value; if(value){ this.year = value; }else{ this.year = this.defaultYear; } this.startYear = this.year - this.year % 10 - 1; this.input.value = value; this.trigger('valueChange', {value:value}) } u.Year.fn.focusEvent = function() { var self = this; u.on(this.element,'click', function(e) { self.show(e); if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; } }); } //下拉图标的点击事件 u.Year.fn.clickEvent = function() { var self = this; var caret = this.element.nextSibling u.on(caret,'click',function(e) { self.show(e); if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; } }) } u.Year.fn.show = function(evt) { var oThis = this; this.createPanel(); this.width = this.element.offsetWidth; if(this.width < 300) this.width = 300; this.panelDiv.style.width = 152 + 'px'; u.showPanelByEle({ ele:this.input, panel:this.panelDiv, position:"bottomLeft" }); document.body.onscroll = function(){ u.showPanelByEle({ ele:oThis.input, panel:oThis.panelDiv, position:"bottomLeft" }); } this.panelDiv.style.zIndex = u.getZIndex(); u.addClass(this.panelDiv, 'is-visible'); var callback = function (e) { if (e !== evt && e.target !== this.input && !oThis.clickPanel(e.target)) { u.off(document,'click',callback); // document.removeEventListener('click', callback); this.hide(); } }.bind(this); u.on(document,'click',callback); // document.addEventListener('click', callback); } u.Year.fn.clickPanel = function(dom){ while(dom){ if(dom == this.panelDiv){ return true }else{ dom = dom.parentNode; } } return false; } u.Year.fn.hide = function() { u.removeClass(this.panelDiv, 'is-visible'); this.panelDiv.style.zIndex = -1; } if (u.compMgr) u.compMgr.regComp({ comp: u.Year, compAsString: 'u.Year', css: 'u-year' })