noalyss Version-9
window_ext.js
Go to the documentation of this file.
1// Copyright (c) 2006 Sébastien Gruhier (http://xilinus.com, http://itseb.com)
2// YOU MUST INCLUDE window.js BEFORE
3//
4// Object to store hide/show windows status in a cookie
5// Just add at the end of your HTML file this javascript line: WindowStore.init()
6WindowStore = {
7 doSetCookie: false,
8 cookieName: "__window_store__",
9 expired: null,
10
11 // Init function with two optional parameters
12 // - cookieName (default = __window_store__)
13 // - expiration date (default 3 years from now)
14 init: function(cookieName, expired) {
15 WindowStore.cookieName = cookieName || WindowStore.cookieName
16
17 if (! expired) {
18 var today = new Date();
19 today.setYear(today.getYear()+1903);
20 WindowStore.expired = today;
21 }
22 else
23 WindowStore.expired = expired;
24
25 Windows.windows.each(function(win) {
26 win.setCookie(win.getId(), WindowStore.expired);
27 });
28
29 // Create observer on show/hide events
30 var myObserver = {
31 onShow: function(eventName, win) {
32 WindowStore._saveCookie();
33 },
34
35 onClose: function(eventName, win) {
36 WindowStore._saveCookie();
37 },
38
39 onHide: function(eventName, win) {
40 WindowStore._saveCookie();
41 }
42 }
43 Windows.addObserver(myObserver);
44
45 WindowStore._restoreWindows();
46 WindowStore._saveCookie();
47 },
48
49 show: function(win) {
50 eval("var cookie = " + WindowUtilities.getCookie(WindowStore.cookieName));
51 if (cookie != null) {
52 if (cookie[win.getId()])
53 win.show();
54 }
55 else
56 win.show();
57 },
58
59 // Function to store windows show/hide status in a cookie
60 _saveCookie: function() {
61 if (!doSetCookie)
62 return;
63
64 var cookieValue = "{";
65 Windows.windows.each(function(win) {
66 if (cookieValue != "{")
67 cookieValue += ","
68 cookieValue += win.getId() + ": " + win.isVisible();
69 });
70 cookieValue += "}"
71
72 WindowUtilities.setCookie(cookieValue, [WindowStore.cookieName, WindowStore.expired]);
73 },
74
75 // Function to restore windows show/hide status from a cookie if exists
76 _restoreWindows: function() {
77 eval("var cookie = " + WindowUtilities.getCookie(WindowStore.cookieName));
78 if (cookie != null) {
79 doSetCookie = false;
80 Windows.windows.each(function(win) {
81 if (cookie[win.getId()])
82 win.show();
83 });
84 }
85 doSetCookie = true;
86 }
87}
88
89// Object to set a close key an all windows
90WindowCloseKey = {
91 keyCode: Event.KEY_ESC,
92
93 init: function(keyCode) {
94 if (keyCode)
95 WindowCloseKey.keyCode = keyCode;
96
97 Event.observe(document, 'keydown', this._closeCurrentWindow.bindAsEventListener(this));
98 },
99
100 _closeCurrentWindow: function(event) {
101 var e = event || window.event
102 var characterCode = e.which || e.keyCode;
103
104 // Check if there is a top window (it means it's an URL content)
105 var win = top.Windows.focusedWindow;
106 if (characterCode == WindowCloseKey.keyCode && win) {
107 if (win.cancelCallback)
108 top.Dialog.cancelCallback();
109 else if (win.okCallback)
110 top.Dialog.okCallback();
111 else
112 top.Windows.close(top.Windows.focusedWindow.getId());
113 }
114 }
115}