[limb-svn] r6371 - in 3.x/trunk/limb/calendar: shared/js shared/js/lang src src/template/tags
svn at limb-project.com
svn at limb-project.com
Wed Oct 3 10:02:04 MSD 2007
Author: svk
Date: 2007-10-03 10:02:04 +0400 (Wed, 03 Oct 2007)
New Revision: 6371
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6371
Added:
3.x/trunk/limb/calendar/shared/js/datewidget.js
3.x/trunk/limb/calendar/src/lmbDateWidget.class.php
3.x/trunk/limb/calendar/src/template/tags/date_select.tag.php
Modified:
3.x/trunk/limb/calendar/shared/js/lang/calendar-en.js
3.x/trunk/limb/calendar/shared/js/lang/calendar-ru.js
Log:
date_select tag
Added: 3.x/trunk/limb/calendar/shared/js/datewidget.js
===================================================================
--- 3.x/trunk/limb/calendar/shared/js/datewidget.js (rev 0)
+++ 3.x/trunk/limb/calendar/shared/js/datewidget.js 2007-10-03 06:02:04 UTC (rev 6371)
@@ -0,0 +1,223 @@
+var DATE_WIDGETS = new Object();
+function DateWidget_Init(name, show_default)
+{
+ DATE_WIDGETS[name] = new DateWidget(name, show_default);
+ DATE_WIDGETS[name].init();
+}
+
+function DateWidget_Action(name, action)
+{
+ var widget = DATE_WIDGETS[name];
+ switch (action)
+ {
+ case "handle_change":
+ DATE_WIDGETS[name].handle_change(DateWidget_Action.arguments[2]);
+ break;
+ default:
+ alert('Unknown action');
+ break;
+ }
+}
+
+function DateWidget(name, show_default)
+{
+ if (!document.getElementById(name))
+ {
+ alert('Component field missed');
+ return;
+ }
+ this.show_default = show_default;
+ this.field_id = name;
+ this.control = document.getElementById(name);
+ this.day_select = document.getElementById(name+"_day");
+ this.month_select = document.getElementById(name+"_month");
+ this.year_select = document.getElementById(name+"_year");
+ this.value = document.getElementById(name).value;
+ this.year = this.value ? this.value.substr(0,4) : 0;
+ this.month = this.value ? parseInt(this.value.substr(5,2), 10) : 0;
+ this.day = this.value ? this.value.substr(8, 2) : 0;
+}
+
+DateWidget.prototype.init = function()
+{
+ this.remove_options(this.year_select);
+ this.remove_options(this.month_select);
+ this.remove_options(this.day_select);
+ this.setup_options(this.year_select, this.year_options(), this.year);
+ this.setup_options(this.month_select, this.month_options(), this.month);
+ this.setup_options(this.day_select, this.day_options(this.year, this.month), this.day);
+ //var obj = this;
+ //this.setValue();
+ //setTimeout(function(){obj.setValue();}, 3);
+}
+
+DateWidget.prototype.setValue = function()
+{
+ this.control.value = parseInt(this.year) + "-" + this.zerofill((parseInt(this.month)), 2) + "-" + this.zerofill(parseInt(this.day), 2);
+ if (this.month_select.value === '-1' && this.day_select.value === '-1' && this.year_select.value === '-1')
+ {
+ this.control.value = "";
+ }
+ this.value = this.control.value;
+ if (!this.show_default && !this.value)
+ {
+ this.setDefaultValue();
+ }
+}
+
+DateWidget.prototype.setDefaultValue = function()
+{
+ this.day = this.day_select.value;
+ this.month = this.month_select.value;
+ this.year = this.year_select.value;
+}
+
+DateWidget.prototype.handle_change = function(field)
+{
+ switch (field)
+ {
+ case "month":
+ this.month = this.month_select.value;
+ this.init();
+ break;
+ case "day":
+ this.day = this.day_select.value;
+ this.init();
+ break;
+ case "year":
+ this.year = this.year_select.value;
+ this.init();
+ break;
+ }
+}
+
+DateWidget.prototype.year_options = function()
+{
+ var opts = new Array();
+ if (this.show_default)
+ {
+ if (Calendar._DEFOPT)
+ {
+ opts[opts.length] = {value: -1, text:Calendar._DEFOPT['year']};
+ }
+ else
+ {
+ opts[opts.length] = {value: -1, text: '---'};
+ }
+ }
+ for (var i=2000; i>=1950; i--)
+ {
+ opts[opts.length] = {value: i, text:i};
+ }
+ return opts;
+}
+
+DateWidget.prototype.month_options = function()
+{
+ var opts = new Array();
+ if (this.show_default)
+ {
+ if (Calendar._DEFOPT)
+ {
+ opts[opts.length] = {value: -1, text:Calendar._DEFOPT['month']};
+ }
+ else
+ {
+ opts[opts.length] = {value: -1, text: '---'};
+ }
+ }
+ for (var i=0; i<Calendar._MN.length; i++)
+ {
+ opts[opts.length] = {value: i+1, text: Calendar._MN[i]};
+ }
+ return opts;
+}
+
+DateWidget.prototype.day_options = function(y, m)
+{
+ m = parseInt(m);
+ var maxday;
+ var opts = new Array();
+ if (this.show_default)
+ {
+ if (Calendar._DEFOPT)
+ {
+ opts[opts.length] = {value: -1, text:Calendar._DEFOPT['day']};
+ }
+ else
+ {
+ opts[opts.length] = {value: -1, text: '---'};
+ }
+ }
+ switch (m)
+ {
+ case 1:
+ case 3:
+ case 5:
+ case 7:
+ case 8:
+ case 10:
+ case 12:
+ maxday = 31;
+ break;
+ case 4:
+ case 6:
+ case 9:
+ case 11:
+ maxday = 30;
+ break;
+ case 2:
+ maxday = y%4 == 0 ? 29 : 28;
+ break;
+ default:
+ maxday = 31;
+ break;
+ }
+
+ for (var i=1; i<=maxday; i++)
+ {
+ opts[opts.length] = {value: i, text: i};
+ }
+ return opts;
+}
+
+DateWidget.prototype.setup_options = function(elem, opts, v)
+{
+ if (!elem) return;
+ var ind = 0;
+ for (var i=0; i<opts.length; i++)
+ {
+ var opt = document.createElement("option");
+ opt.value = opts[i].value;
+ opt.text = opts[i].text;
+ if (opts[i].value == v)
+ {
+ ind = i;
+ }
+ elem.options.add(opt);
+ }
+ var obj = this;
+ setTimeout(function(){elem.selectedIndex=ind;obj.setValue();}, 1);
+}
+
+DateWidget.prototype.remove_options = function(elem)
+{
+ if (!elem) return;
+ while (elem.options.length)
+ {
+ elem.remove(0);
+ }
+}
+
+DateWidget.prototype.zerofill = function(s, n)
+{
+ s = s + "";
+ var l = s.length;
+ var out = "";
+ for (var i=0; i<n-l; i++)
+ {
+ out = "0"+out;
+ }
+ out = out + s + "";
+ return out;
+}
\ No newline at end of file
Modified: 3.x/trunk/limb/calendar/shared/js/lang/calendar-en.js
===================================================================
--- 3.x/trunk/limb/calendar/shared/js/lang/calendar-en.js 2007-10-03 05:42:32 UTC (rev 6370)
+++ 3.x/trunk/limb/calendar/shared/js/lang/calendar-en.js 2007-10-03 06:02:04 UTC (rev 6371)
@@ -125,3 +125,8 @@
Calendar._TT["WK"] = "wk";
Calendar._TT["TIME"] = "Time:";
+
+Calendar._DEFOPT = {};
+Calendar._DEFOPT['day'] = 'day';
+Calendar._DEFOPT['month'] = 'month';
+Calendar._DEFOPT['year'] = 'year';
Modified: 3.x/trunk/limb/calendar/shared/js/lang/calendar-ru.js
===================================================================
--- 3.x/trunk/limb/calendar/shared/js/lang/calendar-ru.js 2007-10-03 05:42:32 UTC (rev 6370)
+++ 3.x/trunk/limb/calendar/shared/js/lang/calendar-ru.js 2007-10-03 06:02:04 UTC (rev 6371)
@@ -125,3 +125,8 @@
Calendar._TT["WK"] = "нед";
Calendar._TT["TIME"] = "Время:";
+
+Calendar._DEFOPT = {};
+Calendar._DEFOPT['day'] = 'число';
+Calendar._DEFOPT['month'] = 'месяц';
+Calendar._DEFOPT['year'] = 'год';
Added: 3.x/trunk/limb/calendar/src/lmbDateWidget.class.php
===================================================================
--- 3.x/trunk/limb/calendar/src/lmbDateWidget.class.php (rev 0)
+++ 3.x/trunk/limb/calendar/src/lmbDateWidget.class.php 2007-10-03 06:02:04 UTC (rev 6371)
@@ -0,0 +1,77 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * This class allows you to enter the date by three select fields.
+ * @package calendar
+ * @version $Id$
+ */
+class lmbDateWidget {
+
+ protected $newline = "\n";
+
+ protected $dw_lang_file;
+ protected $dw_year_class;
+ protected $dw_month_class;
+ protected $dw_day_class;
+ protected $dw_lib_path;
+ protected $calendar_lib_path = '/shared/calendar/js/';
+ protected $calendar_file = 'calendar.js';
+ protected $show_default = false;
+
+ function __construct($lang = 'en',
+ $year_class = "",
+ $month_class = "",
+ $day_class = "",
+ $show_default = false,
+ $lib_path = '/shared/calendar/js/')
+ {
+ $this -> show_default = $show_default;
+ $this -> calendar_lib_path = $lib_path;
+ $this -> dw_lang_file = 'lang/calendar-' . $lang . '.js';
+ $this -> dw_year_class = $year_class;
+ $this -> dw_month_class = $month_class;
+ $this -> dw_day_class = $day_class;
+ $this -> dw_lib_path = preg_replace('/\/+$/', '/', $this->calendar_lib_path).'datewidget.js';
+ }
+
+ function loadFiles()
+ {
+ static $rendered = false;
+
+ $code = '';
+
+ if(!$rendered)
+ {
+ $code = '<script type="text/javascript" src="' .
+ $this->calendar_lib_path . $this->calendar_file .
+ '"></script>' . $this->newline;
+ $code .= '<script type="text/javascript" src="' .
+ $this->calendar_lib_path . $this->dw_lang_file .
+ '"></script>' . $this->newline;
+ $code .= '<script type="text/javascript" src="' .
+ $this->dw_lib_path . '"></script>' . $this->newline;
+ }
+
+ $rendered = true;
+
+ return $code;
+ }
+
+ function makeFields($field_name)
+ {
+ $out = '<select name="'.$field_name.'_day" id="'.$field_name.'_day" class="'.$this->dw_day_class.'" onchange="DateWidget_Action(\''.$field_name.'\', \'handle_change\', \'day\');"></select>'.$this->newline;
+ $out .= '<select name="'.$field_name.'_month" id="'.$field_name.'_month" class="'.$this->dw_month_class.'" onchange="DateWidget_Action(\''.$field_name.'\', \'handle_change\', \'month\');"></select>'.$this->newline;
+ $out .= '<select name="'.$field_name.'_year" id="'.$field_name.'_year" class="'.$this->dw_year_class.'" onchange="DateWidget_Action(\''.$field_name.'\', \'handle_change\', \'year\');"></select>'.$this->newline;
+ $out .= '<script type="text/javascript">DateWidget_Init("'.$field_name.'", '.($this->show_default ? 'true' : 'false').');</script>'.$this->newline;
+ return $out;
+ }
+
+
+}
\ No newline at end of file
Added: 3.x/trunk/limb/calendar/src/template/tags/date_select.tag.php
===================================================================
--- 3.x/trunk/limb/calendar/src/template/tags/date_select.tag.php (rev 0)
+++ 3.x/trunk/limb/calendar/src/template/tags/date_select.tag.php 2007-10-03 06:02:04 UTC (rev 6371)
@@ -0,0 +1,53 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+require_once('limb/wact/src/tags/form/input.tag.php');
+require_once('limb/calendar/src/lmbDateWidget.class.php');
+
+/**
+ * @tag date_select
+ * @forbid_end_tag
+ * @package calendar
+ * @req_attributes id
+ */
+class DateSelectTag extends WactInputTag
+{
+ function getRenderedTag()
+ {
+ return 'input';
+ }
+
+ function prepare()
+ {
+ $this->setAttribute('type', 'hidden');
+ parent :: prepare();
+ }
+
+ function generateAfterCloseTag($code)
+ {
+ parent :: generateAfterCloseTag($code);
+
+ if(!$lang = $this->getAttribute('lang'))
+ $lang = 'en';
+
+ $year_class = $this->getAttribute('year_class');
+ $month_class = $this->getAttribute('year_class');
+ $day_class = $this->getAttribute('year_class');
+ $show_default = $this->getBoolAttribute('show_default');
+
+ $widget = new lmbDateWidget($lang, $year_class, $month_class, $day_class, $show_default);
+
+
+ $code->writeHTML($widget->loadFiles() .
+ $widget->makeFields($this->getAttribute('id') ));
+
+ }
+
+}
+
+
More information about the limb-svn
mailing list