|
ShowDialog() (Custom Dialog) 2 }/ M/ E% _) ?
Example usage:- // addNewFeed - triggered by "Add..." item in feed list and invokes the AddDialog. i4 _- c/ Y! W5 y+ @* I* |. p4 R
- // Assistant defined above./ k+ [( W* q8 E6 c
- //
! }0 B: Q* ^2 b* m5 Y5 m+ t - FeedListAssistant.prototype.addNewFeed = function() {5 n' q& x! S; R3 k9 T
- this.controller.showDialog({5 m% `: d6 s, X# N4 J
- template: 'feedList/addFeed-dialog',0 U% p2 L, m+ [; K( K# x) ?, C
- assistant: new AddDialogAssistant(this)
6 Q0 q% G5 i6 b - });6 K! e7 h$ G6 A2 `. s( p
- };
复制代码
9 _( D6 N# h9 ]+ m( _" T- _* b/ ?
4 w s! E% y$ n; l8 }- PA custom dialog uses a template (HTML file) and an assistant (JavaScript file), similar to a scene. The HTML file defines what will be displayed in the custom dialog and the JavaScript file defines how the elements will behave.$ V* V. c; o4 C x1 _
) D3 ~2 C4 G4 Y; c! f4 d8 MTemplate file:- <div>
O1 R8 D& o4 K u - <span id="add-feed-title" class="palm-dialog-title">Add News Feed Source</span>1 M4 R% |5 k" W9 k/ S, P
- <div class="palm-group unlabeled">. E. O4 h: \+ i% a9 Z: C" P, s) K# P
- <div class="palm-list">3 c/ r. N! H \( Z
- <div class='palm-row first'> W# I* K( B0 X- d+ U
- <div class="palm-row-wrapper textfield-group" x-mojo-focus-highlight="true">
6 n0 b; |) F0 e4 L" d+ r9 W1 O2 | - <div class="title">2 [ r, p" c& e, v2 u! f8 ]: D
- <div class="label">URL</div># f) W B: g8 m6 ?6 C, C
- <div id="newFeedURL" x-mojo-element="TextField" align="left"></div>9 X& U2 `: O# W. l
- </div>$ S3 s8 z7 V- B
- </div>
8 o& s& A6 E- e6 A- S1 g, S# ]4 n0 T - </div>6 A* A5 b, _* z, p
- <div class='palm-row last'>
6 A. U, C7 k/ `2 L& b% @ - <div class="palm-row-wrapper textfield-group" x-mojo-focus-highlight="true">
4 `5 p2 Q( p) h( d# a9 p - <div class="title">, a4 s$ Y7 \& {" X0 u+ n9 m: g
- <div class="label">Title</div>
0 {) n4 c v" T6 a$ ? - <div id="newFeedName" x-mojo-element="TextField" align="left"></div>
0 n5 ~. a: Z& L$ T - </div>. M9 l, v3 d5 t. h, g8 _
- </div>
# p7 V+ N3 v6 b5 n6 R3 L7 }! e+ h - </div>, H+ J& u! T- m* V" A6 q
- </div>+ f+ H/ X2 p1 u, Z8 j4 v7 p
- </div>
. A: h3 O# |( }3 r - <div x-mojo-element="Button" id="okButton"></div>
4 e- }4 |. l+ }7 j - </div>
复制代码 Here is the assistant for the dialog, which sets up the various widgets as well as the event handlers that will respond to user interaction with the widgets.
9 }; H# V1 l w7 h
" C2 B7 D1 v. ]Assistant file:- // AddDialogAssistant - simple controller for adding new feeds to the list.
* v. Z6 E9 ?6 e) @* H - // Invoked by the FeedListAssistant when the "Add..." list item is selected4 E8 H4 D5 V5 j( o' W
- // on the feed list.
. A4 f s& p1 _* \7 G - //
4 u8 @6 N! x7 G4 g) [, ` - // The dialog displays two text fields (URL and Name) and an OK button.* Z+ I) j t' d7 \$ I
- // Either the user enters a feed URL and a name, followed by OK or a; T7 b2 V2 K5 J. F2 B$ V: O* ?3 X
- // back swipe to close. If OK, the feed header is checked through an Ajax
( N) r" C2 b& Y/ n) U - // request and if valid, the feed updated and dialog closed. If an error,
! S9 ]9 f" N" S! i7 k7 r( U - // the posted in place of title and dialog remains open.
) D' k& |( w& g- y. ] - // Swipe back cancels and returns back to the FeedListAssistant. n ~1 H+ g9 H: e5 S
- //# s6 B! _' t1 [
- function AddDialogAssistant(sceneAssistant) {; s1 j+ b; U T! R2 T; F3 u
- this.sceneAssistant = sceneAssistant;4 o( y' ]3 T3 K6 \9 J% r
- }
9 t# M/ i0 d' l' x - AddDialogAssistant.prototype.setup = function(widget) {
8 ~3 H- ?: w5 z - this.widget = widget;
3 y! t# \0 o$ m( f' y7 v - // Setup text field for the new feed's URL3 N9 W$ c+ t g
- //: ] y) |8 d- o( O8 Z
- this.sceneAssistant.controller.setupWidget("newFeedURL",# [3 q; R' R' B6 x3 T% j
- this.urlAttributes = {
J5 J- x$ Q3 @ - property: "value",
" G. d5 G8 y) Q2 X0 W- \! J z - hintText: "RSS or ATOM feed",& ]3 j4 ?$ {4 u" t
- focus: true,
& ]1 p, ?6 Q* _% [; e& D - limitResize: true,! H8 [' h1 I9 C+ B$ g2 X
- textReplacement: false,8 N1 l6 N5 g6 w9 u8 c% ^; b) F
- enterSubmits: false* u4 a. v. g; f( U' L+ M
- },) r& U/ F" n& y4 O2 E" i7 h
- this.urlModel = {value : ""}
2 |" D" E2 w7 B, j - );( I* S0 i/ [2 Y4 x+ h
- // Setup text field for the new feed's name2 h& c5 h0 ]8 P5 v( A7 s
- //& C! T; r* N0 x: u+ O
- this.sceneAssistant.controller.setupWidget("newFeedName",
+ r( l, N0 z1 H' K0 j - this.nameAttributes = {5 L0 o1 P6 x4 h
- property: "value",
2 [1 c: r% v4 ` T" G R$ l - hintText: "Optional",
+ C( M* q4 H4 f! [2 o8 H - limitResize: true,
: N4 d* l# @4 f( r) p& } - textReplacement: false,
$ K# K [$ ~( Y, _ - enterSubmits: false
7 Y0 X6 {( n! |2 J9 z - },/ G1 ^1 K2 m6 W+ G. Y
- this.nameModel = {value : ""
7 L( e; Z* I, l( n7 A7 l - });" F6 _/ N/ C& b( s( ^/ W/ v
- // Setup button and event handler- } D% p+ b4 G0 Z; x: S
- /// |; b# n- T& Q* h0 F w
- this.sceneAssistant.controller.setupWidget("okButton",3 D+ v0 a4 P+ T4 a" T; k
- this.attributes = {},- X$ _% ^! H1 |4 ]) G- p$ t
- this.model = {
1 N. z* m m" F# j% _. K - buttonLabel: "Add Feed",( q: B- ^! ]9 t1 q/ W0 m4 g
- buttonClass: "addFeedButton",6 ~+ b+ x7 u0 s- L" |; N+ c& _
- disabled: false
; Y! [, O! `0 J9 }4 D1 P - }
# w+ \ r0 g- P/ X - );) ]" [% Z4 Q7 E; P! \& M. t* z
- Mojo.Event.listen($('okButton'), Mojo.Event.tap, this.checkIt.bindAsEventListener(this));
3 \* K! X5 k* d- b6 x x - };
' l6 |- @ M2 x- S8 s/ H- o - // ------------------------------------------------------------------------
4 \" c F' _8 A4 v. T+ ` - // Add Feed Functions Q5 I% @* B6 |. j t7 a6 a
- //5 Y: c: Q1 Y: a( q7 K r; @
- // checkIt - called when Add feed OK button is clicked
. X) K9 T3 P; ^4 k' I, h0 T" ` - //$ D9 z/ {& h& |: J6 C1 Q# }2 u, q2 I
- AddDialogAssistant.prototype.checkIt = function() {* ] k4 |' k7 { C( r5 M, z8 ^
- // Check for 'http://' on front or other legal prefix( J; m0 x5 H1 b1 T/ j
- // assume any string of 1 to 5 alpha characters followed+ R& g; r! u! `( v3 [/ u$ | d1 [8 y
- // by ':' is legal, otherwise prepend "http://"/ i9 o5 ]% P* \0 `' h
- // H4 \, n* o- \
- var url = this.urlModel.value;
& o P1 e$ K5 L U* h* R. Q - if (/^[a-z]{1,5}:/.test(url) === false) {
. i. Q) Y1 w* [; l: y+ k9 {& H) f - url = url.replace(/^\/{1,2}/,"");
2 a& w- r& J7 h( Q; O( D X - url = "http://"+url;" `; U+ l1 I9 a( M6 }
- }9 p1 P0 e, E, l
- // Update the submitted URL text model; r0 {% P1 @( D# C0 q! e
- this.urlModel.value = url;
6 t: G* I1 j0 v2 x( k9 L1 Q/ o - this.sceneAssistant.controller.modelChanged(this.urlModel);5 S& o/ u7 o% g2 I
- var request = new Ajax.Request(url, {
. g$ Y( k( m+ M& f, X2 |- f - method: 'get',, m: A, E4 A+ V7 |/ H$ g* \
- evalJSON: 'false',
/ |. f; w6 X/ W, @' W6 x - onSuccess: this.checkOK.bind(this),8 q6 }2 W a8 Q! E
- onFailure: this.checkFailure.bind(this)1 @0 T3 E. P* X
- });
: B' ~1 ^+ ]5 q }5 B* b$ P0 w+ ] - };
" _+ B/ |( o( g! D+ D% o# V/ K9 x* d - //4 Y3 R( f* J$ K( t/ O
- // checkOK - called when the Ajax request is successful.
Y# Z3 J2 r) K: d% t - //3 T5 z9 D1 v J: O
- AddDialogAssistant.prototype.checkOK = function(transport) {) n3 }7 {* o+ K9 U H2 [! ?
- // DEBUG - log the result0 N* ?' H4 b5 ^, U$ z
- //
4 \5 S) v- {- Q t! W - var t = new Template("Status #{status} returned from newsfeed request.");
" ^( v. x! o+ F: d! z! i - var success = "Feed Request Success: "+t.evaluate(transport);8 i8 V7 D6 K0 r7 u
- Mojo.Log.info(success);
8 J9 l- l( G' q9 [; {3 \5 \- V - // Work around when XML comes back in text response' F) f' z e- }: L; F
- if (transport.responseXML === null && transport.responseText !== null) {
0 g! |! J0 J3 m3 X1 T+ q# _ - Mojo.Log.info("••••••••","Request not in XML format - manually converting");
; |% }1 N) a* J+ F8 H" h/ R( T: N - transport.responseXML = new DOMParser().parseFromString (transport.responseText, 'text/xml');
: X1 O2 ]* l; x - }
, r- b" G& R/ ]# c, Q& e3 c - var feedError = errorNone;+ ~; D( }9 j5 r. i, P3 f
- // Push the entered feed onto feedlist and call processFeed to evaluate it.. @6 m9 U6 h; U, r7 I
- // This is mainly to record the user's TITLE entry before processing
: a0 \3 t7 {6 k3 `4 f$ o4 u - //& V# a8 \) W& z# L l( d: F1 D! w
- feedList.push({title:this.nameModel.value, url:this.urlModel.value, type:"", acFreq:0, numUnRead:0, stories:[]});
$ D f0 i E/ u: N) b - feedError = ProcessFeed(transport);. R( u: n" s# l' ]. J% P
- // If successful processFeed returns errorNone
7 N% I; r" e' L; O; k# P0 ^5 A; \ c - //
$ W/ d2 H8 g, \, c - if (feedError === errorNone) {
7 J$ x& n. o2 Y' v5 [2 @5 ? - this.sceneAssistant.feedWgtModel.items = feedList;
, b$ p5 k8 O F1 }/ K' w9 m - this.sceneAssistant.controller.modelChanged(this.sceneAssistant.feedWgtModel);1 Z: ^6 ?" I5 I: ?" l, Y) G; W# d
- this.widget.mojo.close();
% D S- }- N* p- W) }+ n! ^/ X - }
. J$ Y( b) ?( b# f - else {
2 _1 _* q0 G4 P: O A$ P - feedList.pop();5 m- y7 G6 t9 r- a W
- if (feedError == errorUnsupportedFeedType) {
' ]: }* h8 e" D! v3 H - Mojo.Log.info("Feed ", this.urlModel.value," isn't a recognized feed type (#", errorUnsupportedFeedType, ")");
3 o) o4 R& b# }6 v3 y - $("add-feed-title").innerHTML ="Invalid Feed - not a supported feed type";( J# U* L/ n1 M" c% u
- }, {( u+ \6 g4 `5 e
- }
% v$ q# v4 H6 ^" q - };
% l; K0 Z: v1 [7 z4 W5 q N( w - AddDialogAssistant.prototype.checkFailure = function(transport) {
! p, n2 Q& x8 f( E - // Use the Prototype template object to generate a string from the return
+ q6 M: S+ U" ^* l% d+ S - //
/ v) E+ P3 E k9 g& @, Q - var t = new Template($L("#{status}"));
) S0 z6 [) U+ O& l* X2 g6 O - var m = t.evaluate(transport);
+ N( F3 J* o' P( H4 a - // Log error and put message in status" W" \9 S; S9 d. X K
- //% G0 R k2 l# @% `) }) x6 ~
- Mojo.Log.info("Invalid feed (Status ", m, " returned).");* }4 d" ^* c! L# l3 \' P/ |) A5 Z
- $("add-feed-title").innerHTML = "Invalid feed - http failure ("+m+")";
, Y; ?& Z' g9 C+ K5 _! f2 W8 ?" P - };
复制代码 3 J) J; z* y9 l; ~1 y% I' g
: B) K/ V/ E% f/ V" x7 _$ P
9 z( N5 u- x+ GThis example is from the book Palm webOS by Mitch Allen. |
|