|
ShowDialog() (Custom Dialog) 7 J; r ^' w4 _$ }7 ?9 H
Example usage:- // addNewFeed - triggered by "Add..." item in feed list and invokes the AddDialog0 E$ B+ E1 d* j6 L
- // Assistant defined above./ f9 B0 [- z1 X% \% Q8 n! e
- //2 |- \! h6 }4 h8 e j
- FeedListAssistant.prototype.addNewFeed = function() {
, ^# O6 {# W- z6 L8 x - this.controller.showDialog({
7 j1 R" u" c% k- d( { - template: 'feedList/addFeed-dialog',' a/ G e+ E( t% `% @ E& s
- assistant: new AddDialogAssistant(this); k4 q9 L# ]3 P( M
- }); H; @- B* {# L- t) l& c! X- K
- };
复制代码
" a9 A4 b( e& c3 [
0 }) A+ J* ^5 T- i3 |A 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.% c1 t. D4 G- x( a" [- j( @5 r# R' I
) x! ^) R" h; Y5 aTemplate file:- <div>
: h4 n9 d3 T' k E3 Q; H* \$ `' v - <span id="add-feed-title" class="palm-dialog-title">Add News Feed Source</span>% h; `: ]. ~ ?; \, M% _
- <div class="palm-group unlabeled">* M- v3 h+ D& `0 ]# ]4 j% K: K/ x
- <div class="palm-list">% S# l/ R* J$ q$ y; k
- <div class='palm-row first'>
1 _' B' N0 U# i* p6 Y - <div class="palm-row-wrapper textfield-group" x-mojo-focus-highlight="true">$ h+ H+ O) m2 Y7 \. a- }
- <div class="title">
( N: V! g* T+ b6 v - <div class="label">URL</div>
2 y; p( v& k$ B/ M( r u. p+ J9 F- s - <div id="newFeedURL" x-mojo-element="TextField" align="left"></div>
# `" Y) Z1 o; s# O: P7 D: _* C - </div>
& O2 e2 ?7 V" ]7 T! o - </div>
4 T7 D) ~/ d9 c$ O! G) a+ P( F - </div>" M I8 q7 K1 g8 T
- <div class='palm-row last'>
- Q. y y. z7 |% D; _; g" z - <div class="palm-row-wrapper textfield-group" x-mojo-focus-highlight="true">1 ]$ v+ r C- n
- <div class="title">
( t( p& [% `" J; ?! e" } - <div class="label">Title</div>- e. }6 v1 U9 V" }0 F6 B3 `2 t
- <div id="newFeedName" x-mojo-element="TextField" align="left"></div>3 v7 o3 F+ g6 Y3 ]$ G4 e$ H& D; ]
- </div>: G) g, B8 t( `' }
- </div>' ]3 x3 |" [ A0 l# M
- </div>
( X4 c Q: ^6 s9 r" _7 Z1 f3 { W0 G - </div>8 S% |+ L1 _. O: a# M
- </div>% A& {( M, V9 b5 L
- <div x-mojo-element="Button" id="okButton"></div>+ o* b* H1 T+ Y( |" T
- </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.4 e! j+ u. c1 d5 R- n8 y$ Z
% {. ~! p) ?; e% {3 O$ |# @Assistant file:- // AddDialogAssistant - simple controller for adding new feeds to the list.
1 O+ @/ M9 Q+ Q& f9 K9 E5 b1 v - // Invoked by the FeedListAssistant when the "Add..." list item is selected' f* Z8 V8 E' b* S" f6 g) }: U- P
- // on the feed list.2 y0 b3 n( l$ y* L; m
- //' l( {( _ v: \) Z, X# G$ W7 O) H
- // The dialog displays two text fields (URL and Name) and an OK button.
. l, c0 e4 c% r& c7 C. o& D) ~ - // Either the user enters a feed URL and a name, followed by OK or a/ H$ q& l: [2 s/ B
- // back swipe to close. If OK, the feed header is checked through an Ajax
: R; O6 r6 n, r2 L1 b - // request and if valid, the feed updated and dialog closed. If an error,
" c" Q! ~: }% G4 q) C - // the posted in place of title and dialog remains open.
2 i; w) |! }. E% M7 _1 b4 ? - // Swipe back cancels and returns back to the FeedListAssistant.
9 e) a$ [) P" v) t* X# Z3 z0 G - //
1 U1 R6 |# S! G8 s- d8 B7 o - function AddDialogAssistant(sceneAssistant) {0 }& [0 ~# R2 l
- this.sceneAssistant = sceneAssistant;% l# ]! V: [9 X. O0 d
- }
+ _" [+ n7 c- ? - AddDialogAssistant.prototype.setup = function(widget) {
2 s: D# m# ]% D, i* N - this.widget = widget;* _' V t8 r8 Z9 @. u
- // Setup text field for the new feed's URL5 f; O1 Q- M8 G8 ^
- //# T+ p* Q2 M. k2 L
- this.sceneAssistant.controller.setupWidget("newFeedURL",
/ R0 q n& E Z2 U4 @) H, d - this.urlAttributes = {9 X: D8 E# [& g$ G C
- property: "value",6 v* }, M) @# f; @( t4 L
- hintText: "RSS or ATOM feed",
8 e7 x. z9 n+ A/ Y - focus: true,8 Q; U8 H# M1 z. G
- limitResize: true,4 `1 n- ?3 E' H4 `, G \
- textReplacement: false,- w2 A6 c/ Y9 F! d4 g
- enterSubmits: false: @- Z* c+ b1 M4 A6 d/ d
- },4 U8 `5 f! f8 y" @, Z
- this.urlModel = {value : ""}
( M3 G; o! F# x% l/ j - );0 K4 U. K: Z7 [
- // Setup text field for the new feed's name
' J; j, r3 V6 b8 I4 j: h2 P$ H) Z - //" n) [2 [1 c6 E( D* Z
- this.sceneAssistant.controller.setupWidget("newFeedName",
; V/ g: k1 o" h0 C1 H" W - this.nameAttributes = {2 L& p* d% {2 W# Y, T. q- p$ H# W$ f
- property: "value",2 E4 J2 `8 p6 H- e0 x. U
- hintText: "Optional",0 R7 W. i! q# J* X7 X1 ]% z3 b
- limitResize: true,: R" I" L L4 d+ ~
- textReplacement: false,
! J% T1 |* o/ a( ]0 c) C. }$ ] - enterSubmits: false
8 U& T" C6 k0 H) I - },1 _7 x& x8 b# g
- this.nameModel = {value : ""
3 |+ e( r9 j( n9 L$ O9 S - });6 Z' H$ Q( B3 X. ~# p5 ]3 ^
- // Setup button and event handler
" t7 J7 x' t/ h# u$ P, v - //
: s) x* Q/ B) s- [ - this.sceneAssistant.controller.setupWidget("okButton", ?8 _" T- F* L, F! h9 L; W
- this.attributes = {},
3 U. b6 [, S! I Z3 ~ - this.model = {2 D6 L9 @! }) m: G# L
- buttonLabel: "Add Feed",
* Y+ d. I7 j$ u; H4 w/ } - buttonClass: "addFeedButton",4 D. w# X8 I: l5 T/ n* d; \; X( F
- disabled: false
9 U$ e' _. E4 u - }" T, ^, q$ n' V2 F1 B4 m. ?. D
- );
Y' U- G* |+ B: y$ H9 a5 _8 E - Mojo.Event.listen($('okButton'), Mojo.Event.tap, this.checkIt.bindAsEventListener(this));" E* G( R( H& {5 F
- };. W" S1 Z+ D6 u$ Z3 J. s, X: @! D
- // ------------------------------------------------------------------------
6 l" `8 X: V" X* W& O6 ? - // Add Feed Functions" ]8 K/ u9 e. G4 j; F5 M
- //1 B; [2 ~9 k$ l' Q2 x5 ?
- // checkIt - called when Add feed OK button is clicked' U* V, M: K* v. ^7 S5 t5 ~: H
- //
9 Q, e: v: H6 t1 q$ | - AddDialogAssistant.prototype.checkIt = function() {
+ @; U3 Z% I3 r& k - // Check for 'http://' on front or other legal prefix$ j9 d/ k$ y5 U0 k% H, E6 ^ {8 K
- // assume any string of 1 to 5 alpha characters followed
; I( y: x. {( C% P6 H% Q - // by ':' is legal, otherwise prepend "http://"+ M/ l u. z: q+ Z' j/ B
- //
7 d( p8 _: G5 \8 H" p0 J - var url = this.urlModel.value;
6 P5 X/ X' ?- ? - if (/^[a-z]{1,5}:/.test(url) === false) {9 L5 f2 T, ^3 w& j- j
- url = url.replace(/^\/{1,2}/,"");0 A, u4 B: D3 z
- url = "http://"+url;
! A. H$ R5 I, u - }
; \/ U2 S) Z+ Z+ J" P0 k, ?9 D - // Update the submitted URL text model
' k) _, p6 Z- N* j( Q. e - this.urlModel.value = url;1 t' r9 U' d; j& R7 F: K# \# m
- this.sceneAssistant.controller.modelChanged(this.urlModel);
& E; x o! B: m2 L* \* W2 {( ?" j - var request = new Ajax.Request(url, {% Q3 q4 H) X& N, o. R0 h
- method: 'get',
. `- y/ W, q& \! S% ? - evalJSON: 'false',
0 j1 q9 D7 _3 j% @) M3 G) k' ] - onSuccess: this.checkOK.bind(this),. u* g7 C* i) ` w% z# e
- onFailure: this.checkFailure.bind(this)
0 ~) Q4 `$ L9 ^- I5 E* V- }4 P - });
5 W2 Q) P: T; o8 k" X* H - };" K5 o1 @/ d% ~4 B4 {( A: k6 W
- //, R' w' Z6 K4 S& P [. L
- // checkOK - called when the Ajax request is successful.% p% z' c& {1 j6 g& W1 B
- //
& ^( ]3 f1 }/ P* K6 S" o! Y2 k+ x - AddDialogAssistant.prototype.checkOK = function(transport) {
/ Y; w5 Z- x# y - // DEBUG - log the result
4 z8 s U% s& G( A* M - //$ v1 f$ K7 r' m/ c3 y4 i8 Y6 ]" L
- var t = new Template("Status #{status} returned from newsfeed request.");* J- t& ?7 `' d* ?* Z' i; ^
- var success = "Feed Request Success: "+t.evaluate(transport);
1 B @4 p6 X; l: b$ O5 C# z - Mojo.Log.info(success);/ @# X t9 U' A" I1 Z9 j' }5 e
- // Work around when XML comes back in text response
% ]0 c, ^- E, @& e1 O% G - if (transport.responseXML === null && transport.responseText !== null) {$ J7 }/ P* b, N/ Z! |
- Mojo.Log.info("••••••••","Request not in XML format - manually converting");1 `" G, d( X7 R
- transport.responseXML = new DOMParser().parseFromString (transport.responseText, 'text/xml');
. Y- F. X& E+ |- K* i* ?: w6 g - }& |' g- }7 F A0 C# k4 Y# T
- var feedError = errorNone;7 l4 \$ G4 G" w# T$ Z
- // Push the entered feed onto feedlist and call processFeed to evaluate it.( k/ D4 M" X+ S2 D2 C; H
- // This is mainly to record the user's TITLE entry before processing
8 V* B v, R2 t+ J - //4 r' {7 G: H3 i/ G, i% g; J
- feedList.push({title:this.nameModel.value, url:this.urlModel.value, type:"", acFreq:0, numUnRead:0, stories:[]});
7 n+ S; _! j5 {1 i0 {* I - feedError = ProcessFeed(transport);
$ L/ {, }3 I! R3 h: J# l. E - // If successful processFeed returns errorNone
( l" [2 a8 } x h6 ~- D/ j0 g" H - //
" a/ {8 F) l9 ^/ L+ u - if (feedError === errorNone) {9 D l0 Q. C# v/ ^) I
- this.sceneAssistant.feedWgtModel.items = feedList; W; ~* d- X" A: C9 |, d. r
- this.sceneAssistant.controller.modelChanged(this.sceneAssistant.feedWgtModel);4 B5 Z' s* o0 o! f) Q' h, \
- this.widget.mojo.close();6 I& q ~& u' e0 p8 N+ K1 P
- }( Q& J6 b8 u) C* D" z; J# I
- else {
3 `6 w% K$ l! X6 ^ - feedList.pop();( w+ Q1 r$ J/ l0 }/ w
- if (feedError == errorUnsupportedFeedType) {
" g2 K7 s! {; t) E) K$ t( v( ?5 P - Mojo.Log.info("Feed ", this.urlModel.value," isn't a recognized feed type (#", errorUnsupportedFeedType, ")");
" P- ^8 U# D1 Z$ C5 @, I - $("add-feed-title").innerHTML ="Invalid Feed - not a supported feed type";
) i/ _9 a5 Z1 C6 s - }
1 M$ Z# a# B2 m) n& M3 F) q; ] - }$ x# X8 F) u- {1 Y" D4 H
- };0 \: |4 Y( a5 ]; S4 T+ E
- AddDialogAssistant.prototype.checkFailure = function(transport) {
$ w1 Z. ]9 {' G) l/ a) P4 z0 c - // Use the Prototype template object to generate a string from the return
6 M7 ` [4 g7 v L1 a) @ - //0 W, W" r' I ?# y8 M9 H
- var t = new Template($L("#{status}"));* D( x6 b* S. a* b
- var m = t.evaluate(transport);5 v: F2 `( W6 }0 }, z$ O
- // Log error and put message in status0 X3 S8 D; h" g- T$ a6 e0 W
- //
0 \4 @( }5 |* O+ @& ? - Mojo.Log.info("Invalid feed (Status ", m, " returned).");
, w# u3 l9 N; O( L X$ P4 R. N - $("add-feed-title").innerHTML = "Invalid feed - http failure ("+m+")";' p6 }8 P& f- L
- };
复制代码 ) ^4 ?7 q d! X9 Z3 q) N
0 q& L9 k3 Q2 L5 @9 P
2 j/ G! |7 b; H1 V6 o1 `( Y" F" pThis example is from the book Palm webOS by Mitch Allen. |
|