<commit_before>/*
 * Copyright 2011 <NAME>
 *
 * 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;

import scala.collection.mutable.HashMap

import _root_.me.footlights.core.Footlights;
import _root_.me.footlights.plugin.AjaxHandler;
import _root_.me.footlights.plugin.JavaScript;
import _root_.me.footlights.plugin.WebRequest;



/** Acts as an Ajax server for the JavaScript client */
class AjaxServer(footlights:Footlights) extends WebServer
{
	val pluginAjaxHandlers = new HashMap[String, AjaxHandler]
	val globalContext = new GlobalContext(footlights, this)

	override def name:String = "Ajax"

	override def handle(request:WebRequest) = {
		val js =
			request.prefix() match {
				case "global" => globalContext.service(request.shift())
				case "plugin" => {
					val pluginRequest = request.shift()

					pluginAjaxHandlers.get(pluginRequest.prefix())
						.getOrElse(throw new IllegalArgumentException(
							"No such plugin '" + pluginRequest.prefix() + "'"))
						.service(pluginRequest.shift())
				}
				case _:String => new JavaScript()
			}

		Response.newBuilder()
			.setResponse("text/javascript",
				new java.io.ByteArrayInputStream(js.exec().getBytes()))
			.build()
	}


	def reset = pluginAjaxHandlers.clear();
	def register(name:String, pluginHandler:AjaxHandler)
	{
		if (pluginAjaxHandlers.contains(name))
			throw new RuntimeException(name + " already registered");

		pluginAjaxHandlers += name -> pluginHandler
	}
}



/** The global context - code sent here has full DOM access. */
class GlobalContext(footlights:Footlights, server:AjaxServer)
	extends AjaxHandler
{
	override def service(request:WebRequest) = {
		request.path() match {
			case "init" => {
				server.reset

				new JavaScript()
					.append("""
var buttons = document.getElementById('buttons');
buttons.innerHTML='';""")

					.append(button("Good Plugin", JavaScript.ajax("load_plugin/" + GOOD_PLUGIN)))
					.append(button("Wicked Plugin", JavaScript.ajax("load_plugin/" + WICKED_PLUGIN)))
					.append(button("Tic-Tac-Toe", JavaScript.ajax("load_plugin/" + TICTACTOE)))

					.append(button("Reset", JavaScript.ajax("reset")))

					.append(JavaScript.ajax("load_plugin/" + TICTACTOE))
					/*
					.append(ajax("load_plugin/" + GOOD_PLUGIN))
					.append(ajax("load_plugin/" + WICKED_PLUGIN))
					 */

					.append("console.log('UI Initialized');")
			}

			case "reset" => {
				while (footlights.plugins().size() > 0)<commit_msg>Don't load any plugins by default.<commit_after>/*
 * Copyright 2011 <NAME>
 *
 * 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;

import scala.collection.mutable.HashMap

import _root_.me.footlights.core.Footlights;
import _root_.me.footlights.plugin.AjaxHandler;
import _root_.me.footlights.plugin.JavaScript;
import _root_.me.footlights.plugin.WebRequest;



/** Acts as an Ajax server for the JavaScript client */
class AjaxServer(footlights:Footlights) extends WebServer
{
	val pluginAjaxHandlers = new HashMap[String, AjaxHandler]
	val globalContext = new GlobalContext(footlights, this)

	override def name:String = "Ajax"

	override def handle(request:WebRequest) = {
		val js =
			request.prefix() match {
				case "global" => globalContext.service(request.shift())
				case "plugin" => {
					val pluginRequest = request.shift()

					pluginAjaxHandlers.get(pluginRequest.prefix())
						.getOrElse(throw new IllegalArgumentException(
							"No such plugin '" + pluginRequest.prefix() + "'"))
						.service(pluginRequest.shift())
				}
				case _:String => new JavaScript()
			}

		Response.newBuilder()
			.setResponse("text/javascript",
				new java.io.ByteArrayInputStream(js.exec().getBytes()))
			.build()
	}


	def reset = pluginAjaxHandlers.clear();
	def register(name:String, pluginHandler:AjaxHandler)
	{
		if (pluginAjaxHandlers.contains(name))
			throw new RuntimeException(name + " already registered");

		pluginAjaxHandlers += name -> pluginHandler
	}
}



/** The global context - code sent here has full DOM access. */
class GlobalContext(footlights:Footlights, server:AjaxServer)
	extends AjaxHandler
{
	override def service(request:WebRequest) = {
		request.path() match {
			case "init" => {
				server.reset

				new JavaScript()
					.append("""
var buttons = document.getElementById('buttons');
buttons.innerHTML='';""")

					.append(button("Good Plugin", JavaScript.ajax("load_plugin/" + GOOD_PLUGIN)))
					.append(button("Wicked Plugin", JavaScript.ajax("load_plugin/" + WICKED_PLUGIN)))
					.append(button("Tic-Tac-Toe", JavaScript.ajax("load_plugin/" + TICTACTOE)))

					.append(button("Reset", JavaScript.ajax("reset")))

					.append("console.log('UI Initialized');")
			}

			case "reset" => {
				while (footlights.plugins().size() > 0)
{'fraction_non_alphanumeric': 0.125625539257981, 'fraction_numerical': 0.0031061259706643657, 'mean_word_length': 4.120141342756184, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4369358', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1661, 'n_words': 450}