📄 zptime-controlling.js
字号:
//$Id: zptime-controlling.js 6651 2007-03-19 10:15:22Z slip $/** * Sets the time from the given date and * replaces config.date with it if copy * parameter is false. * @param date {object} date object. * @param replace {boolean} should we replace * date object or just copy time. * @return {boolean} true if success, otherwise false. */Zapatec.TimeSelect.prototype.setTime = function(date, replace) { //geting configuration var config = this.getConfiguration(); //if not date passed taking one from the config if (!Zapatec.isDate(date)) { date = config.date; } //rounding time to time interval this._roundDate(date); //checking the availability of the date if (this.checkDate(date) == "disabled") { return false; } if (replace) { //replacing date config.date = date; } else { //copying time config.date.setHours(date.getHours()); config.date.setMinutes(date.getMinutes()); config.date.setSeconds(date.getSeconds()); } //updating time view this._updateTime(); //updating controls(some of them may become disabled) this._updateControls(); //toggling shift button if the mouse pointer is over some select this._toggleShift(); //firing onTimeChange event this.fireEvent("onTimeChange", config.date); //returning success return true;};/** * Sets the first enabled time. */Zapatec.TimeSelect.prototype.setFirstEnabledTime = function() { //getting configuration var config = this.getConfiguration(), //copying date date = new Date(config.date), i = 0; //if can not set config time then starting loop if (!this.setTime()) { //reseting time date.setHours(0); date.setMinutes(0); date.setSeconds(0); //looping through one day for(i = 1; i < 288; ++i) { //if can set then stop the loop if (this.setTime(date)) { break; } //taking next time with 5 minute interval date.setTime(date.getTime() + 300000); } }};/** * Sets hours of our date object and updates view. * @param hours {number} the number to be set as new hours. * @return {boolean} true if success, otherwise false. */Zapatec.TimeSelect.prototype.setHours = function(hours) { //getting configuration var config = this.getConfiguration(); //copying date object var date = new Date(config.date); //setting hours in it date.setHours(hours); //setting new time return this.setTime(date);};/** * Sets minutes of our date object and updates view. * @param minutes {number} the number to be set as new minutes. * @return {boolean} true if success, otherwise false. */Zapatec.TimeSelect.prototype.setMinutes = function(minutes) { //getting configuration var config = this.getConfiguration(); //copying date object var date = new Date(config.date); //setting minutes in it date.setMinutes(minutes); //setting new time return this.setTime(date);};/** * Sets seconds of our date object and updates view. * @param seconds {number} the number to be set as new seconds. * @return {boolean} true if success, otherwise false. */Zapatec.TimeSelect.prototype.setSeconds = function(seconds) { //getting configuration var config = this.getConfiguration(); //copying date object var date = new Date(config.date); //setting seconds in it date.setSeconds(seconds); //setting new time return this.setTime(date);};/** * Gets hours selected. * @return {number} hours. */Zapatec.TimeSelect.prototype.getHours = function() { return this.getConfiguration().date.getHours();};/** * Gets minutes selected. * @return {number} hours. */Zapatec.TimeSelect.prototype.getMinutes = function() { return this.getConfiguration().date.getMinutes();};/** * Gets seconds selected. * @return {number} hours. */Zapatec.TimeSelect.prototype.getSeconds = function() { return this.getConfiguration().date.getSeconds();};/** * Update the time displayed. */Zapatec.TimeSelect.prototype._updateTime = function() { if (!this.fireOnState("loaded", function() {this._updateTime();})) { return; } //getting configuration var config = this.getConfiguration(); var val = null, hours = minutes = seconds = 0, pm = false; //updating hours if (config.showHours) { //trying to handle hours format hours = config.date.getHours(); if (config.timeFormat == "12") { pm = (hours > 11); if (pm && hours != 12) {hours -= 12;} if (!pm && hours === 0) {hours = 12;} //if it is 12 hour format we need to update ampm control this.ampmSelect.setText(pm ? "PM" : "AM"); } //setting hours val = (hours > 9) ? hours : "0" + hours; this.hoursSelect.setText(val); } //updating minutes if (config.showMinutes) { minutes = config.date.getMinutes(); val = (minutes > 9) ? minutes : "0" + minutes; this.minutesSelect.setText(val); } //updating seconds if (config.showSeconds) { seconds = config.date.getSeconds(); val = (seconds > 9) ? seconds : "0" + seconds; this.secondsSelect.setText(val); }};/** * Updates controls, meaning that it will * toggle(disable or enable) all controls * due to timeStatus and timeInterval. */Zapatec.TimeSelect.prototype._updateControls = function() { if (!this.fireOnState("loaded", function() {this._updateControls();})) { return; } //getting configuration var config = this.getConfiguration(); var self = this; //function to toggle control function toggleControl(control) { var date = new Date(config.date); //making capital letter value var cont = control.charAt(0).toUpperCase() + control.slice(1); //getting part date value var val = self["get" + cont](); //taking select var select = self[control + "Select"]; //checking plus step date["set" + cont](val + 1); select.noShiftEnabled = (self.checkDate(date) == "disabled") ? false : true; //renewing the date date = new Date(config.date); //checking minus step date["set" + cont](val - 1); select.shiftEnabled = (self.checkDate(date) == "disabled") ? false : true; //taking arrows var upArrow = self[control + "Up"]; var downArrow = self[control + "Down"]; //getting step var step = self._getStep(control, "plus"); //renewing the date date = new Date(config.date); //toggling up arrow date["set" + cont](val + step); if (self.checkDate(date) == "disabled") { upArrow.disable(); } else { upArrow.enable(); } //getting step step = self._getStep(control, "minus"); //renewing the date date = new Date(config.date); //toggling down arrow date["set" + cont](val - step); if (self.checkDate(date) == "disabled") { downArrow.disable(); } else { downArrow.enable(); } } //no status function - no action if (config.timeStatus) { //togling seconds if (config.showSeconds) { toggleControl("seconds"); } //toggling minutes if (config.showMinutes) { toggleControl("minutes"); } //toggling hours if (config.showHours) { toggleControl("hours"); } } if (config.timeInterval) { //disable controlling of seconds this.secondsSelect.shiftEnabled = false; this.secondsSelect.noShiftEnabled = false; this.secondsUp.disable(); this.secondsDown.disable(); //disabling minutes select this.minutesSelect.shiftEnabled = false; this.minutesSelect.noShiftEnabled = false; if (config.timeInterval >= 60) { //disabling minutes arrow control this.minutesUp.disable(); this.minutesDown.disable(); //disabling hours select this.hoursSelect.shiftEnabled = false; this.hoursSelect.noShiftEnabled = false; } }};/** * Rounds the date due to the timeInterval. * Affects the object itself, so does not * returns anything. * @param date {object} date object to round. */Zapatec.TimeSelect.prototype._roundDate = function(date) { //taking interval var timeInterval = this.getConfiguration().timeInterval; //no interval no rounding if (!timeInterval) { return; } //calculating overcome var MINUTE = 60000; var interval = MINUTE * timeInterval; var overcome = (date.getTime() % interval); //rounding date date.setTime(date.getTime() - overcome + (overcome ? interval : 0));};/** * Gets the step for the given part arrow. * Other buttons can not have step. * @param part {string} string identifying part. * @param direction {string} string "plus" or "minus" * @return {number} step for this part. */Zapatec.TimeSelect.prototype._getStep = function(part, direction) { //taking interval var timeInterval = this.getConfiguration().timeInterval; switch (part) { //hours step case "hours" : { if (timeInterval && timeInterval >= 60) { return timeInterval / 60; } else { return 1; } } //minutes step case "minutes" : { if (!timeInterval) { var diff = this.getMinutes() % 5; if (direction == "plus") { return 5 - diff; } else { return diff || 5; } } else { if (timeInterval < 60) { return timeInterval; } else { return null; } } } //seconds step case "seconds" : { if (!timeInterval) { var diff = this.getSeconds() % 5; if (direction == "plus") { return 5 - diff; } else { return diff || 5; } } else { return null; } } }}; /** * Checks the status of the given date. * @param date {object} date object to check. */Zapatec.TimeSelect.prototype.checkDate = function(date) { var config = this.getConfiguration(); if (!config.timeStatus) { return true; } return config.timeStatus(date, date.getHours(), date.getMinutes(), date.getSeconds());};/** * Toggles shift and hilighting. */Zapatec.TimeSelect.prototype._toggleShift = function() { //the mouse pointer should be over some select if (!this.state.overSelect) { return; } //getting select var select = this.state.overSelect; //getting shift state var shift = this.state.shiftKey; //checking if current state is disabled if ((!select.shiftEnabled && shift) || (!select.noShiftEnabled && !shift)) { select.disable(); Zapatec.Utils.removeClass(select.getInternalContainer(), "zpTimeSelectHovered"); } else { select.enable(); Zapatec.Utils.addClass(select.getInternalContainer(), "zpTimeSelectHovered"); }};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -