<?php if (!defined('PmWiki')) exit();
/*  Copyright 2011 Hans Bracker.
    This file is formsplus.php; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Form markup extensions for PmWiki: adding input types
    'date', 'datetime', 'datetime-local', 'time', 'week', 'month',
    'number', 'email', 'url', 'tel', 'search', 'color', 'range',
    'frame' = 'fieldset', 'frameend' = 'fieldsetend', 'label'.
*/
$RecipeInfo['FormsPlus']['Version'] = '2017-06-16a';

## (:input ...:) directive
## modified from scripts/forms.php to include '-' character for 'type'
## (to accomodate new type='datetime-local')
Markup('input', 'directives', '/\\(:input\\s+(\\w[-\\w]+)(.*?):\\)/i', "InputPlusMarkup");
function InputPlusMarkup($m) { 
	extract($GLOBALS['MarkupToHTML']);
	return InputMarkup($pagename, $m[1], $m[2]); 
}

## new (:input ... :) types: 'date','datetime','datetime-local','time','week','month'
## 'number', 'email','url','tel','search'
foreach(array('date','datetime','datetime-local','time','week','month','number',
	'email','url','tel','search') as $t) {
	SDV($InputTags[$t][':html'], "<input type='$t' \$InputFormArgs />");
	SDV($InputTags[$t]['class'], 'inputbox');
}

## (:input range ... :)  slider control
## uses its own function to inject javascript for showing value in another element.
## The element needs to have id=<rangecontrol-id>show ('show' appended to id of range input id,
## and can be for instance an input text box or  a span element, like %id=range2show%
SDVA($InputTags['range'], array(
	':attr' => array('id', 'name', 'class', 'value', 'min', 'max', 'step'),
	'id' => array('id'),
  'class' => 'inputbox inputrange',
  ':html' => "<input type='range' \$InputFormArgs onchange=\"showInputRange(this);\"/>",
  ':fn' => 'InputRange'));

function InputRange($pagename, $type, $args) {
	global $HTMLFooterFmt;
	$HTMLFooterFmt['inputrange'] = "
	<script>
	function showInputRange(e) {
		var r = e.value;
		var n = e.id + 'show';
		document.getElementById(n).value = r;
		document.getElementById(n).innerHTML = r;
		return false;
	}
	function initInputRange() {
		var ee = document.getElementsByTagName(\"INPUT\");
		for (var i = 0; i < ee.length; i++) {
			var a = ee[i].getAttribute('type');
			if (a=='range') showInputRange(ee[i]);
		}
	}
	window.onload =  initInputRange();
	</script>";
 $html = InputToHTML($pagename, $type, $args, $opt);
	return $html;
}

## (:input color ... :)  color selector control
SDVA($InputTags['color'], array(
	':attr' => array('id', 'name', 'class', 'value'),
  'class' => 'inputbox inputcolor',
  ':html' => "<input type='color' \$InputFormArgs />"));

## (:input frame "<legendtext>":) frame with legend, i.e. HTML <fieldset>
SDVA($InputTags['frame'], array(
  ':args' => array('label', 'value', 'legend', 'class'),
  ':attr' => array('id', 'name', 'class'),
  ':content' => array('label', 'value', 'legend'),
  'class' => 'inputframe',
  ':html' => "<fieldset \$InputFormArgs><legend>\$InputFormContent</legend>"));

## (:input frameend:)
SDV($InputTags['frameend'][':html'], '</fieldset>');

SDV($InputTags['fieldset'], $InputTags['frame']);
SDV($InputTags['fieldsetend'], $InputTags['frameend']);

## (:input label "<labeltext>":)
SDVA($InputTags['label'], array(
	':args' => array('for', 'label', 'value'),
  ':attr' => array('for', 'label', 'name', 'class', 'value'),
  ':content' => array('label', 'value'),
  'class' => 'inputlabel',
  ':html' => "<label \$InputFormArgs>\$InputFormContent</label>"));