23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Hint If you process the right mouse button click event for an element, be aware that the default<br />

behavior to show the app bar will be suppressed over that element. Therefore, use the right-click event<br />

judiciously, because users will become accustomed to right-clicking around the app to bring up the app<br />

bar. Note also that you can programmatically invoke the app bar yourself using its show method.<br />

The Context menu sample gives us some context here—I know, it’s a bad pun! In all cases, you need<br />

only listen to the <strong>HTML</strong> contextmenu event on the appropriate element; you don’t need to worry about<br />

mouse, touch, and keyboard separately. Scenario 1 of the sample, for instance, has a nonselectable<br />

attachment element on which it listens for the event (html/scenario1.html):<br />

document.getElementById("attachment").addEventListener("contextmenu",<br />

attachmentHandler, false);<br />

In the event handler, you then create a Windows.UI.Popups.PopupMenu object (which comes from<br />

WinRT, not WinJS!), populate it with Windows.UI.Popups.UICommand objects (that contain an item label<br />

and click handler) or UICommandSeparator objects, and then call the menu’s showAsync method<br />

(js/scenario1.js):<br />

function attachmentHandler(e) {<br />

var menu = new Windows.UI.Popups.PopupMenu();<br />

menu.commands.append(new Windows.UI.Popups.UICommand("Open with", onOpenWith));<br />

menu.commands.append(new Windows.UI.Popups.UICommand("Save attachment",<br />

onSaveAttachment));<br />

}<br />

menu.showAsync({ x: e.clientX, y: e.clientY }).done(function (invokedCommand) {<br />

if (invokedCommand === null) {<br />

// The command is null if no command was invoked.<br />

}<br />

});<br />

Notice that the results of the showAsync method 40 is the UICommand object that was invoked; you can<br />

examine its id property to take further action. Also, the parameter you give to showAsync is a<br />

Windows.Foundation.Point object that indicates where the menu should appear relative to the mouse<br />

pointer or the touch point. The menu is placed above and centered on this point.<br />

The PopupMenu object also supports a method called showForSelectionAsync, whose first argument is<br />

a Windows.Foundation.Rect that describes the applicable selection. Again, the menu is placed above<br />

and centered on this rectangle. This is demonstrated in Scenario 2 of the sample in js/scenario2.js:<br />

40 The sample actually calls then and not done here. If you’re wondering why such consistencies exist, it’s because the done<br />

method was introduced mid-way during the production of Windows 8 when it became clear that we needed a better<br />

mechanism for surfacing exceptions within chained promises. As a result, numerous SDK samples and code in the<br />

documentation still use then instead of done when handling the last promise in a chain. It still works; it’s just that<br />

exceptions in the chain will be swallowed, thus hiding possible errors.<br />

302

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!