summaryrefslogtreecommitdiffstats
path: root/index.js
blob: 7bdf1e583bc125862423d0f002ae4e8c329223d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var ERR = require("async-stacktrace");
var eejs = require('ep_etherpad-lite/node/eejs/');
var padMessage = require("ep_etherpad-lite/node/handler/PadMessageHandler.js");
var padManager = require("ep_etherpad-lite/node/db/PadManager.js");

exports.clientVars = function(hook, context, callback)
{
  var globalViewInfo = {};

  if (context.hasOwnProperty("pad")) {
    var pad = context.pad;
    if (pad.hasOwnProperty("globalViewInfo")) {
      globalViewInfo = pad.globalViewInfo;
    }
  }  

  return callback({ "globalViewInfo": globalViewInfo });
};

exports.eejsBlock_globalSettings = function (hook_name, args, cb) {
  args.content = args.content + eejs.require("ep_global_view/templates/globalSettings.ejs");
  return cb();
}

exports.eejsBlock_styles = function (hook_name, args, cb) {
  args.content = args.content + eejs.require("ep_global_view/templates/globalSettings_styles.ejs");
  return cb();
}

exports.handleMessage = function(hook_name, context, cb) {
  if ( context.message.type == "COLLABROOM" && context.message.data.type == "GLOBAL_VIEW") {
    var client = context.client;
    var session = padMessage.sessioninfos[client.id];

    padManager.getPad(session.padId, function(err, pad) {
      if (ERR(err)) return cb();

      var globalViewInfo = {};
      if (pad.hasOwnProperty("globalViewInfo")) {
          globalViewInfo = pad.globalViewInfo;
      }

      for (key in context.message.data.data) {
        globalViewInfo[key] = context.message.data.data[key];
      }
      pad.globalViewInfo = globalViewInfo;
      pad.saveToDatabase();

      var message = {
        "type": "COLLABROOM",
        "data": {
          type: "GLOABL_VIEW",
          globalViewInfo: globalViewInfo
        }
      };

      client.broadcast.to(session.padId).json.send(message);
      return cb([null]);
    });
  }
  else {
    return cb();
  }
};