summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/dev/log/WindowAppender.js
blob: e62efe4c375ed52223430c72a0e23c294c75d46e (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2006 by STZ-IDA, Germany, http://www.stz-ida.de

   License:
     LGPL 2.1: http://www.gnu.org/licenses/lgpl.html

   Authors:
     * Til Schneider (til132)

************************************************************************ */

/* ************************************************************************

#module(core)
#module(log)

************************************************************************ */

/**
 * An appender that writes all messages to a log window.
 * <p>
 * This class does not depend on qooxdoo widgets, so it also works when there
 * are problems with widgets or when the widgets are not yet initialized.
 *
 * @param name {string ? "qx_log"} the name of the log window.
 */
qx.OO.defineClass("qx.dev.log.WindowAppender", qx.dev.log.Appender,
function(name) {
  qx.dev.log.Appender.call(this);

  this._id = qx.dev.log.WindowAppender.register(this);
  this._name = (name == null) ? "qx_log" : name;

  this._logWindowOpened = false;
});


/**
 * The maximum number of messages to show. If null the number of messages is not
 * limited.
 */
qx.OO.addProperty({ name:"maxMessages", type:"number", defaultValue:500 });

/** Whether the window should appear under the main window. */
qx.OO.addProperty({ name:"popUnder", type:"boolean", defaultValue:false, allowNull:false });


/**
 * Creates and opens the log window if it doesn't alread exist.
 */
qx.Proto.openWindow = function() {
  if (this._logWindowOpened) {
    // The window is already open -> Nothing to do
    return;
  }

  // Open the logger window
  var winWidth = 600;
  var winHeight = 350;
  var winLeft = window.screen.width - winWidth;
  var winTop = window.screen.height - winHeight;
  var params = "toolbar=no,scrollbars=yes,resizable=yes,"
    + "width=" + winWidth + ",height=" + winHeight
    + ",left=" + winLeft + ",top=" + winTop;

  // NOTE: In window.open the browser will process the event queue.
  //     Which means that other log events may arrive during this time.
  //     The log window is then in an inconsistent state, because the
  //     this._logElem is not created yet. These events will be added to the
  //     this._logEventQueue and logged after this._logElem is created.
  this._logWindow = window.open("", this._name, params);

  if (!this._logWindow || this._logWindow.closed)
  {
    if (!this._popupBlockerWarning) {
      alert("Couldn't open debug window. Please disable your popup blocker!");
    }

    this._popupBlockerWarning = true;
    return;
  }

  // Seems to be OK now.
  this._popupBlockerWarning = false;

  // Store that window is open
  this._logWindowOpened = true;

  if (this.getPopUnder()) {
    this._logWindow.blur();
    window.focus();
  }

  var logDocument = this._logWindow.document;
  // NOTE: We have to use a static onunload handler, because an onunload
  //     that is set later using DOM is ignored completely.
  //     (at least in Firefox, but maybe in IE, too)
  logDocument.open();
  logDocument.write("<html><head><title>" + this._name + "</title></head>"
    + '<body onload="qx = opener.qx;" onunload="try{qx.dev.log.WindowAppender._registeredAppenders[' + this._id + '].closeWindow()}catch(e){}">'
    + '<pre id="log" wrap="wrap" style="font-size:11"></pre></body></html>');
  logDocument.close();

  this._logElem = logDocument.getElementById("log");

  // Log the events from the queue
  if (this._logEventQueue != null) {
    for (var i = 0; i < this._logEventQueue.length; i++) {
      this.appendLogEvent(this._logEventQueue[i]);
    }
    this._logEventQueue = null;
  }
}


/**
 * Closes the log window.
 */
qx.Proto.closeWindow = function() {
  if (this._logWindow != null) {
    this._logWindow.close();
    this._logWindow = null;
    this._logElem = null;
    this._logWindowOpened = false;
  }
}


// overridden
qx.Proto.appendLogEvent = function(evt) {
  if (!this._logWindowOpened) {
    this._logEventQueue = [];
    this._logEventQueue.push(evt);

    this.openWindow();

    // Popup-Blocker was active!
    if (!this._logWindowOpened) {
      return;
    }
  } else if (this._logElem == null) {
    // The window is currenlty opening, but not yet finished
    // -> Put the event in the queue
    this._logEventQueue.push(evt);
  } else {
    var divElem = this._logWindow.document.createElement("div");
    if (evt.level == qx.dev.log.Logger.LEVEL_ERROR) {
      divElem.style.backgroundColor = "#FFEEEE";
    } else if (evt.level == qx.dev.log.Logger.LEVEL_DEBUG) {
      divElem.style.color = "gray";
    }
    divElem.innerHTML = this.formatLogEvent(evt).replace(/&/g, "&amp;")
      .replace(/</g, "&lt;").replace(/  /g, " &#160;").replace(/[\n]/g, "<br>");
    this._logElem.appendChild(divElem);

    while (this._logElem.childNodes.length > this.getMaxMessages()) {
      this._logElem.removeChild(this._logElem.firstChild);

      if (this._removedMessageCount == null) {
        this._removedMessageCount = 1;
      } else {
        this._removedMessageCount++;
      }
    }

    if (this._removedMessageCount != null) {
      this._logElem.firstChild.innerHTML = "(" + this._removedMessageCount
        + " messages removed)";
    }

    // Scroll to bottom
    this._logWindow.scrollTo(0, this._logElem.offsetHeight);
  }
}


// overridden
qx.Proto.dispose = function() {
  if (this.getDisposed()) {
    return true;
  }

  this.closeWindow();

  return qx.dev.log.Appender.prototype.dispose.call(this);
}


qx.Class._nextId = 1;
qx.Class._registeredAppenders = {};


/**
 * Registers a WindowAppender. This is used by the WindowAppender internally.
 * You don't have to call this.
 *
 * @param appender {WindowAppender} the WindowAppender to register.
 * @return {int} the ID.
 */
qx.Class.register = function(appender) {
  var WindowAppender = qx.dev.log.WindowAppender;

  var id = WindowAppender._nextId++;
  WindowAppender._registeredAppenders[id] = appender;

  return id;
}


/**
 * Returns a prviously registered WindowAppender.
 *
 * @param id {int} the ID of the wanted WindowAppender.
 * @return {WindowAppender} the WindowAppender or null if no
 *     WindowAppender with this ID is registered.
 */
qx.Class.getAppender = function(id) {
  return qx.dev.log.WindowAppender._registeredAppenders[id];
}