helpers.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. define(["./boot", "./junk-drawer", "require"], function (boot, $goodies, require) {
  2. var globals = boot.globals,
  3. exports = {},
  4. extend = $goodies.extend,
  5. api = boot.api;
  6. // API
  7. exports.popupHelios = function () {
  8. require(['helios/index'], function (helios) {
  9. helios.popup();
  10. }, function (err) {
  11. window.alert("Error loading helios.\nIf not present, you can install it with 'npm install @ambers/helios --save-dev'.\nThe error follows:\n" + err);
  12. });
  13. };
  14. Object.defineProperty(exports, "api", {
  15. value: api,
  16. enumerable: true, configurable: true, writable: false
  17. });
  18. Object.defineProperty(exports, "globals", {
  19. value: globals,
  20. enumerable: true, configurable: true, writable: false
  21. });
  22. Object.defineProperty(exports, "$nil", {
  23. value: boot.nilAsReceiver,
  24. enumerable: true, configurable: true, writable: false
  25. });
  26. Object.defineProperty(exports, "nil", {
  27. value: boot.nilAsValue,
  28. enumerable: true, configurable: true, writable: false
  29. });
  30. function settingsInLocalStorage () {
  31. var storage;
  32. try {
  33. storage = 'localStorage' in globalThis && globalThis.localStorage;
  34. } catch (ex) {
  35. console.warn("Access denied to localStorage, " +
  36. "settings not loaded nor, subsequently, saved.");
  37. return;
  38. }
  39. if (storage) {
  40. var fromStorage;
  41. try {
  42. fromStorage = JSON.parse(storage.getItem('amber.SmalltalkSettings'));
  43. } catch (ex) {
  44. // pass
  45. }
  46. extend(globals.SmalltalkSettings, fromStorage || {});
  47. if (typeof window !== "undefined") {
  48. window.addEventListener('beforeunload', function () {
  49. storage.setItem('amber.SmalltalkSettings', JSON.stringify(globals.SmalltalkSettings));
  50. });
  51. }
  52. }
  53. }
  54. exports.initialize = function (options) {
  55. return new Promise(function (resolve) {
  56. globals.SmalltalkSettings['transport.defaultAmdNamespace'] = "amber/core";
  57. api.amdRequire = require; // TODO better
  58. settingsInLocalStorage();
  59. extend(globals.SmalltalkSettings, options || {});
  60. resolve(api.initialize());
  61. });
  62. };
  63. exports.loadPackages = function (modules) {
  64. return new Promise(function (resolve, reject) {
  65. require(modules, resolve, reject);
  66. }).then(function () {
  67. return globals.Smalltalk._postLoad();
  68. });
  69. };
  70. exports.beClean = function () {
  71. return globals.Smalltalk._beClean();
  72. };
  73. // Exports
  74. return exports;
  75. });