OLD | NEW |
---|---|
(Empty) | |
1 // Namespace for the share_dialog mock. | |
2 var share_dialog = {}; | |
yoshiki
2013/08/06 01:17:01
Name of namespace should be camelCase.
mtomasz
2013/08/06 02:02:35
Done.
| |
3 | |
4 /** | |
5 * Origin of Files.app. | |
6 * @type {string} | |
7 * @const | |
8 */ | |
9 share_dialog.EMBEDDER_ORIGIN = | |
10 'chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj'; | |
11 | |
12 /** | |
13 * Target width of the sharing window in pixels. | |
14 * @type {number} | |
15 * @const | |
16 */ | |
17 share_dialog.TARGET_WIDTH = 350; | |
18 | |
19 /** | |
20 * Target height of the sharing window in pixels. | |
21 * @type {number} | |
22 * @const | |
23 */ | |
24 share_dialog.TARGET_HEIGHT = 250; | |
25 | |
26 /** | |
27 * Target window of Files.app. Used to communicate over messages. Filled out | |
28 * once the first message from the embedder arrives. | |
29 * @type {Window} | |
30 */ | |
31 share_dialog.embedderTarget = null; | |
32 | |
33 /** | |
34 * List of pending messages enqueued to be sent before establishing the target. | |
35 * @type {Array.<Object>} | |
36 */ | |
37 share_dialog.pendingMessages = []; | |
38 | |
39 /** | |
40 * Sends a message to the embedder. If the embedder target is not available, | |
41 * then enqueues them. Such enqueued messages will be sent as soon as the target | |
42 * is available. | |
43 * | |
44 * @param {string} type Message identifier | |
45 * @param {Object=} opt_args Arguments for the message. | |
46 * @private | |
47 */ | |
48 share_dialog.sendMessage_ = function(type, opt_args) { | |
49 if (!share_dialog.embedderTarget) { | |
50 share_dialog.pendingMessages.push({type: type, args: opt_args}); | |
51 return; | |
52 } | |
53 | |
54 var data = {}; | |
55 data.type = type; | |
56 if (opt_args) | |
57 data.args = args; | |
yoshiki
2013/08/06 01:17:01
opt_args?
mtomasz
2013/08/06 02:02:35
Oops! Done.
| |
58 | |
59 share_dialog.embedderTarget.postMessage(JSON.stringify(data), | |
60 share_dialog.EMBEDDER_ORIGIN); | |
yoshiki
2013/08/06 01:17:01
nit: indentation
mtomasz
2013/08/06 02:02:35
Done.
| |
61 }; | |
62 | |
63 /** | |
64 * Handles a request from the embedder to make the body visible. | |
65 * @private | |
66 */ | |
67 share_dialog.onMakeBodyVisible_ = function() { | |
68 document.body.style.display = ''; | |
69 }; | |
70 | |
71 /** | |
72 * Handles an event from the embedder than preparation to show the contents | |
73 * is done. | |
74 * @private | |
75 */ | |
76 share_dialog.onPrepareComplete_ = function() { | |
77 share_dialog.resize(); | |
78 }; | |
79 | |
80 /** | |
81 * Handles an event from the embedder than preparation resize the window is | |
82 * done. | |
83 * @private | |
84 */ | |
85 share_dialog.onResizeComplete_ = function() { | |
86 var container = document.querySelector('#container'); | |
87 container.style.width = share_dialog.TARGET_WIDTH + 'px'; | |
88 container.style.height = share_dialog.TARGET_HEIGHT + 'px'; | |
89 }; | |
90 | |
91 /** | |
92 * Prepares the embedder to make the contents visible. | |
93 */ | |
94 share_dialog.prepareForVisible = function() { | |
95 share_dialog.sendMessage_('prepareForVisible'); | |
96 }; | |
97 | |
98 /** | |
99 * Resizes the embedder to the content window dimensions. | |
100 */ | |
101 share_dialog.resize = function() { | |
102 share_dialog.sendMessage_('prepareForResize'); | |
103 }; | |
104 | |
105 /** | |
106 * Handles messages sent by the embedder. If it is the first message, then | |
107 * the target is established and all enqueued messages to be sent to the | |
108 * embedder are sent before handling the message from the embedder. | |
109 * | |
110 * @param {Event} message Message event. | |
111 * @private | |
112 */ | |
113 share_dialog.onMessage_ = function(message) { | |
114 if (message.origin != share_dialog.EMBEDDER_ORIGIN) | |
115 return; | |
116 | |
117 if (!share_dialog.embedderTarget) { | |
118 share_dialog.embedderTarget = message.source; | |
119 for (var i = 0; i < share_dialog.pendingMessages.length; i++) { | |
120 share_dialog.sendMessage_(share_dialog.pendingMessages[i].type, | |
121 share_dialog.pendingMessages[i].args); | |
yoshiki
2013/08/06 01:17:01
nit: indentation is off.
mtomasz
2013/08/06 02:02:35
Done.
| |
122 } | |
123 share_dialog.pendingMessages = []; | |
124 } | |
125 | |
126 var packet = JSON.parse(message.data) | |
127 var type = packet.type; | |
128 var args = packet.args; | |
129 | |
130 switch (type) { | |
131 case 'makeBodyVisible': | |
132 share_dialog.onMakeBodyVisible_(args); | |
133 break; | |
134 case 'prepareComplete': | |
135 share_dialog.onPrepareComplete_(args); | |
136 break; | |
137 case 'resizeComplete': | |
138 share_dialog.onResizeComplete_(args); | |
139 break; | |
140 } | |
141 }; | |
142 | |
143 /** | |
144 * Initializes the mocked share dialog. | |
145 */ | |
146 share_dialog.initialize = function() { | |
147 window.addEventListener('message', share_dialog.onMessage_); | |
148 share_dialog.prepareForVisible(); | |
149 }; | |
150 | |
151 window.addEventListener('load', share_dialog.initialize); | |
OLD | NEW |