Platform-Browser.st 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. Smalltalk createPackage: 'Platform-Browser'!
  2. (Smalltalk packageAt: 'Platform-Browser' ifAbsent: [ self error: 'Package not created: Platform-Browser' ]) imports: {'amber/core/Platform-Services'}!
  3. Object subclass: #BrowserPlatform
  4. slots: {}
  5. package: 'Platform-Browser'!
  6. !BrowserPlatform commentStamp!
  7. I am `Platform` service implementation for browser.!
  8. !BrowserPlatform methodsFor: 'accessing'!
  9. globals
  10. ^ window
  11. ! !
  12. !BrowserPlatform methodsFor: 'public API'!
  13. fetch: aStringOrObject
  14. ^ self globals at: #fetch
  15. ifPresent: [ :fetch | fetch value: aStringOrObject ]
  16. ifAbsent: [ Promise signal: 'fetch not available.' ]
  17. !
  18. fetchUrl: aString options: anObject
  19. ^ self globals at: #fetch
  20. ifPresent: [ :fetch | fetch value: aString value: anObject ]
  21. ifAbsent: [ Promise signal: 'fetch not available.' ]
  22. !
  23. newXhr
  24. XMLHttpRequest
  25. ifNotNil: [ ^ NativeFunction constructorOf: XMLHttpRequest ]
  26. ifNil: [ self error: 'XMLHttpRequest not available.' ]
  27. ! !
  28. !BrowserPlatform class methodsFor: 'testing'!
  29. initialize
  30. self isFeasible ifTrue: [ Platform registerIfNone: self new ]
  31. !
  32. isFeasible
  33. <inlineJS: 'return typeof window !!== "undefined"'>
  34. ! !
  35. Object subclass: #BrowserTerminal
  36. slots: {}
  37. package: 'Platform-Browser'!
  38. !BrowserTerminal commentStamp!
  39. I am `Terminal` service implementation for browser.!
  40. !BrowserTerminal methodsFor: 'actions'!
  41. alert: aString
  42. ^ window alert: aString
  43. !
  44. confirm: aString
  45. ^ window confirm: aString
  46. !
  47. prompt: aString
  48. ^ window prompt: aString
  49. !
  50. prompt: aString default: defaultString
  51. ^ window prompt: aString default: defaultString
  52. ! !
  53. !BrowserTerminal class methodsFor: 'testing'!
  54. initialize
  55. self isFeasible ifTrue: [ Terminal registerIfNone: self new ]
  56. !
  57. isFeasible
  58. <inlineJS: 'return typeof window !!== "undefined"'>
  59. ! !
  60. !Object methodsFor: '*Platform-Browser'!
  61. postMessageTo: aFrame
  62. ^ self postMessageTo: aFrame origin: '*'
  63. !
  64. postMessageTo: aFrame origin: aString
  65. <inlineJS: 'return aFrame.postMessage(self, aString)'>
  66. ! !