PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/mb.site/mb.components/mb.extruder/demo/inc/mbExtruder.js

http://mbideasproject.googlecode.com/
JavaScript | 402 lines | 344 code | 39 blank | 19 comment | 56 complexity | ca0936482a30c5e1f92bce7b357448a8 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*******************************************************************************
  2. jquery.mb.components
  3. Copyright (c) 2001-2010. Matteo Bicocchi (Pupunzi); Open lab srl, Firenze - Italy
  4. email: mbicocchi@open-lab.com
  5. site: http://pupunzi.com
  6. Licences: MIT, GPL
  7. http://www.opensource.org/licenses/mit-license.php
  8. http://www.gnu.org/licenses/gpl.html
  9. ******************************************************************************/
  10. /*
  11. * Name:jquery.mb.extruder
  12. * Version: 2.1
  13. * dependencies: jquery.metadata.js, jquery.mb.flipText.js, jquery.hoverintent.js
  14. */
  15. (function($) {
  16. document.extruder=new Object();
  17. document.extruder.left = 0;
  18. document.extruder.top = 0;
  19. document.extruder.bottom = 0;
  20. document.extruder.right = 0;
  21. document.extruder.idx=0;
  22. var isIE=$.browser.msie;
  23. $.mbExtruder= {
  24. author:"Matteo Bicocchi",
  25. version:"2.1",
  26. defaults:{
  27. width:350,
  28. positionFixed:true,
  29. sensibility:800,
  30. position:"top",
  31. accordionPanels:true,
  32. top:"auto",
  33. extruderOpacity:1,
  34. flapMargin:35,
  35. textOrientation:"bt", // or "tb" (top-bottom or bottom-top)
  36. onExtOpen:function(){},
  37. onExtContentLoad:function(){},
  38. onExtClose:function(){},
  39. hidePanelsOnClose:true,
  40. autoCloseTime:0,
  41. slideTimer:300
  42. },
  43. buildMbExtruder: function(options){
  44. return this.each (function (){
  45. this.options = {};
  46. $.extend (this.options, $.mbExtruder.defaults);
  47. $.extend (this.options, options);
  48. this.idx=document.extruder.idx;
  49. document.extruder.idx++;
  50. var extruder,extruderContent,wrapper,extruderStyle,wrapperStyle,txt,timer;
  51. extruder= $(this);
  52. extruderContent=extruder.html();
  53. extruder.css("zIndex",100);
  54. var isVertical = this.options.position=="left" || this.options.position=="right";
  55. var extW= isVertical?1: this.options.width;
  56. var c= $("<div/>").addClass("content").css({overflow:"hidden", width:extW});
  57. c.append(extruderContent);
  58. extruder.html(c);
  59. var position=this.options.positionFixed?"fixed":"absolute";
  60. extruder.addClass("extruder");
  61. extruder.addClass(this.options.position);
  62. var isHorizontal = this.options.position=="top" || this.options.position=="bottom";
  63. extruderStyle=
  64. this.options.position=="top"?
  65. {position:position,top:0,left:"50%",marginLeft:-this.options.width/2,width:this.options.width}:
  66. this.options.position=="bottom"?
  67. {position:position,bottom:0,left:"50%",marginLeft:-this.options.width/2,width:this.options.width}:
  68. this.options.position=="left"?
  69. {position:position,top:0,left:0,width:1}:
  70. {position:position,top:0,right:0,width:1};
  71. extruder.css(extruderStyle);
  72. if(!isIE) extruder.css({opacity:this.options.extruderOpacity});
  73. extruder.wrapInner("<div class='ext_wrapper'></div>");
  74. wrapper= extruder.find(".ext_wrapper");
  75. wrapperStyle={position:"absolute", width:isVertical?1:this.options.width};
  76. wrapper.css(wrapperStyle);
  77. if (isHorizontal){
  78. this.options.position=="top"?document.extruder.top++:document.extruder.bottom++;
  79. if (document.extruder.top>1 || document.extruder.bottom>1){
  80. alert("more than 1 mb.extruder on top or bottom is not supported jet... hope soon!");
  81. return;
  82. }
  83. }
  84. if ($.metadata){
  85. $.metadata.setType("class");
  86. if (extruder.metadata().title) extruder.attr("extTitle",extruder.metadata().title);
  87. if (extruder.metadata().url) extruder.attr("extUrl",extruder.metadata().url);
  88. if (extruder.metadata().data) extruder.attr("extData",extruder.metadata().data);
  89. }
  90. var flapFooter=$("<div class='footer'/>");
  91. var flap=$("<div class='flap'><span class='flapLabel'/></div>");
  92. if (document.extruder.bottom){
  93. wrapper.prepend(flapFooter);
  94. wrapper.prepend(flap);
  95. }else{
  96. wrapper.append(flapFooter);
  97. wrapper.append(flap);
  98. }
  99. txt=extruder.attr("extTitle")?extruder.attr("extTitle"): "";
  100. var flapLabel = extruder.find(".flapLabel");
  101. flapLabel.text(txt);
  102. if(isVertical){
  103. flapLabel.html(txt).css({whiteSpace:"noWrap"});//,height:this.options.flapDim
  104. var orientation= this.options.textOrientation == "tb";
  105. var labelH=extruder.find('.flapLabel').getFlipTextDim()[1];
  106. extruder.find('.flapLabel').mbFlipText(orientation);
  107. }else{
  108. flapLabel.html(txt).css({whiteSpace:"noWrap"});
  109. }
  110. if (extruder.attr("extUrl")){
  111. extruder.setMbExtruderContent({
  112. url:extruder.attr("extUrl"),
  113. data:extruder.attr("extData"),
  114. callback: function(){
  115. if (extruder.get(0).options.onExtContentLoad) extruder.get(0).options.onExtContentLoad();
  116. }
  117. })
  118. }else{
  119. var container=$("<div>").addClass("text").css({width:extruder.get(0).options.width-20, height:extruder.height()-20, overflowY:"auto"});
  120. c.wrapInner(container);
  121. extruder.setExtruderVoicesAction();
  122. }
  123. flap.bind("click",function(){
  124. if (!extruder.attr("open")){
  125. extruder.openMbExtruder();
  126. }else{
  127. extruder.closeMbExtruder();
  128. }
  129. });
  130. c.bind("mouseleave", function(){
  131. $(document).one("click.extruder"+extruder.get(0).idx,function(){extruder.closeMbExtruder();});
  132. timer=setTimeout(function(){
  133. if(extruder.get(0).options.autoCloseTime > 0){
  134. extruder.closeMbExtruder();
  135. }
  136. },extruder.get(0).options.autoCloseTime);
  137. }).bind("mouseenter", function(){clearTimeout(timer); $(document).unbind("click.extruder"+extruder.get(0).idx);});
  138. if (isVertical){
  139. c.css({ height:"100%"});
  140. if(this.options.top=="auto") {
  141. flap.css({top:100+(this.options.position=="left"?document.extruder.left:document.extruder.right)});
  142. this.options.position=="left"?document.extruder.left+=labelH+this.options.flapMargin:document.extruder.right+= labelH+this.options.flapMargin;
  143. }else{
  144. flap.css({top:this.options.top});
  145. }
  146. var clicDiv=$("<div/>").css({position:"absolute",top:0,left:0,width:"100%",height:"100%",background:"transparent"});
  147. flap.append(clicDiv);
  148. }
  149. });
  150. },
  151. setMbExtruderContent: function(options){
  152. this.options = {
  153. url:false,
  154. data:"",
  155. callback:function(){}
  156. };
  157. $.extend (this.options, options);
  158. if (!this.options.url || this.options.url.length==0){
  159. alert("internal error: no URL to call");
  160. return;
  161. }
  162. var url=this.options.url;
  163. var data=this.options.data;
  164. var where=$(this), voice;
  165. var cb= this.options.callback;
  166. var container=$("<div>").addClass("container");
  167. if (!($.browser.msie && $.browser.version<=7))
  168. container.css({width:$(this).get(0).options.width});
  169. where.find(".content").wrapInner(container);
  170. $.ajax({
  171. type: "POST",
  172. url: url,
  173. data: data,
  174. success: function(html){
  175. where.find(".container").append(html);
  176. voice=where.find(".voice");
  177. voice.hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");});
  178. where.setExtruderVoicesAction();
  179. if (cb) {
  180. setTimeout(function(){cb();},100);
  181. }
  182. }
  183. });
  184. },
  185. openMbExtruder:function(c){
  186. var extruder= $(this);
  187. extruder.attr("open",true);
  188. $(document).unbind("click.extruder"+extruder.get(0).idx);
  189. var opt= extruder.get(0).options;
  190. extruder.addClass("open");
  191. if(!isIE) extruder.css("opacity",1);
  192. var position= opt.position;
  193. extruder.mb_bringToFront();
  194. if (position=="top" || position=="bottom"){
  195. extruder.find('.content').slideDown( opt.slideTimer);
  196. if(opt.onExtOpen) opt.onExtOpen();
  197. }else{
  198. if(!isIE) $(this).css("opacity",1);
  199. extruder.find('.ext_wrapper').css({width:""});
  200. extruder.find('.content').css({overflowX:"hidden", display:"block"});
  201. extruder.find('.content').animate({ width: opt.width}, opt.slideTimer);
  202. if(opt.onExtOpen) opt.onExtOpen();
  203. }
  204. if (c) {
  205. setTimeout(function(){
  206. $(document).one("click.extruder"+extruder.get(0).idx,function(){extruder.closeMbExtruder();});
  207. },100);
  208. }
  209. },
  210. closeMbExtruder:function(){
  211. var extruder= $(this);
  212. extruder.removeAttr("open");
  213. var opt= extruder.get(0).options;
  214. extruder.removeClass("open");
  215. $(document).unbind("click.extruder"+extruder.get(0).idx);
  216. if(!isIE) extruder.css("opacity",opt.extruderOpacity);
  217. if(opt.hidePanelsOnClose) extruder.hidePanelsOnClose();
  218. if (opt.position=="top" || opt.position=="bottom"){
  219. extruder.find('.content').slideUp(opt.slideTimer);
  220. if(opt.onExtClose) opt.onExtClose();
  221. }else if (opt.position=="left" || opt.position=="right"){
  222. extruder.find('.content').css({overflow:"hidden"});
  223. extruder.find('.content').animate({ width: 1 }, opt.slideTimer,function(){
  224. extruder.find('.ext_wrapper').css({width:1});
  225. extruder.find('.content').css({overflow:"hidden",display:"none"});
  226. if(opt.onExtClose) opt.onExtClose();
  227. });
  228. }
  229. }
  230. };
  231. jQuery.fn.mb_bringToFront= function(){
  232. var zi=10;
  233. $('*').each(function() {
  234. if($(this).css("position")=="absolute" ||$(this).css("position")=="fixed"){
  235. var cur = parseInt($(this).css('zIndex'));
  236. zi = cur > zi ? parseInt($(this).css('zIndex')) : zi;
  237. }
  238. });
  239. $(this).css('zIndex',zi+=1);
  240. return zi;
  241. };
  242. /*
  243. * EXTRUDER CONTENT
  244. */
  245. $.fn.setExtruderVoicesAction=function(){
  246. var extruder=$(this);
  247. var opt=extruder.get(0).options;
  248. var voices= $(this).find(".voice");
  249. voices.each(function(){
  250. var voice=$(this);
  251. if ($.metadata){
  252. $.metadata.setType("class");
  253. if (voice.metadata().panel) voice.attr("panel",voice.metadata().panel);
  254. if (voice.metadata().data) voice.attr("data",voice.metadata().data);
  255. if (voice.metadata().disabled) voice.attr("setDisabled", voice.metadata().disabled);
  256. }
  257. if (voice.attr("setDisabled"))
  258. voice.disableExtruderVoice();
  259. if (voice.attr("panel") && voice.attr("panel")!="false"){
  260. voice.append("<span class='settingsBtn'/>");
  261. voice.find(".settingsBtn").css({opacity:.5});
  262. voice.find(".settingsBtn").hover(
  263. function(){
  264. $(this).css({opacity:1});
  265. },
  266. function(){
  267. $(this).not(".sel").css({opacity:.5});
  268. }).click(function(){
  269. if ($(this).parents().hasClass("sel")){
  270. if(opt.accordionPanels)
  271. extruder.hidePanelsOnClose();
  272. else
  273. $(this).closePanel();
  274. return;
  275. }
  276. if(opt.accordionPanels){
  277. extruder.find(".optionsPanel").slideUp(400,function(){$(this).remove();});
  278. voices.removeClass("sel");
  279. voices.find(".settingsBtn").removeClass("sel").css({opacity:.5});
  280. }
  281. var content=$("<div class='optionsPanel'></div>");
  282. voice.after(content);
  283. $.ajax({
  284. type: "POST",
  285. url: voice.attr("panel"),
  286. data: voice.attr("data"),
  287. success: function(html){
  288. var c= $(html);
  289. content.html(c);
  290. content.children().not(".text")
  291. .addClass("panelVoice")
  292. .click(function(){
  293. extruder.closeMbExtruder();
  294. });
  295. content.slideDown(400);
  296. }
  297. });
  298. voice.addClass("sel");
  299. voice.find(".settingsBtn").addClass("sel").css({opacity:1});
  300. });
  301. }
  302. if (voice.find("a").length==0 && voice.attr("panel")){
  303. voice.find(".label").not(".disabled").css("cursor","pointer").click(function(){
  304. voice.find(".settingsBtn").click();
  305. });
  306. }
  307. if ((!voice.attr("panel") ||voice.attr("panel")=="false" ) && (!voice.attr("setDisabled") || voice.attr("setDisabled")!="true")){
  308. voice.find(".label").click(function(){
  309. extruder.hidePanelsOnClose();
  310. extruder.closeMbExtruder();
  311. });
  312. }
  313. });
  314. };
  315. $.fn.disableExtruderVoice=function(){
  316. var voice=$(this);
  317. var label = voice.find(".label");
  318. voice.removeClass("sel");
  319. voice.next(".optionsPanel").slideUp(400,function(){$(this).remove();});
  320. voice.attr("setDisabled",true);
  321. label.css("opacity",.4);
  322. voice.hover(function(){$(this).removeClass("hover");},function(){$(this).removeClass("hover");});
  323. label.addClass("disabled").css("cursor","default");
  324. voice.find(".settingsBtn").hide();
  325. voice.bind("click",function(event){
  326. event.stopPropagation();
  327. return false;
  328. });
  329. };
  330. $.fn.enableExtruderVoice=function(){
  331. var voice=$(this);
  332. voice.attr("setDisabled",false);
  333. voice.find(".label").css("opacity",1);
  334. voice.find(".label").removeClass("disabled").css("cursor","pointer");
  335. voice.unbind("click");
  336. voice.find(".settingsBtn").show();
  337. };
  338. $.fn.hidePanelsOnClose=function(){
  339. var voices= $(this).find(".voice");
  340. $(this).find(".optionsPanel").slideUp(400,function(){$(this).remove();});
  341. voices.removeClass("sel");
  342. voices.find(".settingsBtn").removeClass("sel").css("opacity",.5);
  343. };
  344. $.fn.openPanel=function(){
  345. var voice=$(this).hasClass("voice") ? $(this) : $(this).find(".voice");
  346. voice.each(function(){
  347. if($(this).hasClass("sel")) return;
  348. $(this).find(".settingsBtn").click();
  349. })
  350. };
  351. $.fn.closePanel=function(){
  352. var voice=$(this).hasClass("voice") ? $(this) : $(this).parent(".voice");
  353. voice.next(".optionsPanel").slideUp(400,function(){$(this).remove();});
  354. voice.removeClass("sel");
  355. $(this).removeClass("sel").css("opacity",.5);
  356. };
  357. // Public methods
  358. $.fn.buildMbExtruder=$.mbExtruder.buildMbExtruder;
  359. $.fn.setMbExtruderContent=$.mbExtruder.setMbExtruderContent;
  360. $.fn.closeMbExtruder=$.mbExtruder.closeMbExtruder;
  361. $.fn.openMbExtruder=$.mbExtruder.openMbExtruder;
  362. })(jQuery);