1 /*
2  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3  *
4  * See the LICENSE file for terms of use.
5  */
6 #include "Wt/WApplication.h"
7 #include "Wt/WEnvironment.h"
8 #include "Wt/WTimerWidget.h"
9 #include "Wt/WTimer.h"
10 #include "DomElement.h"
11 
12 namespace Wt {
13 
WTimerWidget(WTimer * timer)14 WTimerWidget::WTimerWidget(WTimer *timer)
15   : timer_(timer),
16     timerStarted_(false)
17 { }
18 
~WTimerWidget()19 WTimerWidget::~WTimerWidget()
20 {
21   timer_->timerWidget_ = nullptr;
22 }
23 
renderRemoveJs(bool recursive)24 std::string WTimerWidget::renderRemoveJs(bool recursive)
25 {
26  return "{"
27    "var obj=" + jsRef() + ";"
28    "if (obj && obj.timer) {"
29    """clearTimeout(obj.timer);"
30    """obj.timer = null;"
31    "}" WT_CLASS ".remove('" + id() + "');}";
32 }
33 
timerStart(bool jsRepeat)34 void WTimerWidget::timerStart(bool jsRepeat)
35 {
36   timerStarted_ = true;
37   jsRepeat_ = jsRepeat;
38 
39   repaint();
40 }
41 
enableAjax()42 void WTimerWidget::enableAjax()
43 {
44   if (timer_->isActive()) {
45     timerStarted_ = true;
46     repaint();
47   }
48 
49   WInteractWidget::enableAjax();
50 }
51 
timerExpired()52 bool WTimerWidget::timerExpired()
53 {
54   return timer_->getRemainingInterval() == 0;
55 }
56 
updateDom(DomElement & element,bool all)57 void WTimerWidget::updateDom(DomElement& element, bool all)
58 {
59   if (timerStarted_
60       || ((!WApplication::instance()->environment().javaScript() || all)
61 	  && timer_->isActive())) {
62     if (jsRepeat_)
63       element.setTimeout(timer_->getRemainingInterval(), static_cast<int>(timer_->interval().count()));
64     else
65       element.setTimeout(timer_->getRemainingInterval(), false);
66 
67     timerStarted_ = false;
68   }
69 
70   WInteractWidget::updateDom(element, all);
71 }
72 
domElementType()73 DomElementType WTimerWidget::domElementType() const
74 {
75   return DomElementType::SPAN;
76 }
77 
78 }
79