16 lines
17 KiB
JavaScript
16 lines
17 KiB
JavaScript
/**
|
|
* Copyright 2014 Telerik AD
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
!function(e,define){define(["./kendo.data.min","./kendo.userevents.min","./kendo.mobile.button.min"],e)}(function(){return function(e,t){function n(){return this.nodeType===v.TEXT_NODE&&this.nodeValue.match(U)}function i(e,t){t&&!e[0].querySelector(".km-icon")&&e.prepend('<span class="km-icon km-'+t+'"/>')}function r(e){i(e,S(e,"icon")),i(e,S(e.children(C),"icon"))}function o(e){var t=e.parent(),r=e.add(t.children(m.roleSelector("detailbutton"))),o=t.contents().not(r).not(n);o.length||(e.addClass("km-listview-link").attr(m.attr("role"),"listview-link"),i(e,S(t,"icon")),i(e,S(e,"icon")))}function a(e){if(e[0].querySelector("input[type=checkbox],input[type=radio]")){var t=e.parent();t.contents().not(e).not(function(){return 3==this.nodeType})[0]||(e.addClass("km-listview-label"),e.children("[type=checkbox],[type=radio]").addClass("km-widget km-icon km-check"))}}function s(t,n){e(t).css("transform","translate3d(0px, "+n+"px, 0px)")}var l,d,c,u,h,p,f,g,m=window.kendo,v=window.Node,_=m.mobile,w=_.ui,y=m.data.DataSource,k=w.DataBoundWidget,b=".km-list > li, > li:not(.km-group-container)",x=".km-listview-link, .km-listview-label",C="["+m.attr("icon")+"]",T=e.proxy,S=m.attrValue,D="km-group-title",A="km-state-active",E='<div class="'+D+'"><div class="km-text"></div></div>',I=m.template('<li><div class="'+D+'"><div class="km-text">#= this.headerTemplate(data) #</div></div><ul>#= kendo.render(this.template, data.items)#</ul></li>'),P='<div class="km-listview-wrapper" />',M=m.template('<form class="km-filter-form"><div class="km-filter-wrap"><input type="search" placeholder="#=placeholder#"/><a href="\\#" class="km-filter-reset" title="Clear"><span class="km-icon km-clear"></span><span class="km-text">Clear</span></a></div></form>'),z=".kendoMobileListView",F="styled",L="dataBound",R="dataBinding",H="itemChange",B="click",N="change",O="progress",V="function",U=/^\s+$/,W=/button/,j=m.Class.extend({init:function(e){var t,n,i=e.scroller();i&&(this.options=e.options,this.element=e.element,this.scroller=e.scroller(),this._shouldFixHeaders(),t=this,n=function(){t._cacheHeaders()},e.bind("resize",n),e.bind(F,n),e.bind(L,n),i.bind("scroll",function(e){t._fixHeader(e)}))},_fixHeader:function(t){if(this.fixedHeaders){var n,i,r,o=0,a=this.scroller,s=this.headers,l=t.scrollTop;do{if(n=s[o++],!n){r=e("<div />");break}i=n.offset,r=n.header}while(i+1>l);this.currentHeader!=o&&(a.fixedContainer.html(r.clone()),this.currentHeader=o)}},_shouldFixHeaders:function(){this.fixedHeaders="group"===this.options.type&&this.options.fixedHeaders},_cacheHeaders:function(){if(this._shouldFixHeaders(),this.fixedHeaders){var t=[],n=this.scroller.scrollTop;this.element.find("."+D).each(function(i,r){r=e(r),t.unshift({offset:r.position().top+n,header:r})}),this.headers=t,this._fixHeader({scrollTop:n})}}}),q=function(){return{page:1}},G=m.Class.extend({init:function(e){var t=this,n=e.options,i=e.scroller(),r=n.pullParameters||q;this.listView=e,this.scroller=i,e.bind("_dataSource",function(e){t.setDataSource(e.dataSource)}),i.setOptions({pullToRefresh:!0,pull:function(){t._pulled||(t._pulled=!0,t.dataSource.read(r.call(e,t._first)))},pullTemplate:n.pullTemplate,releaseTemplate:n.releaseTemplate,refreshTemplate:n.refreshTemplate})},setDataSource:function(e){var t=this;this._first=e.view()[0],this.dataSource=e,e.bind("change",function(){t._change()}),e.bind("error",function(){t._change()})},_change:function(){var e,t=this.scroller,n=this.dataSource;this._pulled&&t.pullHandled(),(this._pulled||!this._first)&&(e=n.view(),e[0]&&(this._first=e[0])),this._pulled=!1}}),Y=m.Observable.extend({init:function(e){var t=this;m.Observable.fn.init.call(t),t.buffer=e.buffer,t.height=e.height,t.item=e.item,t.items=[],t.footer=e.footer,t.buffer.bind("reset",function(){t.refresh()})},refresh:function(){for(var e,t,n,i,r=this.buffer,o=this.items,a=!1;o.length;)o.pop().destroy();for(this.offset=r.offset,e=this.item,i=0;r.viewSize>i;i++){if(i===r.total()){a=!0;break}n=e(this.content(this.offset+o.length)),n.below(t),t=n,o.push(n)}this.itemCount=o.length,this.trigger("reset"),this._resize(),a&&this.trigger("endReached")},totalHeight:function(){if(!this.items[0])return 0;var e=this,t=e.items,n=t[0].top,i=t[t.length-1].bottom,r=(i-n)/e.itemCount,o=e.buffer.length-e.offset-e.itemCount;return(this.footer?this.footer.height:0)+i+o*r},batchUpdate:function(e){var t,n,i=this.height(),r=this.items,o=this.offset;if(r[0]){if(this.lastDirection)for(;r[r.length-1].bottom>e+2*i&&0!==this.offset;)this.offset--,t=r.pop(),t.update(this.content(this.offset)),t.above(r[0]),r.unshift(t);else for(;e-i>r[0].top;){if(n=this.offset+this.itemCount,n===this.buffer.total()){this.trigger("endReached");break}if(n===this.buffer.length)break;t=r.shift(),t.update(this.content(this.offset+this.itemCount)),t.below(r[r.length-1]),r.push(t),this.offset++}o!==this.offset&&this._resize()}},update:function(e){var t,n,i,r,o=this,a=this.items,s=this.height(),l=this.itemCount,d=s/2,c=(this.lastTop||0)>e,u=e-d,h=e+s+d;a[0]&&(this.lastTop=e,this.lastDirection=c,c?a[0].top>u&&a[a.length-1].bottom>h+d&&this.offset>0&&(this.offset--,t=a.pop(),n=a[0],t.update(this.content(this.offset)),a.unshift(t),t.above(n),o._resize()):h>a[a.length-1].bottom&&u-d>a[0].top&&(r=this.offset+l,r===this.buffer.total()?this.trigger("endReached"):r!==this.buffer.length&&(t=a.shift(),i=a[a.length-1],a.push(t),t.update(this.content(this.offset+this.itemCount)),o.offset++,t.below(i),o._resize())))},content:function(e){return this.buffer.at(e)},destroy:function(){this.unbind()},_resize:function(){var e=this.items,t=0,n=0,i=e[0],r=e[e.length-1];i&&(t=i.top,n=r.bottom),this.trigger("resize",{top:t,bottom:n}),this.footer&&this.footer.below(r)}});m.mobile.ui.VirtualList=Y,l=m.Class.extend({init:function(t,n){var i=t.append([n])[0],r=i.offsetHeight;e.extend(this,{top:0,element:i,listView:t,height:r,bottom:r})},update:function(e){this.element=this.listView.setDataItem(this.element,e)},above:function(e){e&&(this.height=this.element.offsetHeight,this.top=e.top-this.height,this.bottom=e.top,s(this.element,this.top))},below:function(e){e&&(this.height=this.element.offsetHeight,this.top=e.bottom,this.bottom=this.top+this.height,s(this.element,this.top))},destroy:function(){m.destroy(this.element),e(this.element).remove()}}),d='<div><span class="km-icon"></span><span class="km-loading-left"></span><span class="km-loading-right"></span></div>',c=m.Class.extend({init:function(t){this.element=e('<li class="km-load-more km-scroller-refresh" style="display: none"></li>').appendTo(t.element),this._loadIcon=e(d).appendTo(this.element)},enable:function(){this.element.show(),this.height=this.element.outerHeight(!0)},disable:function(){this.element.hide(),this.height=0},below:function(e){e&&(this.top=e.bottom,this.bottom=this.height+this.top,s(this.element,this.top))}}),u=c.extend({init:function(t,n){this._loadIcon=e(d).hide(),this._loadButton=e('<a class="km-load">'+t.options.loadMoreText+"</a>").hide(),this.element=e('<li class="km-load-more" style="display: none"></li>').append(this._loadIcon).append(this._loadButton).appendTo(t.element);var i=this;this._loadButton.kendoMobileButton().data("kendoMobileButton").bind("click",function(){i._hideShowButton(),n.next()}),n.bind("resize",function(){i._showLoadButton()}),this.height=this.element.outerHeight(!0),this.disable()},_hideShowButton:function(){this._loadButton.hide(),this.element.addClass("km-scroller-refresh"),this._loadIcon.css("display","block")},_showLoadButton:function(){this._loadButton.show(),this.element.removeClass("km-scroller-refresh"),this._loadIcon.hide()}}),h=m.Class.extend({init:function(e){var t=this;this.chromeHeight=e.wrapper.children().not(e.element).outerHeight()||0,this.listView=e,this.scroller=e.scroller(),this.options=e.options,e.bind("_dataSource",function(e){t.setDataSource(e.dataSource,e.empty)}),e.bind("resize",function(){t.list.items.length&&(t.scroller.reset(),t.buffer.range(0),t.list.refresh())}),this.scroller.makeVirtual(),this.scroller.bind("scroll",function(e){t.list.update(e.scrollTop)}),this.scroller.bind("scrollEnd",function(e){t.list.batchUpdate(e.scrollTop)})},destroy:function(){this.list.unbind(),this.buffer.unbind()},setDataSource:function(t,n){var i,r,o,a,s=this,d=this.options,h=this.listView,p=h.scroller(),f=d.loadMore;if(this.dataSource=t,i=t.pageSize()||d.virtualViewSize,!i&&!n)throw Error("the DataSource does not have page size configured. Page Size setting is mandatory for the mobile listview virtual scrolling to work as expected.");this.buffer&&this.buffer.destroy(),r=new m.data.Buffer(t,Math.floor(i/2),f),o=f?new u(h,r):new c(h),this.list&&this.list.destroy(),a=new Y({buffer:r,footer:o,item:function(e){return new l(h,e)},height:function(){return p.height()}}),a.bind("resize",function(){s.updateScrollerSize()}),a.bind("reset",function(){s.footer.enable()}),a.bind("endReached",function(){o.disable(),s.updateScrollerSize()}),r.bind("expand",function(){a.lastDirection=!1,a.batchUpdate(p.scrollTop)}),e.extend(this,{buffer:r,scroller:p,list:a,footer:o})},updateScrollerSize:function(){this.scroller.virtualSize(0,this.list.totalHeight()+this.chromeHeight)},refresh:function(){this.list.refresh()},reset:function(){this.buffer.range(0),this.list.refresh()}}),p=m.Class.extend({init:function(e){var t,n=this;this.listView=e,this.options=e.options,t=this,this._refreshHandler=function(e){t.refresh(e)},this._progressHandler=function(){e.showLoading()},e.bind("_dataSource",function(e){n.setDataSource(e.dataSource)})},destroy:function(){this._unbindDataSource()},reset:function(){},refresh:function(e){var n,i,r,o,a,s,l,d=e&&e.action,c=e&&e.items,u=this.listView,h=this.dataSource,p=this.options.appendOnRefresh,f=h.view(),g=h.group(),m=g&&g[0];return"itemchange"===d?(n=u.findByDataItem(c)[0],n&&u.setDataItem(n,c[0]),t):(a="add"===d&&!m||p&&!u._filter,s="remove"===d&&!m,a?i=[]:s&&(i=u.findByDataItem(c)),u.trigger(R,{action:d||"rebind",items:c,removedItems:i,index:e&&e.index})?(this._shouldShowLoading()&&u.hideLoading(),t):("add"!==d||m?"remove"!==d||m?m?u.replaceGrouped(f):p&&!u._filter?(r=u.prepend(f),o=f):u.replace(f):(r=[],u.remove(c)):(l=f.indexOf(c[0]),l>-1&&(r=u.insertAt(c,l),o=c)),this._shouldShowLoading()&&u.hideLoading(),u.trigger(L,{ns:w,addedItems:r,addedDataItems:o}),t))},setDataSource:function(e){this.dataSource&&this._unbindDataSource(),this.dataSource=e,e.bind(N,this._refreshHandler),this._shouldShowLoading()&&this.dataSource.bind(O,this._progressHandler)},_unbindDataSource:function(){this.dataSource.unbind(N,this._refreshHandler).unbind(O,this._progressHandler)},_shouldShowLoading:function(){var e=this.options;return!e.pullToRefresh&&!e.loadMore&&!e.endlessScroll}}),f=m.Class.extend({init:function(e){var t=this,n=e.options.filterable,i="change paste";this.listView=e,this.options=n,e.element.before(M({placeholder:n.placeholder||"Search..."})),n.autoFilter!==!1&&(i+=" keyup"),this.element=e.wrapper.find(".km-search-form"),this.searchInput=e.wrapper.find("input[type=search]").closest("form").on("submit"+z,function(e){e.preventDefault()}).end().on("focus"+z,function(){t._oldFilter=t.searchInput.val()}).on(i.split(" ").join(z+" ")+z,T(this._filterChange,this)),this.clearButton=e.wrapper.find(".km-filter-reset").on(B,T(this,"_clearFilter")).hide()},_search:function(e){this._filter=!0,this.clearButton[e?"show":"hide"](),this.listView.dataSource.filter(e)},_filterChange:function(e){var t=this;"paste"==e.type&&this.options.autoFilter!==!1?setTimeout(function(){t._applyFilter()},1):this._applyFilter()},_applyFilter:function(){var e=this.options,t=this.searchInput.val(),n=t.length?{field:e.field,operator:e.operator||"startsWith",ignoreCase:e.ignoreCase,value:t}:null;t!==this._oldFilter&&(this._oldFilter=t,this._search(n))},_clearFilter:function(e){this.searchInput.val(""),this._search(null),e.preventDefault()}}),g=k.extend({init:function(e,t){var n=this;k.fn.init.call(this,e,t),e=this.element,t=this.options,t.scrollTreshold&&(t.scrollThreshold=t.scrollTreshold),e.on("down",x,"_highlight").on("move up cancel",x,"_dim"),this._userEvents=new m.UserEvents(e,{filter:b,allowSelection:!0,tap:function(e){n._click(e)}}),e.css("-ms-touch-action","auto"),e.wrap(P),this.wrapper=this.element.parent(),this._headerFixer=new j(this),this._itemsCache={},this._templates(),this.virtual=t.endlessScroll||t.loadMore,this._style(),this.options.filterable&&(this._filter=new f(this)),this._itemBinder=this.virtual?new h(this):new p(this),this.options.pullToRefresh&&(this._pullToRefreshHandler=new G(this)),this.setDataSource(t.dataSource),this._enhanceItems(this.items()),m.notify(this,w)},events:[B,R,L,H],options:{name:"ListView",style:"",type:"flat",autoBind:!0,fixedHeaders:!1,template:"#:data#",headerTemplate:'<span class="km-text">#:value#</span>',appendOnRefresh:!1,loadMore:!1,loadMoreText:"Press to load more",endlessScroll:!1,scrollThreshold:30,pullToRefresh:!1,pullTemplate:"Pull to refresh",releaseTemplate:"Release to refresh",refreshTemplate:"Refreshing",pullOffset:140,filterable:!1,virtualViewSize:null},refresh:function(){this._itemBinder.refresh()},reset:function(){this._itemBinder.reset()},setDataSource:function(e){var t=!e;this.dataSource=y.create(e),this.trigger("_dataSource",{dataSource:this.dataSource,empty:t}),this.options.autoBind&&!t&&(this.items().remove(),this.dataSource.fetch())},destroy:function(){k.fn.destroy.call(this),m.destroy(this.element),this._userEvents.destroy(),this._itemBinder&&this._itemBinder.destroy(),this.element.unwrap(),delete this.element,delete this.wrapper,delete this._userEvents},items:function(){return"group"===this.options.type?this.element.find(".km-list").children():this.element.children().not(".km-load-more")},scroller:function(){return this._scrollerInstance||(this._scrollerInstance=this.element.closest(".km-scroll-wrapper").data("kendoMobileScroller")),this._scrollerInstance},showLoading:function(){var e=this.view();e&&e.loader&&e.loader.show()},hideLoading:function(){var e=this.view();e&&e.loader&&e.loader.hide()},insertAt:function(e,t){var n=this;return n._renderItems(e,function(i){0===t?n.element.prepend(i):-1===t?n.element.append(i):n.items().eq(t-1).after(i),n.angular("compile",function(){return{elements:i,data:e.map(function(e){return{dataItem:e}})}})})},append:function(e){return this.insertAt(e,-1)},prepend:function(e){return this.insertAt(e,0)},replace:function(e){return this.options.type="flat",this._angularItems("cleanup"),this.element.empty(),this._style(),this.insertAt(e,0)},replaceGrouped:function(t){this.options.type="group",this._angularItems("cleanup"),this.element.empty();var n=e(m.render(this.groupTemplate,t));this._enhanceItems(n.children("ul").children("li")),this.element.append(n),_.init(n),this._angularItems("compile"),this._style()},remove:function(e){var t=this.findByDataItem(e);this.angular("cleanup",function(){return{elements:t}}),m.destroy(t),t.remove()},findByDataItem:function(e){var t,n,i=[];for(t=0,n=e.length;n>t;t++)i[t]="[data-"+m.ns+"uid="+e[t].uid+"]";return this.element.find(i.join(","))},setDataItem:function(t,n){var i=this,r=function(r){var o=e(r[0]);m.destroy(t),e(t).replaceWith(o),i.trigger(H,{item:o,data:n,ns:w})};return this._renderItems([n],r)[0]},_renderItems:function(t,n){var i=e(m.render(this.template,t));return n(i),_.init(i),this._enhanceItems(i),i},_dim:function(e){this._toggle(e,!1)},_highlight:function(e){this._toggle(e,!0)},_toggle:function(t,n){if(!(t.which>1)){var i=e(t.currentTarget),r=i.parent(),o=S(i,"role")||"",a=!o.match(W),s=t.isDefaultPrevented();a&&r.toggleClass(A,n&&!s)}},_templates:function(){var e=this.options.template,t=this.options.headerTemplate,n=' data-uid="#=arguments[0].uid || ""#"',i={},r={};typeof e===V&&(i.template=e,e="#=this.template(data)#"),this.template=T(m.template("<li"+n+">"+e+"</li>"),i),r.template=this.template,typeof t===V&&(r._headerTemplate=t,t="#=this._headerTemplate(data)#"),r.headerTemplate=m.template(t),this.groupTemplate=T(I,r)},_click:function(t){if(!(t.event.which>1||t.event.isDefaultPrevented())){var n,i=t.target,r=e(t.event.target),o=r.closest(m.roleSelector("button","detailbutton","backbutton")),a=m.widgetInstance(o,w),s=i.attr(m.attr("uid"));s&&(n=this.dataSource.getByUid(s)),this.trigger(B,{target:r,item:i,dataItem:n,button:a})&&t.preventDefault()}},_styleGroups:function(){var t=this.element.children();t.children("ul").addClass("km-list"),t.each(function(){var t=e(this),n=t.contents().first();t.addClass("km-group-container"),n.is("ul")||n.is("div."+D)||n.wrap(E)})},_style:function(){var e=this.options,t="group"===e.type,n=this.element,i="inset"===e.style;n.addClass("km-listview").toggleClass("km-list",!t).toggleClass("km-virtual-list",this.virtual).toggleClass("km-listinset",!t&&i).toggleClass("km-listgroup",t&&!i).toggleClass("km-listgroupinset",t&&i),n.parents(".km-listview")[0]||n.closest(".km-content").toggleClass("km-insetcontent",i),t&&this._styleGroups(),this.trigger(F)},_enhanceItems:function(t){t.each(function(){var t,n=e(this),i=!1;n.children().each(function(){t=e(this),t.is("a")?(o(t),i=!0):t.is("label")&&(a(t),i=!0)}),i||r(n)})}}),w.plugin(g)}(window.kendo.jQuery),window.kendo},"function"==typeof define&&define.amd?define:function(e,t){t()}); |