jquery.ptTimeSelect.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /**
  2. * FILE: jQuery.ptTileSelect.js
  3. *
  4. * @fileOverview
  5. * jQuery plugin for displaying a popup that allows a user
  6. * to define a time and set that time back to a form's input
  7. * field.
  8. *
  9. * @version 0.8
  10. * @author Paul Tavares, www.purtuga.com
  11. * @see http://pttimeselect.sourceforge.net
  12. *
  13. * @requires jQuery {@link http://www.jquery.com}
  14. *
  15. *
  16. * LICENSE:
  17. *
  18. * Copyright (c) 2007 Paul T. (purtuga.com)
  19. * Dual licensed under the:
  20. *
  21. * - MIT
  22. * <http://www.opensource.org/licenses/mit-license.php>
  23. *
  24. * - GPL
  25. * <http://www.opensource.org/licenses/gpl-license.php>
  26. *
  27. * User can pick whichever one applies best for their project
  28. * and doesn not have to contact me.
  29. *
  30. *
  31. * INSTALLATION:
  32. *
  33. * There are two files (.css and .js) delivered with this plugin and
  34. * that must be included in your html page after the jquery.js library
  35. * and the jQuery UI style sheet (the jQuery UI javascript library is
  36. * not necessary).
  37. * Both of these are to be included inside of the 'head' element of
  38. * the document. Example below demonstrates this along side the jQuery
  39. * libraries.
  40. *
  41. * | <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
  42. * | <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.22/themes/redmond/jquery-ui.css" />
  43. * |
  44. * | <link rel="stylesheet" type="text/css" href="jquery.ptTimeSelect.css" />
  45. * | <script type="text/javascript" src="jquery.ptTimeSelect.js"></script>
  46. * |
  47. *
  48. * USAGE:
  49. *
  50. * - See <$(ele).ptTimeSelect()>
  51. *
  52. *
  53. *
  54. * LAST UPDATED:
  55. *
  56. * - $Date: 2012/08/05 19:40:21 $
  57. * - $Author: paulinho4u $
  58. * - $Revision: 1.8 $
  59. *
  60. */
  61. (function($){
  62. /**
  63. * jQuery definition
  64. *
  65. * @see http://jquery.com/
  66. * @name jQuery
  67. * @class jQuery Library
  68. */
  69. /**
  70. * jQuery 'fn' definition to anchor all public plugin methods.
  71. *
  72. * @see http://jquery.com/
  73. * @name fn
  74. * @class jQuery Library public method anchor
  75. * @memberOf jQuery
  76. */
  77. /**
  78. * Namespace for all properties and methods
  79. *
  80. * @namespace ptTimeSelect
  81. * @memberOf jQuery
  82. */
  83. jQuery.ptTimeSelect = {};
  84. jQuery.ptTimeSelect.version = "__BUILD_VERSION_NUMBER__";
  85. /**
  86. * The default options for all calls to ptTimeSelect. Can be
  87. * overwriten with each individual call to {@link jQuery.fn.ptTimeSelect}
  88. *
  89. * @type {Object} options
  90. * @memberOf jQuery.ptTimeSelect
  91. * @see jQuery.fn.ptTimeSelect
  92. */
  93. jQuery.ptTimeSelect.options = {
  94. containerClass: undefined,
  95. containerWidth: '22em',
  96. hoursLabel: 'Hour',
  97. minutesLabel: 'Minutes',
  98. setButtonLabel: 'Set',
  99. popupImage: undefined,
  100. onFocusDisplay: true,
  101. zIndex: 10,
  102. onBeforeShow: undefined,
  103. onClose: undefined
  104. };
  105. /**
  106. * Internal method. Called when page is initialized to add the time
  107. * selection area to the DOM.
  108. *
  109. * @private
  110. * @memberOf jQuery.ptTimeSelect
  111. * @return {undefined}
  112. */
  113. jQuery.ptTimeSelect._ptTimeSelectInit = function () {
  114. jQuery(document).ready(
  115. function () {
  116. //if the html is not yet created in the document, then do it now
  117. if (!jQuery('#ptTimeSelectCntr').length) {
  118. jQuery("body").append(
  119. '<div id="ptTimeSelectCntr" class="">'
  120. + ' <div class="ui-widget ui-widget-content ui-corner-all">'
  121. + ' <div class="ui-widget-header ui-corner-all">'
  122. + ' <div id="ptTimeSelectCloseCntr" style="float: right;">'
  123. + ' <a href="javascript: void(0);" onclick="jQuery.ptTimeSelect.closeCntr();" '
  124. + ' onmouseover="jQuery(this).removeClass(\'ui-state-default\').addClass(\'ui-state-hover\');" '
  125. + ' onmouseout="jQuery(this).removeClass(\'ui-state-hover\').addClass(\'ui-state-default\');"'
  126. + ' class="ui-corner-all ui-state-default">'
  127. + ' <span class="ui-icon ui-icon-circle-close">X</span>'
  128. + ' </a>'
  129. + ' </div>'
  130. + ' <div id="ptTimeSelectUserTime" style="float: left;">'
  131. + ' <span id="ptTimeSelectUserSelHr">1</span> : '
  132. + ' <span id="ptTimeSelectUserSelMin">00</span> '
  133. + ' <span id="ptTimeSelectUserSelAmPm">AM</span>'
  134. + ' </div>'
  135. + ' <br style="clear: both;" /><div></div>'
  136. + ' </div>'
  137. + ' <div class="ui-widget-content ui-corner-all">'
  138. + ' <div>'
  139. + ' <div class="ptTimeSelectTimeLabelsCntr">'
  140. + ' <div class="ptTimeSelectLeftPane" style="width: 50%; text-align: center; float: left;" class="">Hour</div>'
  141. + ' <div class="ptTimeSelectRightPane" style="width: 50%; text-align: center; float: left;">Minutes</div>'
  142. + ' </div>'
  143. + ' <div>'
  144. + ' <div style="float: left; width: 50%;">'
  145. + ' <div class="ui-widget-content ptTimeSelectLeftPane">'
  146. + ' <div class="ptTimeSelectHrAmPmCntr">'
  147. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);" '
  148. + ' style="display: block; width: 45%; float: left;">AM</a>'
  149. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);" '
  150. + ' style="display: block; width: 45%; float: left;">PM</a>'
  151. + ' <br style="clear: left;" /><div></div>'
  152. + ' </div>'
  153. + ' <div class="ptTimeSelectHrCntr">'
  154. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">1</a>'
  155. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">2</a>'
  156. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">3</a>'
  157. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">4</a>'
  158. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">5</a>'
  159. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">6</a>'
  160. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">7</a>'
  161. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">8</a>'
  162. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">9</a>'
  163. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">10</a>'
  164. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">11</a>'
  165. + ' <a class="ptTimeSelectHr ui-state-default" href="javascript: void(0);">12</a>'
  166. + ' <br style="clear: left;" /><div></div>'
  167. + ' </div>'
  168. + ' </div>'
  169. + ' </div>'
  170. + ' <div style="width: 50%; float: left;">'
  171. + ' <div class="ui-widget-content ptTimeSelectRightPane">'
  172. + ' <div class="ptTimeSelectMinCntr">'
  173. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">00</a>'
  174. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">05</a>'
  175. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">10</a>'
  176. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">15</a>'
  177. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">20</a>'
  178. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">25</a>'
  179. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">30</a>'
  180. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">35</a>'
  181. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">40</a>'
  182. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">45</a>'
  183. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">50</a>'
  184. + ' <a class="ptTimeSelectMin ui-state-default" href="javascript: void(0);">55</a>'
  185. + ' <br style="clear: left;" /><div></div>'
  186. + ' </div>'
  187. + ' </div>'
  188. + ' </div>'
  189. + ' </div>'
  190. + ' </div>'
  191. + ' <div style="clear: left;"></div>'
  192. + ' </div>'
  193. + ' <div id="ptTimeSelectSetButton">'
  194. + ' <a href="javascript: void(0);" onclick="jQuery.ptTimeSelect.setTime()"'
  195. + ' onmouseover="jQuery(this).removeClass(\'ui-state-default\').addClass(\'ui-state-hover\');" '
  196. + ' onmouseout="jQuery(this).removeClass(\'ui-state-hover\').addClass(\'ui-state-default\');"'
  197. + ' class="ui-corner-all ui-state-default">'
  198. + ' SET'
  199. + ' </a>'
  200. + ' <br style="clear: both;" /><div></div>'
  201. + ' </div>'
  202. + ' <!--[if lte IE 6.5]>'
  203. + ' <iframe style="display:block; position:absolute;top: 0;left:0;z-index:-1;'
  204. + ' filter:Alpha(Opacity=\'0\');width:3000px;height:3000px"></iframe>'
  205. + ' <![endif]-->'
  206. + ' </div></div>'
  207. );
  208. var e = jQuery('#ptTimeSelectCntr');
  209. // Add the events to the functions
  210. e.find('.ptTimeSelectMin')
  211. .bind("click", function(){
  212. jQuery.ptTimeSelect.setMin($(this).text());
  213. });
  214. e.find('.ptTimeSelectHr')
  215. .bind("click", function(){
  216. jQuery.ptTimeSelect.setHr($(this).text());
  217. });
  218. $(document).mousedown(jQuery.ptTimeSelect._doCheckMouseClick);
  219. }//end if
  220. }
  221. );
  222. }();// jQuery.ptTimeSelectInit()
  223. /**
  224. * Sets the hour selected by the user on the popup.
  225. *
  226. * @private
  227. * @param {Integer} h - Interger indicating the hour. This value
  228. * is the same as the text value displayed on the
  229. * popup under the hour. This value can also be the
  230. * words AM or PM.
  231. * @return {undefined}
  232. *
  233. */
  234. jQuery.ptTimeSelect.setHr = function(h) {
  235. if ( h.toLowerCase() == "am"
  236. || h.toLowerCase() == "pm"
  237. ) {
  238. jQuery('#ptTimeSelectUserSelAmPm').empty().append(h);
  239. } else {
  240. jQuery('#ptTimeSelectUserSelHr').empty().append(h);
  241. }
  242. };// END setHr() function
  243. /**
  244. * Sets the minutes selected by the user on the popup.
  245. *
  246. * @private
  247. * @param {Integer} m - interger indicating the minutes. This
  248. * value is the same as the text value displayed on the popup
  249. * under the minutes.
  250. * @return {undefined}
  251. */
  252. jQuery.ptTimeSelect.setMin = function(m) {
  253. jQuery('#ptTimeSelectUserSelMin').empty().append(m);
  254. };// END setMin() function
  255. /**
  256. * Takes the time defined by the user and sets it to the input
  257. * element that the popup is currently opened for.
  258. *
  259. * @private
  260. * @return {undefined}
  261. */
  262. jQuery.ptTimeSelect.setTime = function() {
  263. var tSel = jQuery('#ptTimeSelectUserSelHr').text()
  264. + ":"
  265. + jQuery('#ptTimeSelectUserSelMin').text()
  266. + " "
  267. + jQuery('#ptTimeSelectUserSelAmPm').text();
  268. jQuery(".isPtTimeSelectActive").val(tSel);
  269. this.closeCntr();
  270. };// END setTime() function
  271. /**
  272. * Displays the time definition area on the page, right below
  273. * the input field. Also sets the custom colors/css on the
  274. * displayed area to what ever the input element options were
  275. * set with.
  276. *
  277. * @private
  278. * @param {String} uId - Id of the element for whom the area will
  279. * be displayed. This ID was created when the
  280. * ptTimeSelect() method was called.
  281. * @return {undefined}
  282. *
  283. */
  284. jQuery.ptTimeSelect.openCntr = function (ele) {
  285. jQuery.ptTimeSelect.closeCntr();
  286. jQuery(".isPtTimeSelectActive").removeClass("isPtTimeSelectActive");
  287. var cntr = jQuery("#ptTimeSelectCntr");
  288. var i = jQuery(ele).eq(0).addClass("isPtTimeSelectActive");
  289. var opt = i.data("ptTimeSelectOptions");
  290. var style = i.offset();
  291. style['z-index'] = opt.zIndex;
  292. style.top = (style.top + i.outerHeight());
  293. if (opt.containerWidth) {
  294. style.width = opt.containerWidth;
  295. }
  296. if (opt.containerClass) {
  297. cntr.addClass(opt.containerClass);
  298. }
  299. cntr.css(style);
  300. var hr = 1;
  301. var min = '00';
  302. var tm = 'AM';
  303. if (i.val()) {
  304. var re = /([0-9]{1,2}).*:.*([0-9]{2}).*(PM|AM)/i;
  305. var match = re.exec(i.val());
  306. if (match) {
  307. hr = match[1] || 1;
  308. min = match[2] || '00';
  309. tm = match[3] || 'AM';
  310. }
  311. }
  312. cntr.find("#ptTimeSelectUserSelHr").empty().append(hr);
  313. cntr.find("#ptTimeSelectUserSelMin").empty().append(min);
  314. cntr.find("#ptTimeSelectUserSelAmPm").empty().append(tm);
  315. cntr.find(".ptTimeSelectTimeLabelsCntr .ptTimeSelectLeftPane")
  316. .empty().append(opt.hoursLabel);
  317. cntr.find(".ptTimeSelectTimeLabelsCntr .ptTimeSelectRightPane")
  318. .empty().append(opt.minutesLabel);
  319. cntr.find("#ptTimeSelectSetButton a").empty().append(opt.setButtonLabel);
  320. if (opt.onBeforeShow) {
  321. opt.onBeforeShow(i, cntr);
  322. }
  323. cntr.slideDown("fast");
  324. };// END openCntr()
  325. /**
  326. * Closes (hides it) the popup container.
  327. * @private
  328. * @param {Object} i - Optional. The input field for which the
  329. * container is being closed.
  330. * @return {undefined}
  331. */
  332. jQuery.ptTimeSelect.closeCntr = function(i) {
  333. var e = $("#ptTimeSelectCntr");
  334. if (e.is(":visible") == true) {
  335. // If IE, then check to make sure it is realy visible
  336. if (jQuery.support.tbody == false) {
  337. if (!(e[0].offsetWidth > 0) && !(e[0].offsetHeight > 0) ) {
  338. return;
  339. }
  340. }
  341. jQuery('#ptTimeSelectCntr')
  342. .css("display", "none")
  343. .removeClass()
  344. .css("width", "");
  345. if (!i) {
  346. i = $(".isPtTimeSelectActive");
  347. }
  348. if (i) {
  349. var opt = i.removeClass("isPtTimeSelectActive")
  350. .data("ptTimeSelectOptions");
  351. if (opt && opt.onClose) {
  352. opt.onClose(i);
  353. }
  354. }
  355. }
  356. return;
  357. };//end closeCntr()
  358. /**
  359. * Closes the timePicker popup if user is not longer focused on the
  360. * input field or the timepicker
  361. *
  362. * @private
  363. * @param {jQueryEvent} ev - Event passed in by jQuery
  364. * @return {undefined}
  365. */
  366. jQuery.ptTimeSelect._doCheckMouseClick = function(ev){
  367. if (!$("#ptTimeSelectCntr:visible").length) {
  368. return;
  369. }
  370. if ( !jQuery(ev.target).closest("#ptTimeSelectCntr").length
  371. && jQuery(ev.target).not("input.isPtTimeSelectActive").length ){
  372. jQuery.ptTimeSelect.closeCntr();
  373. }
  374. };// jQuery.ptTimeSelect._doCheckMouseClick
  375. /**
  376. * FUNCTION: $().ptTimeSelect()
  377. * Attaches a ptTimeSelect widget to each matched element. Matched
  378. * elements must be input fields that accept a values (input field).
  379. * Each element, when focused upon, will display a time selection
  380. * popoup where the user can define a time.
  381. *
  382. * @memberOf jQuery
  383. *
  384. * PARAMS:
  385. *
  386. * @param {Object} [opt] - An object with the options for the time selection widget.
  387. *
  388. * @param {String} [opt.containerClass=""] - A class to be associated with the popup widget.
  389. *
  390. * @param {String} [opt.containerWidth=""] - Css width for the container.
  391. *
  392. * @param {String} [opt.hoursLabel="Hours"] - Label for the Hours.
  393. *
  394. * @param {String} [opt.minutesLabel="Minutes"] - Label for the Mintues container.
  395. *
  396. * @param {String} [opt.setButtonLabel="Set"] - Label for the Set button.
  397. *
  398. * @param {String} [opt.popupImage=""] - The html element (ex. img or text) to be appended next to each
  399. * input field and that will display the time select widget upon
  400. * click.
  401. *
  402. * @param {Integer} [opt.zIndex=10] - Integer for the popup widget z-index.
  403. *
  404. * @param {Function} [opt.onBeforeShow=undefined] - Function to be called before the widget is made visible to the
  405. * user. Function is passed 2 arguments: 1) the input field as a
  406. * jquery object and 2) the popup widget as a jquery object.
  407. *
  408. * @param {Function} [opt.onClose=undefined] - Function to be called after closing the popup widget. Function
  409. * is passed 1 argument: the input field as a jquery object.
  410. *
  411. * @param {Bollean} [opt.onFocusDisplay=true] - True or False indicating if popup is auto displayed upon focus
  412. * of the input field.
  413. *
  414. *
  415. * RETURN:
  416. * @return {jQuery} selection
  417. *
  418. *
  419. *
  420. * EXAMPLE:
  421. * @example
  422. * $('#fooTime').ptTimeSelect();
  423. *
  424. */
  425. jQuery.fn.ptTimeSelect = function (opt) {
  426. return this.each(function(){
  427. if(this.nodeName.toLowerCase() != 'input') return;
  428. var e = jQuery(this);
  429. if (e.hasClass('hasPtTimeSelect')){
  430. return this;
  431. }
  432. var thisOpt = {};
  433. thisOpt = $.extend(thisOpt, jQuery.ptTimeSelect.options, opt);
  434. e.addClass('hasPtTimeSelect').data("ptTimeSelectOptions", thisOpt);
  435. //Wrap the input field in a <div> element with
  436. // a unique id for later referencing.
  437. if (thisOpt.popupImage || !thisOpt.onFocusDisplay) {
  438. var img = jQuery('<span>&nbsp;</span><a href="javascript:" onclick="' +
  439. 'jQuery.ptTimeSelect.openCntr(jQuery(this).data(\'ptTimeSelectEle\'));">' +
  440. thisOpt.popupImage + '</a>'
  441. )
  442. .data("ptTimeSelectEle", e);
  443. e.after(img);
  444. }
  445. if (thisOpt.onFocusDisplay){
  446. e.focus(function(){
  447. jQuery.ptTimeSelect.openCntr(this);
  448. });
  449. }
  450. return this;
  451. });
  452. };// End of jQuery.fn.ptTimeSelect
  453. })(jQuery);