not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.3 KiB

  1. define(function () {
  2. 'use strict';
  3. var env = {};
  4. env.getEnvironment = function (callback) {
  5. // FIXME: we assume this code runs on the same thread as the
  6. // javascript executed from sugar-toolkit-gtk3 (python)
  7. if (env.isSugarizer()) {
  8. var getUrlParameter = function(name) {
  9. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  10. return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
  11. };
  12. window.top.sugar = {};
  13. window.top.sugar.environment = {
  14. activityId: getUrlParameter("aid"),
  15. activityName: getUrlParameter("n"),
  16. bundleId: getUrlParameter("a"),
  17. objectId: getUrlParameter("o"),
  18. sharedId: getUrlParameter("s"),
  19. help: getUrlParameter("h"),
  20. standAlone: getUrlParameter("sa")
  21. };
  22. if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
  23. chrome.storage.local.get('sugar_settings', function(values) {
  24. window.top.sugar.environment.user = JSON.parse(values.sugar_settings);
  25. callback(null, window.top.sugar.environment);
  26. });
  27. } else {
  28. window.top.sugar.environment.user = JSON.parse(localStorage.sugar_settings);
  29. setTimeout(function () {
  30. callback(null, window.top.sugar.environment);
  31. }, 0);
  32. }
  33. } else if (env.isStandalone()) {
  34. setTimeout(function () {
  35. callback(null, {});
  36. }, 0);
  37. } else {
  38. var environmentCallback = function () {
  39. callback(null, window.top.sugar.environment);
  40. };
  41. if (window.top.sugar) {
  42. setTimeout(function () {
  43. environmentCallback();
  44. }, 0);
  45. } else {
  46. window.top.sugar = {};
  47. window.top.sugar.onEnvironmentSet = function () {
  48. environmentCallback();
  49. };
  50. }
  51. }
  52. };
  53. env.getObjectId = function (callback) {
  54. env.getEnvironment(function (error, environment) {
  55. callback(environment.objectId);
  56. });
  57. };
  58. env.getURLScheme = function () {
  59. return window.location.protocol;
  60. };
  61. env.getHost = function() {
  62. return window.location.hostname;
  63. };
  64. env.isStandalone = function () {
  65. var webActivityURLScheme = "activity:";
  66. var fileURLScheme = "file:";
  67. var currentURLScheme = env.getURLScheme();
  68. // the control of hostname !== '0.0.0.0' is used
  69. // for compatibility with F18 and webkit1
  70. return currentURLScheme !== webActivityURLScheme &&
  71. currentURLScheme !== fileURLScheme &&
  72. window.location.hostname !== '0.0.0.0';
  73. };
  74. env.isSugarizer = function() {
  75. // HACK: If in Chrome App automatic deduction that in Sugarizer
  76. if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
  77. return true;
  78. } else if (typeof(Storage)!=="undefined" && typeof(window.localStorage)!=="undefined") {
  79. try {
  80. return (window.localStorage.getItem('sugar_settings') !== null);
  81. } catch(err) {
  82. return false;
  83. }
  84. }
  85. return false;
  86. };
  87. env.isSugarizerOS = function() {
  88. if (typeof window.sugarizerOS != 'undefined')
  89. return true;
  90. return false;
  91. }
  92. return env;
  93. });