<commit_before>/**
 Copyright (C) 2013 by <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
/// <reference path="../../../types/angularjs/angular.d.ts"/>
/// <reference path="../../../source/game/gameWorld.ts"/>
/// <reference path="./gameService.ts"/>

module pow2.ui {

   export interface IPowAlertObject {
      message:string;
      duration?:number;
      done?(message:IPowAlertObject);
   }

   export interface IAlertScopeService extends ng.IRootScopeService {
      powAlert:IPowAlertObject;
   }

   export interface IPowAlertService {
      show(message:string):IPowAlertObject;
      queue(config:IPowAlertObject);
   }

   /**
    * Provide a basic service for queuing and showing messages to the user.
    */
   export class PowAlertService extends pow2.Events implements pow2.IWorldObject, pow2.IProcessObject, IPowAlertService {
      scope:IAlertScopeService;
      timeout:ng.ITimeoutService;
      game:PowGameService;
      world:pow2.GameWorld;
      element:ng.IAugmentedJQuery;
      container:ng.IAugmentedJQuery;
      animate:any;
      id:number = _.uniqueId();

      private _current:IPowAlertObject = null;
      private _queue:IPowAlertObject[] = [];
      constructor(element,container,scope:IAlertScopeService,timeout:ng.ITimeoutService,game:PowGameService,animate:any){
         super();
         this.scope = scope;
         this.timeout = timeout;
         this.game = game;
         this.element = element;
         this.container = container;
         this.animate = animate;

         game.world.mark(this);
         game.world.time.addObject(this);
      }

      destroy() {
         this.game.world.time.removeObject(this);
         this.game.world.erase(this);
      }

      show(message:string,done?:() => void,duration?:number):IPowAlertObject{
         var obj:IPowAlertObject = {
            message:message,
            duration: typeof duration === 'undefined' ? 1000 : duration,
            done:done
         };
         this._queue.push(obj);
         return obj;
      }

      queue(config:IPowAlertObject){
         this._queue.push(config);
      }

      /*
       * Update current message, and manage event generation for transitions
       * between messages.
       * @param elapsed number The elapsed time since the last invocation, in milliseconds.
       */
      processFrame(elapsed:number) {
         if(this._current || this._queue.length === 0){
            return;
         }
         this._current = this._queue.shift();
         this.scope.$apply(() => {
            this.scope.powAlert = this._current;
            this.animate.enter(this.element, this.container, null,() => {
               this.timeout(() => {
                  this.animate.leave(this.element, () => {
                     this._current.done && this._current.done(this._current);
                     this.scope.powAlert = this._current = null;
                  });
               },this._current.duration);
            });
         });
      }
   }
   app.factory('powAlert', [
      '$rootScope',
      '$timeout',
      'game',
      '$compile',
      '$document',
      '$animate',<commit_msg>Update Alert service to support click dismissing alerts.  - This can happen before a non-confirm message has timed out naturally (to speed things up), or is required if no duration is specified.<commit_after>/**
 Copyright (C) 2013 by <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
/// <reference path="../../../types/angularjs/angular.d.ts"/>
/// <reference path="../../../source/game/gameWorld.ts"/>
/// <reference path="./gameService.ts"/>

module pow2.ui {

   export interface IPowAlertObject {
      message:string; // The message to display.
      duration?:number; // The duration the message should be displayed after shown.
      elapsed?:number; // Elapsed time since the alert has been full shown (after any enter animations)
      dismissed?:boolean; // Set to dismiss.
      busy?:boolean; // Set to ignore all processing for this alert.
      done?(message:IPowAlertObject); // A callback for after this alert has been issued and dismissed.
   }

   export interface IAlertScopeService extends ng.IRootScopeService {
      powAlert:IPowAlertObject;
   }

   export interface IPowAlertService {
      show(message:string):IPowAlertObject;
      queue(config:IPowAlertObject);
   }

   /**
    * Provide a basic service for queuing and showing messages to the user.
    */
   export class PowAlertService extends pow2.Events implements pow2.IWorldObject, pow2.IProcessObject, IPowAlertService {
      scope:IAlertScopeService;
      timeout:ng.ITimeoutService;
      game:PowGameService;
      world:pow2.GameWorld;
      element:ng.IAugmentedJQuery;
      container:ng.IAugmentedJQuery;
      animate:any;
      id:number = _.uniqueId();

      private _current:IPowAlertObject = null;
      private _queue:IPowAlertObject[] = [];
      constructor(element,container,scope:IAlertScopeService,timeout:ng.ITimeoutService,game:PowGameService,animate:any){
         super();
         this.scope = scope;
         this.timeout = timeout;
         this.game = game;
         this.element = element;
         this.container = container;
         this.animate = animate;

         this.container.on('click',(e) => {
            this.dismiss();
         });

         game.world.mark(this);
         game.world.time.addObject(this);
      }

      destroy() {
         this.game.world.time.removeObject(this);
         this.game.world.erase(this);
      }

      dismiss() {
         if(this._current){
            this._current.dismissed = true;
         }
      }
      show(message:string,done?:() => void,duration?:number):IPowAlertObject{
         var obj:IPowAlertObject = {
            message:message,
            duration: typeof duration === 'undefined' ? 1000 : duration,
            done:done
         };
         return this.queue(obj);
      }

      queue(config:IPowAlertObject){
         config.elapsed = 0;
         this._queue.push(config);
         return config;
      }

      /*
       * Update current message, and manage event generation for transitions
       * between messages.
       * @param elapsed number The elapsed time since the last invocation, in milliseconds.
       */
      processFrame(elapsed:number) {
         if(this._current && this._current.busy !== true){
            var c = this._current;
            var timeout:boolean = c.duration && c.elapsed > c.duration;
            var dismissed:boolean = c.dismissed === true;
            if(!timeout && !dismissed){
               c.elapsed += elapsed;
               return;
            }
            c.busy = true;
            this.scope.$apply(() => {
               this.animate.leave(this.element, () => {
                  this._current.done && this._current.done(this._current);
                  this.scope.powAlert = this._current = null;
               });
            });
         }
         if(this._queue.length === 0){
            return;
         }
         var newAlert = this._queue.shift();
         this.scope.$apply(() => {
            this.scope.powAlert = newAlert;
            this.animate.enter(this.element, this.container, null,() => {
               this._current = newAlert;
            });
         });
      }
   }
   app.factory('powAlert', [
      '$rootScope',
      '$timeout',
      'game',
      '$compile',
      '$document',
      '$animate',
{'fraction_non_alphanumeric': 0.11374182034503272, 'fraction_numerical': 0.004402141582391434, 'mean_word_length': 2.2644660194174757, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '108874', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2192, 'n_words': 574}