You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
13 KiB
JavaScript

5 years ago
var clippy={};clippy.Agent=function(a,b,c){this.path=a,this._queue=new clippy.Queue($.proxy(this._onQueueEmpty,this)),this._el=$('<div class="clippy"></div>').hide(),$(document.body).append(this._el),this._animator=new clippy.Animator(this._el,a,b,c),this._balloon=new clippy.Balloon(this._el),this._setupEvents()},clippy.Agent.prototype={gestureAt:function(a,b){var c=this._getDirection(a,b),d="Gesture"+c,e="Look"+c,f=this.hasAnimation(d)?d:e;return this.play(f)},hide:function(a,b){this._hidden=!0;var c=this._el;this.stop();if(a){this._el.hide(),this.stop(),this.pause(),b&&b();return}return this._playInternal("Hide",function(){c.hide(),this.pause(),b&&b()})},moveTo:function(a,b,c){var d=this._getDirection(a,b),e="Move"+d;c===undefined&&(c=1e3),this._addToQueue(function(d){if(c===0){this._el.css({top:b,left:a}),this.reposition(),d();return}if(!this.hasAnimation(e)){this._el.animate({top:b,left:a},c,d);return}var f=$.proxy(function(e,f){f===clippy.Animator.States.EXITED&&d(),f===clippy.Animator.States.WAITING&&this._el.animate({top:b,left:a},c,$.proxy(function(){this._animator.exitAnimation()},this))},this);this._playInternal(e,f)},this)},_playInternal:function(a,b){this._isIdleAnimation()&&this._idleDfd&&this._idleDfd.state()==="pending"&&this._idleDfd.done($.proxy(function(){this._playInternal(a,b)},this)),this._animator.showAnimation(a,b)},play:function(a,b,c){return this.hasAnimation(a)?(b===undefined&&(b=5e3),this._addToQueue(function(d){var e=!1,f=function(a,b){b===clippy.Animator.States.EXITED&&(e=!0,c&&c(),d())};b&&window.setTimeout($.proxy(function(){if(e)return;this._animator.exitAnimation()},this),b),this._playInternal(a,f)},this),!0):!1},show:function(a){this._hidden=!1;if(a){this._el.show(),this.resume(),this._onQueueEmpty();return}if(this._el.css("top")==="auto"||!this._el.css("left")==="auto"){var b=$(window).width()*.8,c=($(window).height()+$(document).scrollTop())*.8;this._el.css({top:c,left:b})}return this.resume(),this.play("Show")},speak:function(a,b){this._addToQueue(function(c){this._balloon.speak(c,a,b)},this)},closeBalloon:function(){this._balloon.hide()},delay:function(a){a=a||250,this._addToQueue(function(b){this._onQueueEmpty(),window.setTimeout(b,a)})},stopCurrent:function(){this._animator.exitAnimation(),this._balloon.close()},stop:function(){this._queue.clear(),this._animator.exitAnimation(),this._balloon.hide()},hasAnimation:function(a){return this._animator.hasAnimation(a)},animations:function(){return this._animator.animations()},animate:function(){var a=this.animations(),b=a[Math.floor(Math.random()*a.length)];return b.indexOf("Idle")===0?this.animate():this.play(b)},_getDirection:function(a,b){var c=this._el.offset(),d=this._el.height(),e=this._el.width(),f=c.left+e/2,g=c.top+d/2,h=g-b,i=f-a,j=Math.round(180*Math.atan2(h,i)/Math.PI);return-45<=j&&j<45?"Right":45<=j&&j<135?"Up":135<=j&&j<=180||-180<=j&&j<-135?"Left":-135<=j&&j<-45?"Down":"Top"},_onQueueEmpty:function(){if(this._hidden||this._isIdleAnimation())return;var a=this._getIdleAnimation();this._idleDfd=$.Deferred(),this._animator.showAnimation(a,$.proxy(this._onIdleComplete,this))},_onIdleComplete:function(a,b){b===clippy.Animator.States.EXITED&&this._idleDfd.resolve()},_isIdleAnimation:function(){var a=this._animator.currentAnimationName;return a&&a.indexOf("Idle")===0},_getIdleAnimation:function(){var a=this.animations(),b=[];for(var c=0;c<a.length;c++){var d=a[c];d.indexOf("Idle")===0&&b.push(d)}var e=Math.floor(Math.random()*b.length);return b[e]},_setupEvents:function(){$(window).on("resize",$.proxy(this.reposition,this)),this._el.on("mousedown",$.proxy(this._onMouseDown,this)),this._el.on("dblclick",$.proxy(this._onDoubleClick,this))},_onDoubleClick:function(){this.play("ClickedOn")||this.animate()},reposition:function(){if(!this._el.is(":visible"))return;var a=this._el.offset(),b=this._el.outerHeight(),c=this._el.outerWidth(),d=$(window).width(),e=$(window).height(),f=$(window).scrollTop(),g=$(window).scrollLeft(),h=a.top-f,i=a.left-g,j=5;h-j<0?h=j:h+b+j>e&&(h=e-b-j),i-j<0?i=j:i+c+j>d&&(i=d-c-j),this._el.css({left:i,top:h}),t