2 * (c) 2005-2009 Richard Cowin (http://openrico.org)
3 * (c) 2005-2009 Matt Brown (http://dowdybrown.com)
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6 * file except in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the
11 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12 * either express or implied. See the License for the specific language governing permissions
13 * and limitations under the License.
16 Rico.applyShadow = function(elem,shadowFlag) {
17 if (typeof shadowFlag=='undefined') shadowFlag=true;
18 if (shadowFlag) Rico.addClass(elem,'ricoShadow');
22 Rico.Popup = function(containerDiv,options) {
23 this.initialize(containerDiv,options);
26 Rico.Popup.prototype = {
28 * @class Class to manage pop-up div windows.
30 * @param options object may contain any of the following:<dl>
31 * <dt>hideOnEscape</dt><dd> hide popup when escape key is pressed? default=true</dd>
32 * <dt>hideOnClick </dt><dd> hide popup when mouse button is clicked? default=true</dd>
33 * <dt>ignoreClicks</dt><dd> if true, mouse clicks within the popup are not allowed to bubble up to parent elements</dd>
34 * <dt>position </dt><dd> defaults to absolute, use "auto" to auto-detect</dd>
35 * <dt>shadow </dt><dd> display shadow with popup? default=true</dd>
36 * <dt>zIndex </dt><dd> which layer? default=1</dd>
37 * <dt>canDrag </dt><dd> boolean value (or function that returns a boolean) indicating if it is ok to drag/reposition popup, default=false</dd>
38 * <dt>onClose </dt><dd> function to call when the popup is closed</dd>
40 * @param containerDiv if supplied, then setDiv() is called at the end of initialization
42 initialize: function(containerDiv,options) {
47 position : 'absolute',
54 if (containerDiv) this.setDiv(containerDiv,options);
57 createContainer: function(options) {
58 this.setDiv(document.createElement('div'), options);
59 if (options && options.parent) {
60 options.parent.appendChild(this.container);
62 document.getElementsByTagName("body")[0].appendChild(this.container);
67 * Apply popup behavior to a div that already exists in the DOM
68 * @param containerDiv div element (or element id) in the DOM. If null, then the div is created automatically.
70 setDiv: function(containerDiv,options) {
71 Rico.extend(this.options, options || {});
72 this.container=Rico.$(containerDiv);
73 if (this.options.position == 'auto') {
74 this.position=Rico.getStyle(this.container,'position').toLowerCase();
76 this.position=this.container.style.position=this.options.position;
78 if (this.position != 'absolute') {
79 this.content=this.container;
82 if (this.options.zIndex >= 0) this.container.style.zIndex=this.options.zIndex;
83 if (this.options.closeFunc) {
84 this.closeFunc=this.options.closeFunc;
87 this.closeFunc=function() { self.closePopup(); };
89 //this.container.style.overflow='hidden';
90 this.container.style.top='0px';
91 this.container.style.left='0px';
92 this.container.style.display='none';
94 this.content=document.createElement('div');
95 while (this.container.firstChild) {
96 this.content.appendChild(this.container.firstChild);
98 this.container.appendChild(this.content);
99 this.content.className='RicoPopupContent';
100 this.content.style.position='relative';
102 if (Rico.isIE && Rico.ieVersion < 7) {
103 // create iframe shim
104 this.ifr = document.createElement('iframe');
105 this.ifr.style.position="absolute";
106 this.ifr.style.top = '0px';
107 this.ifr.style.left = '0px';
108 this.ifr.style.width = '2000px';
109 this.ifr.style.height = '2000px';
110 this.ifr.style.zIndex = -1;
111 this.ifr.frameBorder = 0;
112 this.ifr.src="javascript:false;";
113 this.content.appendChild(this.ifr);
115 Rico.applyShadow(this.container,this.options.shadow);
117 if (this.options.hideOnClick)
118 Rico.eventBind(document,"click", Rico.eventHandle(this,'_docClick'));
119 if (this.options.hideOnEscape)
120 Rico.eventBind(document,"keyup", Rico.eventHandle(this,'_checkKey'));
121 this.dragEnabled=false;
122 this.mousedownHandler = Rico.eventHandle(this,'_startDrag');
123 this.dragHandler = Rico.eventHandle(this,'_drag');
124 this.dropHandler = Rico.eventHandle(this,'_endDrag');
125 if (this.options.canDrag) this.enableDragging();
126 if (this.options.ignoreClicks || this.options.canDrag) this.ignoreClicks();
129 clearContent: function() {
130 this.content.innerHTML="";
133 setContent: function(content) {
134 this.content.innerHTML=content;
137 enableDragging: function() {
138 if (!this.dragEnabled && this.options.dragElement) {
139 Rico.eventBind(this.options.dragElement, "mousedown", this.mousedownHandler);
140 this.dragEnabled=true;
142 return this.dragEnabled;
145 disableDragging: function() {
146 if (!this.dragEnabled) return;
147 Rico.eventUnbind(this.options.dragElement, "mousedown", this.mousedownHandler);
148 this.dragEnabled=false;
151 setZ: function(zIndex) {
152 this.container.style.zIndex=zIndex;
156 ignoreClicks: function() {
157 Rico.eventBind(this.container,"click", Rico.eventHandle(this,'_ignoreClick'));
160 _ignoreClick: function(e) {
161 if (e.stopPropagation)
164 e.cancelBubble = true;
168 // event handler to process keyup events (hide menu on escape key)
169 _checkKey: function(e) {
170 if (Rico.eventKey(e)!=27 || !this.visible()) return true;
171 //alert('closing popup: '+this.container.className);
177 _docClick: function(e) {
183 * Move popup to specified position
185 move: function(left,top) {
186 if (typeof left=='number') this.container.style.left=left+'px';
187 if (typeof top=='number') this.container.style.top=top+'px';
190 _startDrag : function(event){
191 var elem=Rico.eventElement(event);
192 this.container.style.cursor='move';
193 this.lastMouse = Rico.eventClient(event);
194 Rico.eventBind(document, "mousemove", this.dragHandler);
195 Rico.eventBind(document, "mouseup", this.dropHandler);
196 Rico.eventStop(event);
199 _drag : function(event){
200 var newMouse = Rico.eventClient(event);
201 var newLeft = parseInt(this.container.style.left,10) + newMouse.x - this.lastMouse.x;
202 var newTop = parseInt(this.container.style.top,10) + newMouse.y - this.lastMouse.y;
203 this.move(newLeft, newTop);
204 this.lastMouse = newMouse;
205 Rico.eventStop(event);
208 _endDrag : function(){
209 this.container.style.cursor='';
210 Rico.eventUnbind(document, "mousemove", this.dragHandler);
211 Rico.eventUnbind(document, "mouseup", this.dropHandler);
215 * Display popup at specified position
217 openPopup: function(left,top) {
218 this.container.style.display=this.position=='absolute' ? "block" : Rico.isIE && Rico.ieVersion<8 ? "inline" : "inline-block";
219 if (typeof left=='number') this.container.style.left=left+'px';
220 if (typeof top=='number') this.container.style.top=top+'px';
221 if (this.container.id) Rico.log('openPopup '+this.container.id+' at '+left+','+top);
224 centerPopup: function() {
226 var msgWidth=this.container.offsetWidth;
227 var msgHeight=this.container.offsetHeight;
228 var divwi=this.container.parentNode.offsetWidth;
229 var divht=this.container.parentNode.offsetHeight;
230 this.move(parseInt(Math.max((divwi-msgWidth)/2,0),10), parseInt(Math.max((divht-msgHeight)/2,0),10));
233 visible: function() {
234 return Rico.visible(this.container);
240 closePopup: function() {
241 if (!Rico.visible(this.container)) return;
242 if (this.container.id) Rico.log('closePopup '+this.container.id);
243 if (this.dragEnabled) this._endDrag();
244 this.container.style.display="none";
245 if (this.options.onClose) this.options.onClose();
250 Rico.closeButton = function(handle) {
251 var a = document.createElement('a');
252 a.className='RicoCloseAnchor';
253 if (Rico.theme.closeAnchor) Rico.addClass(a,Rico.theme.closeAnchor);
254 var span = a.appendChild(document.createElement('span'));
255 span.title=Rico.getPhraseById('close');
256 new Rico.HoverSet([a]);
257 Rico.addClass(span,Rico.theme.close || 'RicoClose');
258 Rico.eventBind(a,"click", handle);
262 Rico.floatButton = function(buttonName, handle, title) {
263 var a=document.createElement("a");
264 a.className='RicoButtonAnchor'
265 Rico.addClass(a,Rico.theme.buttonAnchor || 'RicoButtonAnchorNative');
266 var span=a.appendChild(document.createElement("span"));
267 if (title) span.title=title;
268 span.className=Rico.theme[buttonName.toLowerCase()] || 'Rico'+buttonName;
269 Rico.eventBind(a,"click", handle, false);
270 new Rico.HoverSet([a]);
274 Rico.clearButton = function(handle) {
275 var span=document.createElement("span");
276 span.title=Rico.getPhraseById('clear');
277 span.className='ricoClear';
278 Rico.addClass(span, Rico.theme.clear || 'ricoClearNative');
279 span.style.display='inline-block';
280 span.style.cursor='pointer';
281 Rico.eventBind(span,"click", handle);
285 Rico.Window = function(title, options, contentParam) {
286 this.initialize(title, options, contentParam);
289 Rico.Window.prototype = {
292 * Create popup div with a title bar.
294 initialize: function(title, options, contentParam) {
295 options=options || {overflow:'auto'};
296 Rico.extend(this, new Rico.Popup());
298 this.titleDiv = document.createElement('div');
299 this.options.canDrag=true;
300 this.options.dragElement=this.titleDiv;
301 this.options.hideOnClick=false;
302 this.createContainer(options);
303 this.content.appendChild(this.titleDiv);
304 contentParam=Rico.$(contentParam);
305 this.contentDiv=contentParam || document.createElement('div');
306 this.content.appendChild(this.contentDiv);
309 this.titleDiv.className='ricoTitle';
310 if (Rico.theme.dialogTitle) Rico.addClass(this.titleDiv,Rico.theme.dialogTitle);
311 this.titleDiv.style.position='relative';
312 this.titleContent = document.createElement('span');
313 this.titleContent.className='ricoTitleSpan';
314 this.titleDiv.appendChild(this.titleContent);
315 this.titleDiv.appendChild(Rico.closeButton(Rico.eventHandle(this,'closePopup')));
316 if (!title && contentParam) {
317 title=contentParam.title;
318 contentParam.title='';
320 this.setTitle(title || ' ');
322 // create content area
323 this.contentDiv.className='ricoContent';
324 if (Rico.theme.dialogContent) Rico.addClass(this.contentDiv,Rico.theme.dialogContent);
325 this.contentDiv.style.position='relative';
326 if (options.height) this.contentDiv.style.height=options.height;
327 if (options.width) this.contentDiv.style.width=options.width;
328 if (options.overflow) this.contentDiv.style.overflow=options.overflow;
329 Rico.addClass(this.content,'ricoWindow');
330 if (Rico.theme.dialog) Rico.addClass(this.content,Rico.theme.dialog);
332 // fix float'ed content in IE
333 this.titleDiv.style.zoom=1;
334 this.contentDiv.style.zoom=1;
337 this.content=this.contentDiv;
340 setTitle: function(title) {
341 this.titleContent.innerHTML=title;
347 Rico.Menu = function(options) {
348 this.initialize(options);
351 Rico.Menu.prototype = {
353 * @class Implements popup menus and submenus
354 * @extends Rico.Popup
357 initialize: function(options) {
358 Rico.extend(this, new Rico.Popup());
359 Rico.extend(this.options, {
363 if (typeof options=='string')
364 this.options.width=options;
366 Rico.extend(this.options, options || {});
368 this.highlightElem=null;
369 new Image().src = Rico.imgDir+'left_b.gif';
370 new Image().src = Rico.imgDir+'right_b.gif';
373 createDiv: function(parentNode) {
374 if (this.container) return;
376 var options={ closeFunc: function() { self.cancelmenu(); } };
377 if (parentNode) options.parent=parentNode;
378 this.createContainer(options);
379 this.content.className = Rico.isWebKit ? 'ricoMenuSafari' : 'ricoMenu';
380 this.content.style.width=this.options.width;
381 this.direction=Rico.getStyle(this.container,'direction') || 'ltr';
382 this.direction=this.direction.toLowerCase(); // ltr or rtl
387 showmenu: function(e,hideFunc){
389 this.hideFunc=hideFunc;
390 if (this.content.childNodes.length==0) {
394 var mousePos = Rico.eventClient(e);
395 this.openmenu(mousePos.x,mousePos.y,0,0);
398 openmenu: function(x,y,clickItemWi,clickItemHt,noOffset) {
399 var newLeft=x + (noOffset ? 0 : Rico.docScrollLeft());
400 this.container.style.visibility="hidden";
401 this.container.style.display="block";
402 var w=this.container.offsetWidth;
403 var cw=this.content.offsetWidth;
404 //window.status='openmenu: newLeft='+newLeft+' width='+w+' clickItemWi='+clickItemWi+' windowWi='+Rico.windowWidth();
405 if (this.direction == 'rtl') {
406 if (newLeft > w+clickItemWi) newLeft-=cw+clickItemWi;
408 if (x+w > Rico.windowWidth()) newLeft-=cw+clickItemWi-2;
410 var scrTop=Rico.docScrollTop();
411 var newTop=y + (noOffset ? 0 : scrTop);
412 if (y+this.container.offsetHeight-scrTop > Rico.windowHeight())
413 newTop=Math.max(newTop-this.content.offsetHeight+clickItemHt,0);
414 this.openPopup(newLeft,newTop);
415 this.container.style.visibility ="visible";
419 clearMenu: function() {
421 this.defaultAction=null;
425 addMenuHeading: function(hdg) {
426 var el=document.createElement('div');
428 el.className='ricoMenuHeading';
429 this.content.appendChild(el);
432 addMenuBreak: function() {
433 var brk=document.createElement('div');
434 brk.className="ricoMenuBreak";
435 this.content.appendChild(brk);
438 addSubMenuItem: function(menutext, submenu, translate) {
439 var dir=this.direction=='rtl' ? 'left' : 'right';
440 var a=this.addMenuItem(menutext,null,true,null,translate);
441 a.className='ricoSubMenu';
442 a.style.backgroundImage='url('+Rico.imgDir+dir+'_b.gif)';
443 a.style.backgroundRepeat='no-repeat';
444 a.style.backgroundPosition=dir;
445 a.RicoSubmenu=submenu;
446 Rico.eventBind(a,"mouseover", Rico.eventHandle(this,'showSubMenu'));
447 Rico.eventBind(a,"mouseout", Rico.eventHandle(this,'subMenuOut'));
450 showSubMenu: function(e) {
451 if (this.openSubMenu) this.hideSubMenu();
452 var a=Rico.eventElement(e);
453 this.openSubMenu=a.RicoSubmenu;
454 this.openMenuAnchor=a;
455 if (a.className=='ricoSubMenu') a.className='ricoSubMenuOpen';
456 a.RicoSubmenu.openmenu(parseInt(this.container.style.left)+a.offsetWidth, parseInt(this.container.style.top)+a.offsetTop, a.offsetWidth-2, a.offsetHeight+2,true);
459 subMenuOut: function(e) {
460 if (!this.openSubMenu) return;
462 var elem=Rico.eventElement(e);
463 var reltg = Rico.eventRelatedTarget(e) || e.toElement;
465 while (reltg != null && reltg != this.openSubMenu.div)
466 reltg=reltg.parentNode;
468 if (reltg == this.openSubMenu.div) return;
472 hideSubMenu: function() {
473 if (this.openMenuAnchor) {
474 this.openMenuAnchor.className='ricoSubMenu';
475 this.openMenuAnchor=null;
477 if (this.openSubMenu) {
478 this.openSubMenu.hidemenu();
479 this.openSubMenu=null;
483 addMenuItemId: function(phraseId,action,enabled,title,target) {
484 if ( arguments.length < 3 ) enabled=true;
485 this.addMenuItem(Rico.getPhraseById(phraseId),action,enabled,title,target);
488 // if action is a string, then it is assumed to be a URL and the target parm can be used indicate which window gets the content
489 // action can also be a function
490 // action can also be a Rico.eventHandle, but set target='event' in this case
491 addMenuItem: function(menutext,action,enabled,title,target) {
492 if (arguments.length >= 3 && !enabled && !this.options.showDisabled) return null;
494 var a = document.createElement(typeof action=='string' ? 'a' : 'div');
495 if ( arguments.length < 3 || enabled ) {
496 if (typeof action=='string') {
498 if (target) a.target = target;
499 } else if (target=='event') {
500 Rico.eventBind(a,"click", action);
504 a.className = 'enabled';
505 if (this.defaultAction==null) this.defaultAction=action;
508 a.className = 'disabled';
510 a.innerHTML = menutext;
511 if (typeof title=='string')
513 a=this.content.appendChild(a);
514 Rico.eventBind(a,"mouseover", Rico.eventHandle(this,'mouseOver'));
515 Rico.eventBind(a,"mouseout", Rico.eventHandle(this,'mouseOut'));
519 mouseOver: function(e) {
520 if (this.highlightElem && this.highlightElem.className=='enabled-hover') {
521 // required for Safari
522 this.highlightElem.className='enabled';
523 this.highlightElem=null;
525 var elem=Rico.eventElement(e);
526 if (this.openMenuAnchor && this.openMenuAnchor!=elem)
528 if (elem.className=='enabled') {
529 elem.className='enabled-hover';
530 this.highlightElem=elem;
534 mouseOut: function(e) {
535 var elem=Rico.eventElement(e);
536 if (elem.className=='enabled-hover') elem.className='enabled';
537 if (this.highlightElem==elem) this.highlightElem=null;
540 cancelmenu: function() {
541 if (!this.visible()) return;
542 if (this.hideFunc) this.hideFunc();
547 hidemenu: function() {
548 if (this.openSubMenu) this.openSubMenu.hidemenu();
555 Rico.SelectionSet = function(selectionSet, options) {
556 this.initialize(selectionSet, options);
559 Rico.SelectionSet.prototype = {
563 * @param selectionSet collection of DOM elements (or a CSS selection string)
564 * @param options object may contain any of the following:<dl>
565 * <dt>selectedClass</dt><dd>class name to add when element is selected, default is "selected"</dd>
566 * <dt>selectNode </dt><dd>optional function that returns the element to be selected</dd>
567 * <dt>onSelect </dt><dd>optional function that gets called when element is selected</dd>
568 * <dt>onFirstSelect</dt><dd>optional function that gets called the first time element is selected</dd>
569 * <dt>noDefault </dt><dd>when true, no element in the set is initially selected, default is false</dd>
570 * <dt>selectedIndex</dt><dd>index of the element that should be initially selected, default is 0</dd>
571 * <dt>cookieName </dt><dd>optional name of cookie to use to remember selected element. If specified, and the cookie exists, then the cookie value overrides selectedIndex.</dd>
572 * <dt>cookieDays </dt><dd>specifies how long cookie should persist (in days). If unspecified, then the cookie persists for the current session.</dd>
573 * <dt>cookiePath </dt><dd>optional cookie path</dd>
574 * <dt>cookieDomain </dt><dd>optional cookie domain</dd>
577 initialize: function(selectionSet, options){
578 Rico.log('SelectionSet#initialize');
579 this.options = options || {};
580 if (typeof selectionSet == 'string')
581 selectionSet = Rico.select(selectionSet);
582 this.previouslySelected = [];
583 this.selectionSet = [];
584 this.selectedClassName = this.options.selectedClass || Rico.theme.selected || "selected";
585 this.selectNode = this.options.selectNode || function(e){return e;};
586 this.onSelect = this.options.onSelect;
587 this.onFirstSelect = this.options.onFirstSelect;
589 this.clickHandler = function(idx) { self.selectIndex(idx); };
590 this.selectedIndex=-1;
591 for (var i=0; i<selectionSet.length; i++)
592 this.add(selectionSet[i]);
593 if (!this.options.noDefault) {
594 var cookieIndex=this.options.cookieName ? this.getCookie() : 0;
595 this.selectIndex(cookieIndex || this.options.selectedIndex || 0);
598 getCookie: function() {
599 var cookie = Rico.getCookie(this.options.cookieName);
600 if (!cookie) return 0;
601 var index = parseInt(cookie);
602 return index < this.selectionSet.length ? index : 0;
605 this.previouslySelected = [];
606 this._notifySelected(this.selectedIndex);
608 clearSelected: function() {
610 Rico.removeClass(this.selectNode(this.selected), this.selectedClassName);
612 getIndex: function(element) {
613 for (var i=0; i<this.selectionSet.length; i++) {
614 if (element == this.selectionSet[i]) return i;
618 select: function(element){
619 if (this.selected == element) return;
620 var i=this.getIndex(element);
621 if (i >= 0) this.selectIndex(i);
623 _notifySelected: function(index){
624 if (index < 0) return;
625 var element = this.selectionSet[index];
626 if (this.options.cookieName)
627 Rico.setCookie(this.options.cookieName, index, this.options.cookieDays, this.options.cookiePath, this.options.cookieDomain);
628 if (this.onFirstSelect && !this.previouslySelected[index]){
629 this.onFirstSelect(element, index);
630 this.previouslySelected[index] = true;
634 this.onSelect(index);
637 selectIndex: function(index){
638 if (this.selectedIndex == index || index >= this.selectionSet.length) return;
639 this.clearSelected();
640 this._notifySelected(index);
641 this.selectedIndex = index;
642 this.selected=this.selectionSet[index].element;
643 Rico.addClass(this.selectNode(this.selected), this.selectedClassName);
645 nextSelectIndex: function(){
646 return (this.selectedIndex + 1) % this.selectionSet.length;
648 nextSelectItem: function(){
649 return this.selectionSet[this.nextSelectIndex()];
651 selectNext: function(){
652 this.selectIndex(this.nextSelectIndex());
655 var index=this.selectionSet.length;
656 this.selectionSet[index] = new Rico._SelectionItem(item,index,this.clickHandler);
658 remove: function(item){
659 if (item==this.selected) this.clearSelected();
660 var i=this.getIndex(item);
662 this.selectionSet[i].remove();
663 this.selectionSet.splice(i,1);
665 removeAll: function(){
666 this.clearSelected();
667 while (this.selectionSet.length > 0) {
668 this.selectionSet.pop().remove();
674 Rico._SelectionItem=function(element,index,callback) {
675 this.add(element,index,callback);
678 Rico._SelectionItem.prototype = {
679 add: function(element,index,callback) {
680 this.element=element;
682 this.callback=callback;
683 this.handle=Rico.eventHandle(this,'click');
684 Rico.eventBind(element, "click", this.handle);
687 click: function(ev) {
688 this.callback(this.index);
692 Rico.eventUnbind(this.element, "click", this.handle);
697 Rico.HoverSet = function(hoverSet, options) {
698 this.initialize(hoverSet, options);
701 Rico.HoverSet.prototype = {
705 * @param hoverSet collection of DOM elements
706 * @param options object may contain any of the following:<dl>
707 * <dt>hoverClass</dt><dd> class name to add when mouse is over element, default is "hover"</dd>
708 * <dt>hoverNodes</dt><dd> optional function to select/filter which nodes are in the set</dd>
711 initialize: function(hoverSet, options){
712 Rico.log('HoverSet#initialize');
713 options = options || {};
714 this.hoverClass = options.hoverClass || Rico.theme.hover || "hover";
715 this.hoverFunc = options.hoverNodes || function(e){return [e];};
717 if (!hoverSet) return;
718 for (var i=0; i<hoverSet.length; i++)
719 this.add(hoverSet[i]);
721 add: function(item) {
722 this.hoverSet.push(new Rico._HoverItem(item,this.hoverFunc,this.hoverClass));
724 removeAll: function(){
725 while (this.hoverSet.length > 0) {
726 this.hoverSet.pop().remove();
732 Rico._HoverItem=function(element,selectFunc,hoverClass) {
733 this.add(element,selectFunc,hoverClass);
736 Rico._HoverItem.prototype = {
737 add: function(element,selectFunc,hoverClass) {
738 this.element=element;
739 this.selectFunc=selectFunc;
740 this.hoverClass=hoverClass;
741 this.movehandle=Rico.eventHandle(this,'move');
742 this.outhandle=Rico.eventHandle(this,'mouseout');
743 Rico.eventBind(element, "mousemove", this.movehandle);
744 Rico.eventBind(element, "mouseout", this.outhandle);
748 var elems=this.selectFunc(this.element);
749 for (var i=0; i<elems.length; i++)
750 Rico.addClass(elems[i],this.hoverClass);
753 mouseout: function(ev) {
754 var elems=this.selectFunc(this.element);
755 for (var i=0; i<elems.length; i++)
756 Rico.removeClass(elems[i],this.hoverClass);
760 Rico.eventUnbind(element, "mousemove", this.movehandle);
761 Rico.eventUnbind(element, "mouseout", this.outhandle);
768 Rico.Effect.easeIn = function(step){
769 return Math.sqrt(step);
771 Rico.Effect.easeOut = function(step){
776 /** @class core methods for transition effects */
777 Rico.ContentTransitionBase = function() {};
778 Rico.ContentTransitionBase.prototype = {
779 initBase: function(titles, contents, options) {
780 Rico.log('ContentTransitionBase#initBase');
781 if (typeof titles == 'string')
782 titles = Rico.select(titles);
783 if (typeof contents == 'string')
784 contents = Rico.select(contents);
786 this.options = options || {};
787 this.titles = titles;
788 this.contents = contents;
789 this.hoverSet = new Rico.HoverSet(titles, options);
790 for (var i=0; i<contents.length; i++) {
791 if (contents[i]) Rico.hide(contents[i]);
794 this.selectionSet = new Rico.SelectionSet(titles, Rico.extend(options, { onSelect: function(idx) { self._finishSelect(idx); } }));
797 this.selectionSet.reset();
799 select: function(index) {
800 this.selectionSet.selectIndex(index);
802 _finishSelect: function(index) {
803 Rico.log('ContentTransitionBase#_finishSelect');
804 var panel = this.contents[index];
806 alert('Internal error: no panel @index='+index);
809 if ( this.selected == panel) return;
810 if (this.transition){
812 this.transition(panel);
814 panel.style.display='block';
817 if (this.selected) Rico.hide(this.selected);
818 panel.style.display='block';
820 this.selected = panel;
822 addBase: function(title, content){
823 this.titles.push(title);
824 this.contents.push(content);
825 this.hoverSet.add(title);
826 this.selectionSet.add(title);
828 //this.selectionSet.select(title);
830 removeBase: function(title){},
831 removeAll: function(){
832 this.hoverSet.removeAll();
833 this.selectionSet.removeAll();
839 * @class Implements accordion effect
840 * @see Rico.ContentTransitionBase#initialize for construction parameters
841 * @extends Rico.ContentTransitionBase
843 Rico.Accordion = function(element, options) {
844 this.initialize(element, options);
847 Rico.Accordion.prototype = Rico.extend(new Rico.ContentTransitionBase(),
848 /** @lends Rico.Accordion# */
850 initialize: function(element, options) {
851 element=Rico.$(element);
852 element.style.overflow='hidden';
853 element.className=options.accClass || Rico.theme.accordion || "Rico_accordion";
854 if (typeof options.panelWidth=='number') options.panelWidth+="px";
855 if (options.panelWidth) element.style.width = options.panelWidth;
856 var panels=Rico.getDirectChildrenByTag(element,'div');
857 var items,titles=[], contents=[];
858 for (var i=0; i<panels.length; i++) {
859 items=Rico.getDirectChildrenByTag(panels[i],'div');
860 if (items.length>=2) {
861 items[0].className=options.titleClass || Rico.theme.accTitle || "Rico_accTitle";
862 items[1].className=options.contentClass || Rico.theme.accContent || "Rico_accContent";
863 titles.push(items[0]);
864 contents.push(items[1]);
865 var a=Rico.wrapChildren(items[0],'','','a');
866 a.href="javascript:void(0)";
869 Rico.log('creating Rico.Accordion for '+element.id+' with '+titles.length+' panels');
870 this.initBase(titles, contents, options);
871 this.selected.style.height = this.options.panelHeight + "px";
872 this.totSteps=(typeof options.duration =='number' ? options.duration : 200)/25;
874 transition: function(p){
875 if (!this.options.noAnimate) {
876 this.closing=this.selected;
880 this.timer=setInterval(function() { self.step(); },25);
882 p.style.height = this.options.panelHeight + "px";
883 if (this.selected) Rico.hide(this.selected);
884 p.style.display='block';
889 var oheight=Math.round(this.curStep/this.totSteps*this.options.panelHeight);
890 this.opening.style.height=oheight+'px';
891 this.closing.style.height=(this.options.panelHeight - oheight)+'px';
892 if (this.curStep==1) {
893 this.opening.style.paddingTop=this.opening.style.paddingBottom='0px';
894 this.opening.style.display='block';
896 if (this.curStep==this.totSteps) {
897 clearInterval(this.timer);
898 this.opening.style.paddingTop=this.opening.style.paddingBottom='';
899 Rico.hide(this.closing);
902 setPanelHeight: function(h) {
903 this.options.panelHeight = h;
904 this.selected.style.height = this.options.panelHeight + "px";
910 * @class Implements tabbed panel effect
911 * @see Rico.ContentTransitionBase#initialize for construction parameters
912 * @extends Rico.ContentTransitionBase
914 Rico.TabbedPanel = function(element, options) {
915 this.initialize(element, options);
918 Rico.TabbedPanel.prototype = Rico.extend(new Rico.ContentTransitionBase(),
920 initialize: function(element, options) {
921 element=Rico.$(element);
922 options=options || {};
923 if (typeof options.panelWidth=='number') options.panelWidth+="px";
924 if (typeof options.panelHeight=='number') options.panelHeight+="px";
925 element.className=options.tabClass || Rico.theme.tabPanel || "Rico_tabPanel";
926 if (options.panelWidth) element.style.width = options.panelWidth;
928 var allKids = element.childNodes;
929 for( var i = 0 ; i < allKids.length ; i++ ) {
930 if (allKids[i] && allKids[i].tagName && allKids[i].tagName.match(/^div|ul$/i))
931 items.push(allKids[i]);
933 if (items.length < 2) return;
934 var childTag=items[0].tagName.toLowerCase()=='ul' ? 'li' : 'div';
935 items[0].className=options.navContainerClass || Rico.theme.tabNavContainer || "Rico_tabNavContainer";
936 items[0].style.listStyle='none';
937 items[1].className=options.contentContainerClass || Rico.theme.tabContentContainer || "Rico_tabContentContainer";
938 var titles=Rico.getDirectChildrenByTag(items[0], childTag);
939 var contents=Rico.getDirectChildrenByTag(items[1],'div');
940 if (!options.corners) options.corners='top';
941 for (var i=0; i<titles.length; i++) {
942 titles[i].className=options.titleClass || Rico.theme.tabTitle || "Rico_tabTitle";
943 var a=Rico.wrapChildren(titles[i],'','','a');
944 a.href="javascript:void(0)";
945 contents[i].className=options.contentClass || Rico.theme.tabContent || "Rico_tabContent";
946 if (options.panelHeight) contents[i].style.overflow='auto';
947 if (options.corners!='none') {
948 if (options.panelHdrWidth) titles[i].style.width=options.panelHdrWidth;
949 Rico.Corner.round(titles[i], Rico.theme.tabCornerOptions || options);
952 options.selectedClass=Rico.theme.tabSelected || 'selected';
953 this.initBase(titles, contents, options);
954 if (this.selected) this.transition(this.selected);
956 transition: function(p){
957 Rico.log('TabbedPanel#transition '+typeof(p));
958 if (this.selected) Rico.hide(this.selected);
960 if (this.options.panelHeight) p.style.height = this.options.panelHeight;
970 round: function(e, options) {
974 bgColor : "fromParent",
976 nativeCorners: false // only use native corners?
978 Rico.extend(this.options, options || {});
979 if (typeof(Rico.getStyle(e,'border-radius'))=='string')
980 this._roundCornersStdCss(e);
981 else if (typeof(Rico.getStyle(e,'-webkit-border-radius'))=='string')
982 this._roundCornersWebKit(e);
983 else if (typeof(Rico.getStyle(e,'-moz-border-radius'))=='string')
984 this._roundCornersMoz(e);
985 else if (!this.options.nativeCorners)
986 this._roundCornersImpl(e);
989 _roundCornersStdCss: function(e) {
990 var radius=this.options.compact ? '4px' : '8px';
991 if (this._hasString(this.options.corners, "all"))
992 Rico.setStyle(e, {borderRadius:radius});
994 if (this._hasString(this.options.corners, "top", "tl")) Rico.setStyle(e, {borderTopLeftRadius:radius});
995 if (this._hasString(this.options.corners, "top", "tr")) Rico.setStyle(e, {borderTopRightRadius:radius});
996 if (this._hasString(this.options.corners, "bottom", "bl")) Rico.setStyle(e, {borderBottomLeftRadius:radius});
997 if (this._hasString(this.options.corners, "bottom", "br")) Rico.setStyle(e, {borderBottomRightRadius:radius});
1001 _roundCornersWebKit: function(e) {
1002 var radius=this.options.compact ? '4px' : '8px';
1003 if (this._hasString(this.options.corners, "all"))
1004 Rico.setStyle(e, {WebkitBorderRadius:radius});
1006 if (this._hasString(this.options.corners, "top", "tl")) Rico.setStyle(e, {WebkitBorderTopLeftRadius:radius});
1007 if (this._hasString(this.options.corners, "top", "tr")) Rico.setStyle(e, {WebkitBorderTopRightRadius:radius});
1008 if (this._hasString(this.options.corners, "bottom", "bl")) Rico.setStyle(e, {WebkitBorderBottomLeftRadius:radius});
1009 if (this._hasString(this.options.corners, "bottom", "br")) Rico.setStyle(e, {WebkitBorderBottomRightRadius:radius});
1013 _roundCornersMoz: function(e) {
1014 var radius=this.options.compact ? '4px' : '8px';
1015 if (this._hasString(this.options.corners, "all"))
1016 Rico.setStyle(e, {MozBorderRadius:radius});
1018 if (this._hasString(this.options.corners, "top", "tl")) Rico.setStyle(e, {MozBorderRadiusTopleft:radius});
1019 if (this._hasString(this.options.corners, "top", "tr")) Rico.setStyle(e, {MozBorderRadiusTopright:radius});
1020 if (this._hasString(this.options.corners, "bottom", "bl")) Rico.setStyle(e, {MozBorderRadiusBottomleft:radius});
1021 if (this._hasString(this.options.corners, "bottom", "br")) Rico.setStyle(e, {MozBorderRadiusBottomright:radius});
1025 _roundCornersImpl: function(e) {
1026 var bgColor = this.options.bgColor == "fromParent" ? this._background(e.parentNode) : this.options.bgColor;
1027 e.style.position='relative';
1028 //this.options.numSlices = this.options.compact ? 2 : 4;
1029 if (this._hasString(this.options.corners, "all", "top", "tl")) this._createCorner(e,'top','left',bgColor);
1030 if (this._hasString(this.options.corners, "all", "top", "tr")) this._createCorner(e,'top','right',bgColor);
1031 if (this._hasString(this.options.corners, "all", "bottom", "bl")) this._createCorner(e,'bottom','left',bgColor);
1032 if (this._hasString(this.options.corners, "all", "bottom", "br")) this._createCorner(e,'bottom','right',bgColor);
1035 _createCorner: function(elem,tb,lr,bgColor) {
1036 //alert('Corner: '+tb+' '+lr+' bgColor='+typeof(bgColor));
1037 var corner = document.createElement("div");
1038 corner.className='ricoCorner';
1039 Rico.setStyle(corner,{width:'6px', height:'5px'});
1040 var borderStyle = Rico.getStyle(elem,'border-'+tb+'-style');
1041 var borderColor = borderStyle=='none' ? bgColor : Rico.getStyle(elem,'border-'+tb+'-color');
1042 //alert('Corner: '+tb+' '+borderStyle+borderColor+' '+);
1043 var pos=borderStyle=='none' ? '0px' : '-1px';
1044 corner.style[tb]=pos;
1045 corner.style[lr]=Rico.isIE && Rico.ieVersion<7 && lr=='right' && borderStyle!='none' ? '-2px' : '-1px';
1046 //corner.style[lr]='-1px';
1047 elem.appendChild(corner);
1048 var marginSizes = [ 0, 2, 3, 4, 4 ];
1049 if (tb=='bottom') marginSizes.reverse();
1050 var borderVal= borderStyle=='none' ? '0px none' : '1px solid '+borderColor;
1051 var d= lr=='left' ? 'Right' : 'Left';
1052 for (var i=0; i<marginSizes.length; i++) {
1053 var slice = document.createElement("div");
1054 Rico.setStyle(slice,{backgroundColor:bgColor,height:'1px'});
1055 slice.style['margin'+d]=marginSizes[i]+'px';
1056 slice.style['border'+d]=borderVal;
1057 corner.appendChild(slice);
1061 _background: function(elem) {
1063 var actualColor = Rico.getStyle(elem, "backgroundColor");
1065 // if color is tranparent, check parent
1066 // Safari returns "rgba(0, 0, 0, 0)", which means transparent
1067 if ( actualColor.match(/^(transparent|rgba\(0,\s*0,\s*0,\s*0\))$/i) && elem.parentNode )
1068 return this._background(elem.parentNode);
1070 return actualColor == null ? "#ffffff" : actualColor;
1076 _hasString: function(str) {
1077 for(var i=1 ; i<arguments.length ; i++) {
1078 if (str.indexOf(arguments[i]) >= 0) return true;
1085 Rico.toColorPart = function(c) {
1086 return Rico.zFill(c, 2, 16);
1090 Rico.Color = function(red, green, blue) {
1091 this.initialize(red, green, blue);
1094 Rico.Color.prototype = {
1096 * @class Methods to manipulate color values.
1098 * @param red integer (0-255)
1099 * @param green integer (0-255)
1100 * @param blue integer (0-255)
1102 initialize: function(red, green, blue) {
1103 this.rgb = { r: red, g : green, b : blue };
1106 setRed: function(r) {
1110 setGreen: function(g) {
1114 setBlue: function(b) {
1118 setHue: function(h) {
1120 // get an HSB model, and set the new hue...
1121 var hsb = this.asHSB();
1124 // convert back to RGB...
1125 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
1128 setSaturation: function(s) {
1129 // get an HSB model, and set the new hue...
1130 var hsb = this.asHSB();
1133 // convert back to RGB and set values...
1134 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
1137 setBrightness: function(b) {
1138 // get an HSB model, and set the new hue...
1139 var hsb = this.asHSB();
1142 // convert back to RGB and set values...
1143 this.rgb = Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b );
1146 darken: function(percent) {
1147 var hsb = this.asHSB();
1148 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.max(hsb.b - percent,0));
1151 brighten: function(percent) {
1152 var hsb = this.asHSB();
1153 this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.min(hsb.b + percent,1));
1156 blend: function(other) {
1157 this.rgb.r = Math.floor((this.rgb.r + other.rgb.r)/2);
1158 this.rgb.g = Math.floor((this.rgb.g + other.rgb.g)/2);
1159 this.rgb.b = Math.floor((this.rgb.b + other.rgb.b)/2);
1162 isBright: function() {
1163 var hsb = this.asHSB();
1164 return this.asHSB().b > 0.5;
1167 isDark: function() {
1168 return ! this.isBright();
1172 return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")";
1176 return "#" + Rico.toColorPart(this.rgb.r) + Rico.toColorPart(this.rgb.g) + Rico.toColorPart(this.rgb.b);
1180 return Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b);
1183 toString: function() {
1184 return this.asHex();
1190 * Factory method for creating a color from an RGB string
1191 * @param hexCode a 3 or 6 digit hex string, optionally preceded by a # symbol
1192 * @returns a Rico.Color object
1194 Rico.Color.createFromHex = function(hexCode) {
1195 if(hexCode.length==4) {
1196 var shortHexCode = hexCode;
1198 for(var i=1;i<4;i++)
1199 hexCode += (shortHexCode.charAt(i) + shortHexCode.charAt(i));
1201 if ( hexCode.indexOf('#') == 0 )
1202 hexCode = hexCode.substring(1);
1203 if (!hexCode.match(/^[0-9A-Fa-f]{6}$/)) return null;
1204 var red = hexCode.substring(0,2);
1205 var green = hexCode.substring(2,4);
1206 var blue = hexCode.substring(4,6);
1207 return new Rico.Color( parseInt(red,16), parseInt(green,16), parseInt(blue,16) );
1211 * Retrieves the background color of an HTML element
1212 * @param elem the DOM element whose background color should be retreived
1213 * @returns a Rico.Color object
1215 Rico.Color.createColorFromBackground = function(elem) {
1217 if (!elem.style) return new Rico.Color(255,255,255);
1218 var actualColor = Rico.getStyle(elem, "background-color");
1220 // if color is tranparent, check parent
1221 // Safari returns "rgba(0, 0, 0, 0)", which means transparent
1222 if ( actualColor.match(/^(transparent|rgba\(0,\s*0,\s*0,\s*0\))$/i) && elem.parentNode )
1223 return Rico.Color.createColorFromBackground(elem.parentNode);
1225 if (actualColor == null) return new Rico.Color(255,255,255);
1227 if ( actualColor.indexOf("rgb(") == 0 ) {
1228 var colors = actualColor.substring(4, actualColor.length - 1 );
1229 var colorArray = colors.split(",");
1230 return new Rico.Color( parseInt( colorArray[0],10 ),
1231 parseInt( colorArray[1],10 ),
1232 parseInt( colorArray[2],10 ) );
1235 else if ( actualColor.indexOf("#") == 0 ) {
1236 return Rico.Color.createFromHex(actualColor);
1239 return new Rico.Color(255,255,255);
1243 * Converts hue/saturation/brightness to RGB
1244 * @returns a 3-element object: r=red, g=green, b=blue.
1246 Rico.Color.HSBtoRGB = function(hue, saturation, brightness) {
1252 if (saturation == 0) {
1253 red = parseInt(brightness * 255.0 + 0.5,10);
1258 var h = (hue - Math.floor(hue)) * 6.0;
1259 var f = h - Math.floor(h);
1260 var p = brightness * (1.0 - saturation);
1261 var q = brightness * (1.0 - saturation * f);
1262 var t = brightness * (1.0 - (saturation * (1.0 - f)));
1264 switch (parseInt(h,10)) {
1266 red = (brightness * 255.0 + 0.5);
1267 green = (t * 255.0 + 0.5);
1268 blue = (p * 255.0 + 0.5);
1271 red = (q * 255.0 + 0.5);
1272 green = (brightness * 255.0 + 0.5);
1273 blue = (p * 255.0 + 0.5);
1276 red = (p * 255.0 + 0.5);
1277 green = (brightness * 255.0 + 0.5);
1278 blue = (t * 255.0 + 0.5);
1281 red = (p * 255.0 + 0.5);
1282 green = (q * 255.0 + 0.5);
1283 blue = (brightness * 255.0 + 0.5);
1286 red = (t * 255.0 + 0.5);
1287 green = (p * 255.0 + 0.5);
1288 blue = (brightness * 255.0 + 0.5);
1291 red = (brightness * 255.0 + 0.5);
1292 green = (p * 255.0 + 0.5);
1293 blue = (q * 255.0 + 0.5);
1298 return { r : parseInt(red,10), g : parseInt(green,10) , b : parseInt(blue,10) };
1302 * Converts RGB value to hue/saturation/brightness
1303 * @param r integer (0-255)
1304 * @param g integer (0-255)
1305 * @param b integer (0-255)
1306 * @returns a 3-element object: h=hue, s=saturation, b=brightness.
1307 * (unlike some HSB documentation which states hue should be a value 0-360, this routine returns hue values from 0 to 1.0)
1309 Rico.Color.RGBtoHSB = function(r, g, b) {
1315 var cmax = (r > g) ? r : g;
1319 var cmin = (r < g) ? r : g;
1323 brightness = cmax / 255.0;
1325 saturation = (cmax - cmin)/cmax;
1329 if (saturation == 0)
1332 var redc = (cmax - r)/(cmax - cmin);
1333 var greenc = (cmax - g)/(cmax - cmin);
1334 var bluec = (cmax - b)/(cmax - cmin);
1337 hue = bluec - greenc;
1339 hue = 2.0 + redc - bluec;
1341 hue = 4.0 + greenc - redc;
1348 return { h : hue, s : saturation, b : brightness };
1352 * Returns a new XML document object
1354 Rico.createXmlDocument = function() {
1355 if (document.implementation && document.implementation.createDocument) {
1356 var doc = document.implementation.createDocument("", "", null);
1357 // some older versions of Moz did not support the readyState property
1358 // and the onreadystate event so we patch it!
1359 if (doc.readyState == null) {
1361 doc.addEventListener("load", function () {
1363 if (typeof doc.onreadystatechange == "function") {
1364 doc.onreadystatechange();
1371 if (window.ActiveXObject)
1372 return Rico.tryFunctions(
1373 function() { return new ActiveXObject('MSXML2.DomDocument'); },
1374 function() { return new ActiveXObject('Microsoft.DomDocument');},
1375 function() { return new ActiveXObject('MSXML.DomDocument'); },
1376 function() { return new ActiveXObject('MSXML3.DomDocument'); }