12345678910111213 |
- /*
- * jquery.portlet 1.1.3
- *
- * 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.
- *
- * Detail: http://www.kafeitu.me/jquery-ui-portlet.html
- * Demo: http://www.kafeitu.me/demo/jquery-ui-portlet
- */
- (function($){$.widget("ui.portlet",{options:{columns:{},sortable:true,singleView:true,removeItem:null},_create:function(){this.element.addClass("ui-portlet");var _this=this;var _ele=_this.element;var o=_this.options;$.each(o.columns,function(ci,c){var $column=$('<div/>',{width:c.width}).addClass('ui-portlet-column').appendTo(_ele);$.each(c.portlets,function(pi,p){var item=$('<div/>').addClass('ui-portlet-item ui-widget ui-widget-content ui-helper-clearfix ui-corner-all').data('option',p).appendTo($column);if(p.attrs){item.attr(p.attrs)}var title=$('<div/>',{'class':'ui-portlet-header ui-widget-header ui-corner-all',html:function(){if($.isFunction(p.title)){return p.title}return"<span class='"+p.icon+"'></span>"+p.title}}).appendTo(item);if(p.icon){title.prepend("<span class='ui-portlet-header-icon ui-icon "+p.icon+"'></span>")}title.prepend("<a href='#' class='ui-corner-all ui-portlet-event'><span class='ui-icon ui-icon-refresh ui-portlet-refresh'></span></a>");title.prepend("<a href='#' class='ui-corner-all ui-portlet-event'><span class='ui-icon ui-icon-minusthick ui-portlet-toggle'></span></a>");title.prepend("<a href='#' class='ui-corner-all ui-portlet-event'><span class='ui-icon ui-icon-closethick ui-portlet-close'></span></a>");var ct=$('<div/>',{'class':'ui-portlet-content'}).appendTo(item);if(p.content.style){$(ct).css(p.content.style)}if(p.content.attrs){$.each(p.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)})}_this._content.call(_ele,item,p,function(data){_this._loadScripts(p.scripts)})})});_this._initEvents();if(o.singleView===true){_this._regSingleView()}_this._sortable(o.sortable)},_setOption:function(key,value){if(this.options[key]){this.options[key]=value}switch(key){case"sortable":this._sortable(value);break}},_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:$item.data('height')}).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){if(scripts){$.each(scripts,function(){var head=$('head').remove('#loadScript');$("<script>"+"</script>").attr({src:this,type:'text/javascript',id:'loadScript'}).appendTo(head)})}},_sortable:function(value){var st=$(".ui-portlet-column",this.element).sortable({connectWith:".ui-portlet-column"}).disableSelection();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')}},_initEvents:function(){var _this=this;var toggle=$(".ui-portlet-toggle",this.element).click(function(event){$(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");$(this).parents(".ui-portlet-item:first").find(".ui-portlet-content").slideToggle()}).dblclick(function(event){event.stopPropagation()});var refresh=$(".ui-portlet-refresh",this.element).click(function(event){_this.refresh.call(_this,event)}).dblclick(function(event){event.stopPropagation()});var close=$(".ui-portlet-close",this.element).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');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="<span style='padding:0.2em' class='ui-state-error ui-corner-all'>Load Error...</span>";$(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)}}},_destroy:function(){this.element.removeClass("ui-portlet").text("");$.Widget.prototype.destroy.call(this);return this}})})(jQuery);
|