      1 /**
      2  * 2011 Peter 'Pita' Martischka
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS-IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16
     17
     18 /**
     19  * The AuthorManager controlls all information about the Pad authors
     20  */
     21
     22 /**
     23  * Saves all Authors as a assoative Array. The Key is the author id.
     24  * Authors can have the following attributes:
     25  * -name The Name of the Author as shown on the Pad
     26  * -colorId The Id of Usercolor. A number between 0 and 31
     27  * -timestamp The timestamp on which the user was last seen
     28  */
     29 var globalAuthors = {};
     30
     31 /**
     32  * A easy key value pair. The Key is the token, the value is the authorid
     33  */
     34 var token2author = {};
     35
     36 /**
     37  * Returns the Author Id for a token. If the token is unkown,
     38  * it creates a author for the token
     39  * @param token The token
     40  */
     41 exports.getAuthor4Token = function (token)
     42 {
     43   var author;
     44
     45   if(token2author[token] == null)
     46   {
     47     author = "g." + _randomString(16);
     48
     49     while(globalAuthors[author] != null)
     50     {
     51       author = "g." + _randomString(16);
     52     }
     53
     54     token2author[token]=author;
     55
     56     globalAuthors[author] = {};
     57     globalAuthors[author].colorId = Math.floor(Math.random()*32);
     58     globalAuthors[author].name = null;
     59   }
     60   else
     61   {
     62     author = token2author[token];
     63   }
     64
     65   globalAuthors[author].timestamp = new Date().getTime();
     66
     67   return author;
     68 }
     69
     70 /**
     71  * Returns the color Id of the author
     72  */
     73 exports.getAuthorColorId = function (author)
     74 {
     75   throwExceptionIfAuthorNotExist(author);
     76
     77   return globalAuthors[author].colorId;
     78 }
     79
     80 /**
     81  * Sets the color Id of the author
     82  */
     83 exports.setAuthorColorId = function (author, colorId)
     84 {
     85   throwExceptionIfAuthorNotExist(author);
     86
     87   globalAuthors[author].colorId = colorId;
     88 }
     89
     90 /**
     91  * Returns the name of the author
     92  */
     93 exports.getAuthorName = function (author)
     94 {
     95   throwExceptionIfAuthorNotExist(author);
     96
     97   return globalAuthors[author].name;
     98 }
     99
    100 /**
    101  * Sets the name of the author
    102  */
    103 exports.setAuthorName = function (author, name)
    104 {
    105   throwExceptionIfAuthorNotExist(author);
    106
    107   globalAuthors[author].name = name;
    108 }
    109
    110 /**
    111  * A internal function that checks if the Author exist and throws a exception if not
    112  */
    113 function throwExceptionIfAuthorNotExist(author)
    114 {
    115   if(globalAuthors[author] == null)
    116   {
    117     throw "Author '" + author + "' is unkown!";
    118   }
    119 }
    120
    121 /**
    122  * Generates a random String with the given length. Is needed to generate the Author Ids
    123  */
    124 function _randomString(len) {
    125   // use only numbers and lowercase letters
    126   var pieces = [];
    127   for(var i=0;i<len;i++) {
    128     pieces.push(Math.floor(Math.random()*36).toString(36).slice(-1));
    129   }
    130   return pieces.join('');
    131 }
    132