stdweb/webapi/
history.rs

1use webcore::value::Reference;
2use webcore::try_from::TryInto;
3use webcore::serialization::JsSerialize;
4use private::TODO;
5
6/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/History)
7// https://html.spec.whatwg.org/#history-3
8#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
9#[reference(instance_of = "History")]
10pub struct History(Reference);
11
12impl History {
13    /// Adds a new entry to history.
14    ///
15    /// pushState() takes three parameters: a state object, a title (which is currently ignored),
16    /// and (optionally) a URL. Let's examine each of these three parameters in more detail:
17    ///
18    /// - state object — The state object is a JavaScript object which is associated with the new
19    /// history entry created by pushState(). Whenever the user navigates to the new state, a
20    /// popstate event is fired, and the state property of the event contains a copy of the history
21    /// entry's state object.
22    ///
23    /// - title — Firefox currently ignores this parameter, although it may use it in the future.
24    /// Passing the empty string here should be safe against future changes to the method.
25    /// Alternatively, you could pass a short title for the state to which you're moving.
26    ///
27    /// - URL — The new history entry's URL is given by this parameter. Note that the browser won't
28    /// attempt to load this URL after a call to pushState(), but it might attempt to load the URL
29    /// later, for instance after the user restarts the browser. The new URL does not need to be
30    /// absolute; if it's relative, it's resolved relative to the current URL. The new URL must be
31    /// of the same origin as the current URL; otherwise, pushState() will throw an exception.
32    /// This parameter is optional; if it isn't specified, it's set to the document's current URL.
33    ///
34    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState%28%29_method)
35    // https://html.spec.whatwg.org/#the-history-interface:dom-history-pushstate
36    pub fn push_state<T: JsSerialize>(&self, state: T, title: &str, url: Option<&str>) {
37        js!{ @(no_return)
38            @{self}.pushState(@{state}, @{title}, @{url});
39        };
40    }
41
42    /// Operates exactly like history.push_state() except that replace_state() modifies the current
43    /// history entry instead of creating a new one. Note that this doesn't prevent the creation of
44    /// a new entry in the global browser history.
45    ///
46    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_replaceState%28%29_method)
47    // https://html.spec.whatwg.org/#the-history-interface:dom-history-replacestate
48    pub fn replace_state<T: JsSerialize>(&self, state: T, title: &str, url: Option<&str>) -> Result< (), TODO > {
49        js!{ @(no_return)
50            @{self}.replaceState(@{state}, @{title}, @{url});
51        };
52        Ok(())
53    }
54
55    /// You can use the go() method to load a specific page from session history, identified by its
56    /// relative position to the current page (with the current page being, of course, relative
57    /// index 0).
58    ///
59    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/History_API#Traveling_through_history)
60    // https://html.spec.whatwg.org/#the-history-interface:dom-history-go
61    pub fn go(&self, offset: i32) -> Result< (), TODO > {
62        js! { @(no_return)
63            @{self}.go(@{offset});
64        };
65        Ok(())
66    }
67
68    /// Move one step backward through history.
69    ///
70    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/History_API#Traveling_through_history)
71    // https://html.spec.whatwg.org/#the-history-interface:dom-history-back
72    pub fn back(&self) -> Result< (), TODO > {
73        js! { @(no_return)
74            @{self}.back();
75        };
76        Ok(())
77    }
78
79    /// Move one step forward through history.
80    ///
81    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/History_API#Traveling_through_history)
82    // https://html.spec.whatwg.org/#the-history-interface:dom-history-forward
83    pub fn forward(&self) -> Result< (), TODO > {
84        js! { @(no_return)
85            @{self}.forward();
86        };
87        Ok(())
88    }
89
90    /// Returns the current number of history entries.
91    ///
92    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/History)
93    // https://html.spec.whatwg.org/#the-history-interface:dom-history-length
94    pub fn len(&self) -> u32 {
95        js!(
96            return @{self}.length;
97        ).try_into().unwrap()
98    }
99}