Skip to content

Commit 6ee5f5d

Browse files
committed
Added skip functionality
Now you can simply call ClientTimezone::skip() in the your controller and the javascript will not attempt to get the client timezone.
1 parent 5c3526e commit 6ee5f5d

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed
File renamed without changes.

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ $carbon = Carbon::now();
6363
$carbon->addMinutes(ClientTimezone::getOffset());
6464
```
6565

66+
There may be times when you do not want javascript to check the client timezone. An example
67+
of this is when the client creates a new session on your application by following a URL
68+
to verify their email address, this page displays a flash message, but will be shown briefly
69+
until Javascript reloads the page. Upon the page reload, the flash message will have expired.
70+
71+
To prevent Javascript from checking the client timezone and reloading the page if it needs to,
72+
just add this line to your controller before returning a view.
73+
74+
`ClientTimezone::skip();`
75+
6676
## Authors
6777

6878
* **Kevin Orriss** - [Website](http://kevinorriss.com)
@@ -71,4 +81,4 @@ See also the list of [contributors](https://github.com/kevinorriss/clienttimezon
7181

7282
## License
7383

74-
This project is licensed under the MIT License - see the [LICENSE](src/LICENSE) file for details
84+
This project is licensed under the MIT License - see the [LICENSE](LICENSE.md) file for details

src/ClientTimezone.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class ClientTimezone
3030
*/
3131
const CLIENT_TIMEZONE_POST = "clienttimezone";
3232

33+
/**
34+
* Flag to skip getting the client timezone
35+
*
36+
* @var boolean
37+
*/
38+
protected static $skip = FALSE;
39+
3340

3441
/**
3542
* Returns the session key where the timezone offset is stored.
@@ -55,6 +62,16 @@ public static function getPostUrl()
5562
return env('CLIENT_TIMEZONE_POST', self::CLIENT_TIMEZONE_POST);
5663
}
5764

65+
/**
66+
* Returns if the client timezone is stored in session or not
67+
*
68+
* @return boolean
69+
*/
70+
public static function hasOffset()
71+
{
72+
return Session::has(static::getSessionKey());
73+
}
74+
5875
/**
5976
* Returns the client timezone as the number of minutes offset from the current UTC time.
6077
* If the client timezone is not set in the session, NULL is returned
@@ -63,7 +80,7 @@ public static function getPostUrl()
6380
*/
6481
public static function getOffset()
6582
{
66-
if (Session::has(static::getSessionKey()))
83+
if (static::hasOffset())
6784
{
6885
return intval(Session::get(static::getSessionKey()));
6986
}
@@ -87,4 +104,22 @@ public static function forget()
87104
{
88105
Session::forget(static::getSessionKey());
89106
}
107+
108+
/**
109+
* Sets the skip flag to true, meaning that the javascript will not check for the client timezone
110+
*/
111+
public static function skip()
112+
{
113+
static::$skip = TRUE;
114+
}
115+
116+
/**
117+
* Returns if the client timezone should be checked or not
118+
*
119+
* @return boolean
120+
*/
121+
public static function check()
122+
{
123+
return !static::$skip && is_null(static::getOffset());
124+
}
90125
}

src/views/javascript.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script>
22
$(document).ready(function()
33
{
4-
if("{{ ClientTimezone::getOffset() }}".length == 0)
4+
if({{ (ClientTimezone::check() ? "true" : "false") }})
55
{
66
var clienttime = new Date();
77
var clienttimezone = -clienttime.getTimezoneOffset();

0 commit comments

Comments
 (0)