<commit_before>/*
 * Copyright (C) 2015 iWARES Solution Provider
 *
 * 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.
 *
 */

/**
 * @file	modules/interaction.js
 * @author	Eric.Tsai
 *
 */

// Base class of staple interaction.
define('staple/interaction', function (require, exports, module) {

var SuperClass = require('staple/object');
var InteractionManager = require('staple/interaction-manager');
var HTMLParser = require('staple/html-parser');
var PeriodicalTask = require('staple/periodical-task');

var OverlayManager = Class.create({

	initialize : function () {
		this.panel = window.document.createElement('article');
		this.panel.id = 'overlay';
		this.updater = new PeriodicalTask(100, false, this);
	},

	resume : function () {
		window.document.body.appendChild(this.panel);
		this.updater.start(true);
	},

	pause : function () {
		this.updater.stop();
		window.document.body.removeChild(this.panel);
	},

	run : function () {
		var panel = this.panel;

		var dialogs = panel.querySelectorAll("article#overlay>div.dim");
		for (var i = 0, dialog; dialog = dialogs[i]; ++i)
			dialog.classList.remove('active');
		if (dialogs.length)
			dialogs[dialogs.length - 1].classList.add('active');

		if (panel.childElementCount === 0)
			panel.classList.remove('active');
		else
			panel.classList.add('active');
	},

	attach : function (overlay) {
		this.panel.appendChild(overlay.$attrs.frame);
		this.updater.start(true);
	},

	detach : function (overlay) {
		this.panel.removeChild(overlay.$attrs.frame);
		this.run();
	},

	handleBackPressed : function () {
		var target = this.panel.lastElementChild;
		if (!target)
			return false;
		target.handleBackPressed();
		return true;
	},

});

return Class.create(SuperClass, {

	create : function (state) {
		var attrs = this.$attrs;
		attrs.active = false;
		attrs.tasks = {};
		attrs.overlayManager = new OverlayManager();

		attrs.creating = true;
		this.invokeMethodAndEnsureSuperCalled('onCreate', state);
		delete attrs.creating;

		// Delegate click event.
		this.$attrs.root.onclick = (function (event) {
			var handler = this[event.target.getAttribute('on-click')];
			if (!Object.isFunction(handler))
				return;
			handler.call(this, event.target);
		}).bind(this);
	},

	onCreate : function (state) {<commit_msg>fix issue that child element breaks "on-click" event delegate.<commit_after>/*
 * Copyright (C) 2015 iWARES Solution Provider
 *
 * 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.
 *
 */

/**
 * @file	modules/interaction.js
 * @author	Eric.Tsai
 *
 */

// Base class of staple interaction.
define('staple/interaction', function (require, exports, module) {

var SuperClass = require('staple/object');
var InteractionManager = require('staple/interaction-manager');
var HTMLParser = require('staple/html-parser');
var PeriodicalTask = require('staple/periodical-task');

var OverlayManager = Class.create({

	initialize : function () {
		this.panel = window.document.createElement('article');
		this.panel.id = 'overlay';
		this.updater = new PeriodicalTask(100, false, this);
	},

	resume : function () {
		window.document.body.appendChild(this.panel);
		this.updater.start(true);
	},

	pause : function () {
		this.updater.stop();
		window.document.body.removeChild(this.panel);
	},

	run : function () {
		var panel = this.panel;

		var dialogs = panel.querySelectorAll("article#overlay>div.dim");
		for (var i = 0, dialog; dialog = dialogs[i]; ++i)
			dialog.classList.remove('active');
		if (dialogs.length)
			dialogs[dialogs.length - 1].classList.add('active');

		if (panel.childElementCount === 0)
			panel.classList.remove('active');
		else
			panel.classList.add('active');
	},

	attach : function (overlay) {
		this.panel.appendChild(overlay.$attrs.frame);
		this.updater.start(true);
	},

	detach : function (overlay) {
		this.panel.removeChild(overlay.$attrs.frame);
		this.run();
	},

	handleBackPressed : function () {
		var target = this.panel.lastElementChild;
		if (!target)
			return false;
		target.handleBackPressed();
		return true;
	},

});

return Class.create(SuperClass, {

	create : function (state) {
		var attrs = this.$attrs;
		attrs.active = false;
		attrs.tasks = {};
		attrs.overlayManager = new OverlayManager();

		attrs.creating = true;
		this.invokeMethodAndEnsureSuperCalled('onCreate', state);
		delete attrs.creating;

		// Delegate click event.
		this.$attrs.root.onclick = (function (event) {
			var target = event.target;
			while (target) {
				var handler = this[target.getAttribute('on-click')];
				if (Object.isFunction(handler)) {
					handler.call(this, event.target);
					break;
				}
				target = target.parentElement;
			}
		}).bind(this);
	},

	onCreate : function (state) {
{'fraction_non_alphanumeric': 0.1376659678546471, 'fraction_numerical': 0.004891684136967156, 'mean_word_length': 4.0087489063867014, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1269431', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1748, 'n_words': 455}