Skip to content

Commit bf8f647

Browse files
committed
Promote view_email and load_email_menu to first-class observers.
- update documentation.
1 parent ebc0891 commit bf8f647

File tree

2 files changed

+26
-50
lines changed

2 files changed

+26
-50
lines changed

README.md

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ gmail.observe.on("load", function(){
211211
- **`compose`** - When a new compose window is opened, or a message is replied to or forwarded
212212
- **`recipient_change`** - When an email being written (either new compose, reply or forward) has its to, cc or bcc recipients updated
213213
- **`view_thread`** - When a conversation thread is opened to read
214-
- **`view_email`** - Sub-observer to `view_thread`. When an individual email is loaded within a conversation thread.
214+
- **`view_email`* - When an individual email is loaded within a conversation thread.
215215
It's worth noting this event is only triggered when the email is actually rendered in the DOM. Gmail tends to cache the rendered emails, so it should not be expected to fire reliably for every viewing of the same email. It will most likely fire once, for the initial and possibly only rendering.
216-
- **`load_email_menu`** - Sub-observer to `view_thread`. When the dropdown menu next to the reply button is clicked
216+
- **`load_email_menu`** - When the dropdown menu next to the reply button is clicked
217217
- [gmail.observe**.before(action, callback)**](#gmailobservebeforeaction-callback)
218218
- [gmail.observe**.after(action, callback)**](#gmailobserveafteraction-callback)
219219
- gmail.observe**.bind(type, action, callback)** - implements the on, after, before callbacks
@@ -854,32 +854,15 @@ Your callback will be fired directly after Gmail's XMLHttpRequest has been sent
854854

855855
The on method also supports observering specific DOM events in the Gmail Interface (for example when a new compose window is opened). These are only available via the `on` method (not the `before` or `after` methods).
856856

857-
Some actions/observers also have defined 'sub-observers' which only (!) become available if you have an action bound to the parent observer. Sub-observers are defined as such because they only make sense once the parent has been triggered. I.e. for an individual email (or several emails) to display as part of a conversation thread, the thread must first be opened/loaded in the interface.
858-
859-
Example usage:
860-
861-
```js
862-
gmail.observe.on('view_thread', function(obj) {
863-
console.log('view_thread', obj);
864-
});
865-
866-
// now we have access to the sub observers
867-
and load_email_menu
868-
gmail.observe.on('view_email', function(obj) {
869-
console.log('view_email', obj);
870-
});
871-
```
872-
873-
**Available DOM Actions/Observers & Sub-observers**
857+
**Available DOM Actions/Observers**
874858

875859
- **load** - When the gmail interface has completed loading
876860
- **compose** - When a new compose window opens, or a message is replied to or forwarded
877861
- **compose_cancelled** - When an existing compose window is closed.
878862
- **recipient_change** - When the recipient (to, cc or bcc) is changed when composing a new email or replying/forwarding an email
879863
- **view_thread** - When a new coversation thread is opened
880-
- **view_thread Sub-observers**
881-
- **view_email** - When an individual email is loaded within a thread (also fires when thread loads displaying the latest email)
882-
- **load_email_menu** - When the dropdown menu next to the reply button is clicked
864+
- **view_email** - When an individual email is loaded within a thread (also fires when thread loads displaying the latest email)
865+
- **load_email_menu** - When the dropdown menu next to the reply button is clicked
883866

884867
```js
885868
gmail.observe.on("http_event", function(params) {

src/gmail.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,35 +1633,28 @@ var Gmail = function(localJQuery) {
16331633
handler: function(match, callback) {
16341634
match = new api.dom.thread(match);
16351635
callback(match);
1636+
}
1637+
},
16361638

1637-
// look for any email elements in this thread that are currently displaying
1638-
// and fire off any view_email sub_observers for each of them
1639-
var email = match.dom("opened_email");
1640-
if (email.length) {
1641-
api.observe.trigger_dom("view_email", email, api.tracker.dom_observers.view_thread.sub_observers.view_email.handler);
1642-
}
1643-
},
1644-
sub_observers: {
1645-
1646-
// when an individual email is loaded within a thread (also fires when thread loads displaying the latest email)
1647-
"view_email": {
1648-
class: "",
1649-
sub_selector: "div.adn",
1650-
handler: function(match, callback) {
1651-
match = new api.dom.email(match);
1652-
callback(match);
1653-
}
1654-
},
1655-
1656-
// when the dropdown menu next to the reply button is inserted into the DOM when viewing an email
1657-
"load_email_menu": {
1658-
class: "J-N",
1659-
selector: "div[role=menu] div[role=menuitem]:first-child", // use the first menu item in the popoup as the indicator to trigger this observer
1660-
handler: function(match, callback) {
1661-
match = match.closest("div[role=menu]");
1662-
callback(match);
1663-
}
1664-
}
1639+
// when an individual email is loaded within a thread (also fires when thread loads displaying the latest email)
1640+
"view_email": {
1641+
// class depends if is_preview_pane - Bu for preview pane, nH for standard view,
1642+
// the empty class ("") is for emails opened after thread is rendered.
1643+
class: ["Bu", "nH", ""],
1644+
sub_selector: "div.adn",
1645+
handler: function(match, callback) {
1646+
match = new api.dom.email(match);
1647+
callback(match);
1648+
}
1649+
},
1650+
1651+
// when the dropdown menu next to the reply button is inserted into the DOM when viewing an email
1652+
"load_email_menu": {
1653+
class: "J-N",
1654+
selector: "div[role=menu] div[role=menuitem]:first-child", // use the first menu item in the popoup as the indicator to trigger this observer
1655+
handler: function(match, callback) {
1656+
match = match.closest("div[role=menu]");
1657+
callback(match);
16651658
}
16661659
},
16671660

0 commit comments

Comments
 (0)