Platform-Browser.st 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. Smalltalk createPackage: 'Platform-Browser'!
  2. Object subclass: #BrowserInterface
  3. instanceVariableNames: ''
  4. package: 'Platform-Browser'!
  5. !BrowserInterface commentStamp!
  6. I am platform interface class that tries to use window and jQuery; that is, one for browser environment.
  7. ## API
  8. self isAvailable. "true if window and jQuery exist".
  9. self alert: 'Hey, there is a problem'.
  10. self confirm: 'Affirmative?'.
  11. self prompt: 'Your name:'.
  12. self ajax: #{
  13. 'url' -> '/patch.js'. 'type' -> 'GET'. dataType->'script'
  14. }.!
  15. !BrowserInterface methodsFor: 'actions'!
  16. ajax: anObject
  17. self deprecatedAPI: 'Use newXhr or dedicated library.'.
  18. ^ JQuery
  19. ifNil: [ self error: 'JQuery wrapper not loaded, cannot do AJAX.' ]
  20. ifNotNil: [ JQuery current ajax: anObject ]
  21. !
  22. alert: aString
  23. ^ window alert: aString
  24. !
  25. confirm: aString
  26. ^ window confirm: aString
  27. !
  28. newXhr
  29. ^ XMLHttpRequest new
  30. !
  31. prompt: aString
  32. ^ window prompt: aString
  33. !
  34. prompt: aString default: defaultString
  35. ^ window prompt: aString default: defaultString
  36. ! !
  37. !BrowserInterface methodsFor: 'testing'!
  38. isAvailable
  39. <return typeof window !!== "undefined" && typeof jQuery !!== "undefined">
  40. ! !
  41. Object subclass: #BrowserTerminal
  42. instanceVariableNames: ''
  43. package: 'Platform-Browser'!
  44. !BrowserTerminal commentStamp!
  45. I am `Terminal` service implementation for browser.!
  46. !BrowserTerminal methodsFor: 'actions'!
  47. alert: aString
  48. ^ window alert: aString
  49. !
  50. confirm: aString
  51. ^ window confirm: aString
  52. !
  53. prompt: aString
  54. ^ window prompt: aString
  55. !
  56. prompt: aString default: defaultString
  57. ^ window prompt: aString default: defaultString
  58. ! !
  59. !BrowserTerminal class methodsFor: 'testing'!
  60. initialize
  61. self isFeasible ifTrue: [ Terminal registerIfNone: self new ]
  62. !
  63. isFeasible
  64. <return typeof window !!== "undefined">
  65. ! !
  66. !Object methodsFor: '*Platform-Browser'!
  67. postMessageTo: aFrame
  68. ^ self postMessageTo: aFrame origin: '*'
  69. !
  70. postMessageTo: aFrame origin: aString
  71. <return aFrame.postMessage(self, aString)>
  72. ! !