/*
 * Copyright 2011 Jonathan Anderson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.footlights.ui.web.ajax;

import me.footlights.core.Footlights;
import me.footlights.ui.web.Request;


/** The global context - code sent here has full DOM access. */
class GlobalContext extends Context
{
	GlobalContext(final Footlights footlights)
	{
		super("global");

		register("initialize", new Initializer());
		register("load_plugin", new PluginLoader(footlights));
		register("reset", new AjaxHandler() {
			@Override
			public JavaScript service(Request request)
			{
				while(footlights.plugins().size() > 0)
					footlights.unloadPlugin(
						footlights.plugins().iterator().next());

				return new JavaScript().append("window.location.reload()");
			}
		});
		
		
		/*
			if(request.path().equals("/cajole"))
			{
				type = "code";
				context = "to-be-cajoled";

				InputStream in = getClass().getResourceAsStream("content/sandbox.js");

				final char[] buffer = new char[2048];
				Reader reader = new InputStreamReader(in);
				int bytes;
				do {
				  bytes = reader.read(buffer, 0, buffer.length);
				  if (bytes > 0) content.append(buffer, 0, bytes);
				} while (bytes >= 0);

				content.append("");
				/*
				content.append("var sandbox = document.createElement('div');");
				content.append("sandbox.className = 'sandbox';");
				content.append("sandbox.width = 400;");
				content.append("document.getElementById('content').appendChild(sandbox);");
				content.append("retrieveAndRunModule('sandbox.js', 'sandboxed', sandbox, log);");
				content.append("};");
		 */
	}
}

@@ -1282,458 +1282,191 @@
 %7D);%0A
-%09%09
 %0A%09%09
-%0A%09%09/*%0A%09%09%09if(request.path().equals
+register
 (%22
-/
 cajole%22
-))%0A%09%09%09%7B%0A%09%09%09%09type = %22code%22;%0A%09%09%09%09context = %22to-be-cajoled%22;%0A%0A%09%09%09%09InputStream in = getClass().getResourceAsStream(%22content/sandbox.js%22);%0A%0A%09%09%09%09final char%5B%5D buffer = new char%5B2048%5D;%0A%09%09%09%09Reader reader = new InputStreamReader(in);%0A%09%09%09%09int bytes;
+, new AjaxHandler()%0A%09%09%09%7B%0A%09%09%09%09@Override public JavaScript service(Request request) throws Throwable
 %0A%09%09%09%09
-do 
 %7B%0A%09%09%09%09
-  bytes = reader.read(buffer, 0, buffer.length);%0A%09%09%09%09  if (bytes %3E 0) content.append(buffer, 0, bytes);%0A%09%09%09%09%7D while (bytes %3E= 0);%0A%0A
+%09JavaScript code = new JavaScript();%0A%09
 %09%09%09%09co
-ntent
+de
 .append(
 %22%22);
@@ -1465,197 +1465,107 @@
 end(
-%22%22);
 %0A%09%09%09%09
-/*%0A%09%09%09%09content.append(%22var sandbox = document.createElement('div');%22);%0A%09%09%09%09content.append(%22sandbox.className = 'sandbox';%22);%0A%09%09%09%09content.append(%22sandbox.width = 4
+%09%09%22var sandbox = sandboxes.getOrCreate('sandbox', rootContext, 0, 0, 200, 2
 00
+)
 ;%22);%0A
+//%09
 %09%09%09%09co
-ntent
+de
 .app
@@ -1573,185 +1573,79 @@
 nd(%22
-document.getElementById('content').appendChild(sandbox);
+sandbox.ajax('content/sandboxed.js')
 %22);%0A
+%0A
 %09%09%09%09
-content.append(%22retrieveAndRunModule('sandbox.js', 'sandboxed', sandbox, log);%22);%0A%09%09%09%09content.append(%22%7D;%22);%0A%09%09 */
+%09return code;%0A%09%09%09%09%7D%0A%09%09%09%7D);
 %0A%09%7D%0A

Implement Cajole button.