adding all weblabels from weblabels.fsf.org
[weblabels.fsf.org.git] / etherpad.fsf.org / 20120516 / files / AttributePoolFactory.js
CommitLineData
5a920362 1/**
2 * This code represents the Attribute Pool Object of the original Etherpad.
3 * 90% of the code is still like in the original Etherpad
4 * Look at https://github.com/ether/pad/blob/master/infrastructure/ace/www/easysync2.js
5 * You can find a explanation what a attribute pool is here:
6 * https://github.com/Pita/etherpad-lite/blob/master/doc/easysync/easysync-notes.txt
7 */
8
9/*
10 * Copyright 2009 Google Inc., 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS-IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25exports.createAttributePool = function () {
26 var p = {};
27 p.numToAttrib = {}; // e.g. {0: ['foo','bar']}
28 p.attribToNum = {}; // e.g. {'foo,bar': 0}
29 p.nextNum = 0;
30
31 p.putAttrib = function (attrib, dontAddIfAbsent) {
32 var str = String(attrib);
33 if (str in p.attribToNum) {
34 return p.attribToNum[str];
35 }
36 if (dontAddIfAbsent) {
37 return -1;
38 }
39 var num = p.nextNum++;
40 p.attribToNum[str] = num;
41 p.numToAttrib[num] = [String(attrib[0] || ''), String(attrib[1] || '')];
42 return num;
43 };
44
45 p.getAttrib = function (num) {
46 var pair = p.numToAttrib[num];
47 if (!pair) {
48 return pair;
49 }
50 return [pair[0], pair[1]]; // return a mutable copy
51 };
52
53 p.getAttribKey = function (num) {
54 var pair = p.numToAttrib[num];
55 if (!pair) return '';
56 return pair[0];
57 };
58
59 p.getAttribValue = function (num) {
60 var pair = p.numToAttrib[num];
61 if (!pair) return '';
62 return pair[1];
63 };
64
65 p.eachAttrib = function (func) {
66 for (var n in p.numToAttrib) {
67 var pair = p.numToAttrib[n];
68 func(pair[0], pair[1]);
69 }
70 };
71
72 p.toJsonable = function () {
73 return {
74 numToAttrib: p.numToAttrib,
75 nextNum: p.nextNum
76 };
77 };
78
79 p.fromJsonable = function (obj) {
80 p.numToAttrib = obj.numToAttrib;
81 p.nextNum = obj.nextNum;
82 p.attribToNum = {};
83 for (var n in p.numToAttrib) {
84 p.attribToNum[String(p.numToAttrib[n])] = Number(n);
85 }
86 return p;
87 };
88
89 return p;
90}