/* * jquery.portlet 1.3.1 * * Copyright (c) 2012~2013 * 咖啡兔 (http://www.kafeitu.me) * * Dual licensed under the GPL (http://www.gnu.org/licenses/gpl.html) * and MIT (http://www.opensource.org/licenses/mit-license.php) licenses. * * Demo: http://www.kafeitu.me/demo/jquery-ui-portlet * Github: https://github.com/henryyan/jquery-ui-portlet */ (function($,undefined){$.widget("ui.portlet",{options:{columns:[],sortable:true,singleView:true,removeItem:null,filterRepeat:false,columnWidth:300,events:{drag:{start:null,stop:null,over:null}}},_create:function(){this.element.addClass("ui-portlet");var _this=this;var _ele=_this.element;var o=_this.options;if(!o.columns||o.columns.length==0){$('
',{width:o.columnWidth}).addClass('ui-portlet-column').appendTo(_ele)}$.each(o.columns,function(ci,c){var $column=$('
',{width:c.width}).addClass('ui-portlet-column').appendTo(_ele);$.each(c.portlets,function(index,portlet){_this._createSinglePortlet(_this,_ele,$column,'last',portlet)})});_this._initEvents();if(o.singleView===true){_this._regSingleView()}_this._sortable(o.sortable)},_createSinglePortlet:function(portlet,_ele,column,positon,pattrs){var o=portlet.options;if(o.filterRepeat===true){if(pattrs.attrs.id){if($('#'+pattrs.attrs.id).length>0){if($.isFunction(o.handleRepeat)){var returnCode=o.handleRepeat.call(_ele,column,pattrs);if(returnCode===false){return}}else{return}}}}if($.isFunction(pattrs.beforeCreate)){var returnCode=pattrs.beforeCreate.call(_ele,positon);if(!returnCode){return}}var item=$('
').addClass('ui-portlet-item ui-widget ui-widget-content ui-helper-clearfix ui-corner-all').data('option',pattrs);if(positon==='last'){item.appendTo(column)}else{if(positon.x==='last'){item.insertAfter($(column).find('.ui-portlet-item:last'))}else{item.insertBefore($(column).find('.ui-portlet-item').eq(positon.x))}}if(pattrs.attrs){item.attr(pattrs.attrs)}var title=$('
',{'class':'ui-portlet-header ui-widget-header ui-corner-all',html:function(){if($.isFunction(pattrs.title)){return pattrs.title}if(pattrs.icon){return""+pattrs.title}else{return pattrs.title}}}).appendTo(item);if(pattrs.icon){title.prepend("")}title.prepend("");title.prepend("");title.prepend("");var ct=$('
',{'class':'ui-portlet-content'}).appendTo(item);if(pattrs.content.style){$(ct).css(pattrs.content.style)}if(pattrs.content.attrs){$.each(pattrs.content.attrs,function(k,v){var attr=ct.attr(k);if(attr){if(k=='style'&&v.substr(v.length-1)!=';'){attr+=';'}if(k=='class'){attr+=' '}attr+=v}ct.attr(k,attr)})}portlet._content.call(_ele,item,pattrs,function(data){portlet._loadScripts(pattrs.scripts,function(){if($.isFunction(pattrs.afterLoadContent)){pattrs.afterLoadContent.call(item,item.find('.ui-portlet-content'))}})});if($.isFunction(pattrs.afterCreated)){pattrs.afterCreated.call(_ele)}return item},_setOption:function(key,value){var self=this.element;var o=this.options;if(this.options[key]){this.options[key]=value}switch(key){case"sortable":this._sortable(value);break;case"add":this._addSingle(value);break;case"remove":$(value,self).find('.ui-portlet-close').trigger('click');break;case"filterRepeat":if(value==null||value==undefined){return o.filterRepeat}else{o.filterRepeat=value;break}}},index:function(a,b){var self=this.element;var indexs={};$('.ui-portlet-column').each(function(i,v){$('.ui-portlet-item',this).each(function(j,v2){var id=$(this).attr('id');indexs[id]={x:i,y:j}})});return indexs},_regSingleView:function(){var _ele=this.element;$(_ele).find('.ui-portlet-header').dblclick(function(){var $item=$(this).parents('.ui-portlet-item');var p=$item.data('option');if($item.hasClass('ui-portlet-single-view')){$(_ele).find('.ui-portlet-item').show();$item.removeClass('ui-portlet-single-view').animate({width:$item.data('width'),height:'auto'}).css({position:'static'}).removeData('width').removeData('height');if(p.singleView){if($.isFunction(p.singleView.recovery)){p.singleView.recovery.call($item,p)}}}else{$(_ele).find('.ui-portlet-item').hide();$item.show().addClass('ui-portlet-single-view').data({width:$item.width(),height:$item.height()}).css({position:'absolute',left:0,top:0});var wh={};if(p.singleView){if(p.singleView.width){if($.isFunction(p.singleView.width)){wh.width=p.singleView.width.call($item,p)}else{wh.width=p.singleView.width}}if(p.singleView.height){if($.isFunction(p.singleView.height)){wh.height=p.singleView.height.call($item,p)}else{wh.height=p.singleView.height}}}else{wh.width=$(_ele).width()+14}$item.animate({width:wh.width,height:wh.height});if(p.singleView&&$.isFunction(p.singleView.enable)){p.singleView.enable.call($item,p)}}})},_loadScripts:function(scripts,callback){if(scripts){$.each(scripts,function(){var head=$('head').remove('#loadScript');$("").attr({src:this,type:'text/javascript',id:'loadScript'}).appendTo(head)})}if($.isFunction(callback)){callback()}},_sortable:function(value){var o=this.options;var st=$(".ui-portlet-column",this.element).sortable({connectWith:".ui-portlet-column",start:function(event,ui){if($.isFunction(o.events.drag.start)){o.events.drag.start.call(ui.item[0],event,ui)}},stop:function(event,ui){if($.isFunction(o.events.drag.stop)){o.events.drag.stop.call(ui.item[0],event,ui)}},over:function(event,ui){if($.isFunction(o.events.drag.over)){o.events.drag.over.call(ui.item[0],event,ui)}}});if(value===true){$(this.element).find('.ui-portlet-header').css('cursor','move');st.sortable('enable');$(".ui-portlet-content",this.element).draggable({start:function(e,ui){return false}})}else{$(this.element).find('.ui-portlet-header').css('cursor','default');st.sortable('disable')}},_addSingle:function(option){var _this=this;var _ele=_this.element;var o=this.options;var addOpt=option;var column;if($('.ui-portlet-column',_ele).eq(addOpt.position.y).length>0){column=$('.ui-portlet-column',_ele).eq(addOpt.position.y)}else{column=$('.ui-portlet-column',_ele).eq(0)}console.log(column);var item=_this._createSinglePortlet(_this,_ele,column,option.position,option.portlet);_this._initEvents(item);if(o.singleView===true){_this._regSingleView()}_this._sortable(o.sortable)},_initEvents:function(element){var _this=this;var _ele=element||this.element;var toggle=$(".ui-portlet-toggle",_ele).click(function(event,type){var ct=$(this).parents(".ui-portlet-item:first").find(".ui-portlet-content");type=type||'toggle';if(type=='toggle'){ct.slideToggle();$(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick")}else if(type=='hide'){ct.slideUp();$(this).removeClass("ui-icon-minusthick").addClass("ui-icon-plusthick")}else if(type=='show'){ct.slideDown();$(this).removeClass("ui-icon-plusthick").addClass("ui-icon-minusthick")}}).dblclick(function(event){event.stopPropagation()});var refresh=$(".ui-portlet-refresh",_ele).click(function(event){_this.refresh.call(_this,event)}).dblclick(function(event){event.stopPropagation()});var close=$(".ui-portlet-close",_ele).click(function(event){_this._destoryItem.call(_this,event)}).dblclick(function(event){event.stopPropagation()});this._hoverable(toggle.parent());this._hoverable(refresh.parent())},_hoverable:function(element){$(element).hover(function(){$(this).addClass('ui-state-hover')},function(){$(this).removeClass('ui-state-hover')})},_destoryItem:function(event){var o=this.options;var item=$(event.target).parents('.ui-portlet-item');var itemOpt=item.data('option');if($.isFunction(itemOpt.beforeRemove)){var returnCode=itemOpt.beforeRemove();if(!returnCode){return}}item.remove();if($.isFunction(o.removeItem)){o.removeItem()}},refresh:function(event){var o=this.options;var portlet=$(event.target).parents('.ui-portlet');var item=$(event.target).parents('.ui-portlet-item');var pio=item.data('option');var ct=item.find('.ui-portlet-content');var pt=item.parents('.ui-portlet');if($.isFunction(pio.beforeRefresh)){pio.beforeRefresh.call(pt,pio)}this._content.call(portlet,item,pio,function(data){if($.isFunction(pio.afterRefresh)){pio.afterRefresh.call(pt,data,pio)}});this._loadScripts(pio.scripts)},_content:function(item,pio,cl){var o=this.options;var that=this;var type=pio.content.type;var content=null;var ct=item.find('.ui-portlet-content');if($.isFunction(pio.content.beforeShow)){pio.content.beforeShow.call(this,pio.content.text)}if(type=='text'){content=pio.content.text;if($.isFunction(content)){content=content(that,item,pio)}if($.isFunction(cl)){cl.call(that,content)}ct.html(content);_callAfterShow(pio.content.text)}else if(type=='ajax'){var dataType=pio.content.dataType||'html';$.ajax({url:pio.content.url,dataType:dataType,beforeSend:function(){$(ct).html('Loading...')},success:function(data,textStatus,jqXHR){if(dataType=='html'){content=data;$(ct).html(data)}else if(dataType=='json'){if($.isFunction(pio.content.formatter)){content=pio.content.formatter(o,pio,data);$(ct).html(content)}}_callAfterShow(content);if($.isFunction(cl)){cl.call(that,data)}},error:function(jqXHR,textStatus,errorThrown){var content="Load Error...";$(ct).html(content);if($.isFunction(pio.content.error)){pio.content.error.call(ct,jqXHR,textStatus,errorThrown)}}})}function _callAfterShow(content){if($.isFunction(pio.content.afterShow)){pio.content.afterShow.call(that,content)}}},toggle:function(itemId,type){var self=this.element;$('#'+itemId+' .ui-portlet-toggle',self).trigger('click',[type||'toggle'])},toggleAll:function(type){var self=this.element;$('.ui-portlet-toggle',self).trigger('click',[type||'toggle'])},destroy:function(){this.element.removeClass("ui-portlet").text("");$.Widget.prototype.destroy.call(this);return this}})})(jQuery);