|
| 1 | +" |
| 2 | +I implement a minimal browser application. |
| 3 | +
|
| 4 | +I am a version that doesn't need a DNU infrastructure. |
| 5 | +" |
| 6 | +Class { |
| 7 | + #name : #PjDrawRectangleNoDnuApp, |
| 8 | + #superclass : #PjDrawRectangleApp, |
| 9 | + #category : #'PharoJsExamples-DrawRectangle' |
| 10 | +} |
| 11 | + |
| 12 | +{ #category : #'initialize - release' } |
| 13 | +PjDrawRectangleNoDnuApp >> setupDOM [ |
| 14 | + rectangle := document js_createElement: 'div'. |
| 15 | + rectangle js_at:#id put: 'grn'. |
| 16 | + colour := 'green'. |
| 17 | + (rectangle js_at:#style) |
| 18 | + js_at:#backgroundColor put: colour; |
| 19 | + js_at:#height put: '3cm'; |
| 20 | + js_at:#width put: '3cm'; |
| 21 | + js_at:#margin put: '-1.5cm'; |
| 22 | + js_at:#position put: #absolute; |
| 23 | + js_at:#left put: '100px'; |
| 24 | + js_at:#top put: '150px'. |
| 25 | + rectangle js_at:#innerHTML put: 'Clck to ','' capitalized,'change' capitalized,' colour'. |
| 26 | + rectangle js_addEventListener: #click block: [ : ev | |
| 27 | + colour = 'green' ifTrue: [ |
| 28 | + colour := 'pink'. |
| 29 | + (rectangle js_at:#style) js_at:#backgroundColor put: colour |
| 30 | + ] ifFalse: [ |
| 31 | + colour = 'pink' ifTrue: [ |
| 32 | + colour := 'yellow'. |
| 33 | + (rectangle js_at:#style) js_at:#backgroundColor put: colour |
| 34 | + ] ifFalse: [ |
| 35 | +" self flag: 'TODO: firefox requires the listener in removeEventListener'. |
| 36 | +" (document js_at:#body) js_removeChild: rectangle. |
| 37 | + self stop |
| 38 | + ]] |
| 39 | + ]. |
| 40 | + |
| 41 | + (document js_at:#body) |
| 42 | + js_appendChild: rectangle; |
| 43 | + js_addEventListener: #click block: [ : ev | | x y | |
| 44 | + x := ev js_at:#pageX. |
| 45 | + y := ev js_at:#pageY. |
| 46 | + (rectangle js_at:#style) |
| 47 | + js_at:#left put: x asString,'px'; |
| 48 | + js_at:#top put: y asString,'px' |
| 49 | + ] |
| 50 | +] |
| 51 | + |
| 52 | +{ #category : #'initialize - release' } |
| 53 | +PjDrawRectangleNoDnuApp >> subscribeToDeviceEvents [ |
| 54 | + " Bind any events that are required on startup. Common events are: |
| 55 | + 'load', 'deviceready', 'offline', and 'online'." |
| 56 | + super subscribeToDeviceEvents. |
| 57 | + document js_addEventListener: 'load' block: [: ev | self onLoad]. |
| 58 | + |
| 59 | +] |
0 commit comments