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.

238 lines
6.4 KiB

  1. // Utility functions
  2. // Namespace
  3. Util = {};
  4. // Activity context handling
  5. var app;
  6. Util.context = {
  7. filter: {category: "", text: "", favorite: false},
  8. libraries: null,
  9. library: null,
  10. favorites: {},
  11. readtimes: {},
  12. currentindex: 0
  13. };
  14. Util.saveContext = function() {
  15. if (Util.onSugar() || !app || !app.activity) return;
  16. var datastoreObject = app.activity.getDatastoreObject();
  17. var jsonData = JSON.stringify(Util.context);
  18. datastoreObject.setDataAsText(jsonData);
  19. //console.log("SAVE CONTEXT <"+jsonData+">");
  20. datastoreObject.save(function() {});
  21. };
  22. Util.loadContext = function(callback, loaded) {
  23. if (!Util.onSugar()) {
  24. requirejs(["sugar-web/env"], function (env) {
  25. env.getEnvironment(function(err, environment) {
  26. if (environment.objectId) {
  27. var datastoreObject = app.activity.getDatastoreObject();
  28. datastoreObject.loadAsText(function (error, metadata, data) {
  29. //console.log("LOAD CONTEXT <"+data+">");
  30. var context = JSON.parse(data);
  31. if (context) {
  32. Util.context = context;
  33. app.loadDatabase();
  34. } else {
  35. app.loadLibraries();
  36. }
  37. callback();
  38. });
  39. } else {
  40. app.loadLibraries();
  41. }
  42. });
  43. });
  44. } else {
  45. Util.context = loaded;
  46. app.loadDatabase();
  47. app.hideLibraries();
  48. }
  49. };
  50. // Context update
  51. Util.setFilter = function(newfilter) {
  52. if (newfilter.favorite !== undefined) Util.context.filter.favorite = newfilter.favorite;
  53. if (newfilter.category !== undefined) Util.context.filter.category = newfilter.category;
  54. if (newfilter.text !== undefined) Util.context.filter.text = newfilter.text;
  55. app.filterChanged();
  56. }
  57. Util.getFilter = function() {
  58. return Util.context.filter;
  59. }
  60. Util.getCollection = function() {
  61. var database = Util.database;
  62. var filter = [];
  63. for (var i = 0 ; i < database.length ; i++) {
  64. if (Util.context.filter.favorite && !Util.getFavorite(database[i].id))
  65. continue;
  66. if (Util.context.filter.category.length > 0 && database[i].category != Util.context.filter.category )
  67. continue;
  68. if (Util.context.filter.text.length > 0 && database[i].title.toLowerCase().indexOf(Util.context.filter.text.toLowerCase()) == -1)
  69. continue;
  70. filter.push(database[i]);
  71. }
  72. return filter;
  73. }
  74. Util.setFavorite = function(id, value) {
  75. if (value)
  76. Util.context.favorites[id] = value;
  77. else
  78. Util.context.favorites[id] = undefined;
  79. }
  80. Util.getFavorite = function(id) {
  81. return Util.context.favorites[id];
  82. }
  83. Util.setReadTime = function(id, time) {
  84. if (time)
  85. Util.context.readtimes[id] = time;
  86. else
  87. Util.context.readtimes[id] = undefined;
  88. }
  89. Util.getReadTime = function(id) {
  90. return Util.context.readtimes[id];
  91. }
  92. Util.database = [];
  93. Util.categories = [];
  94. Util.loadLibraries = function(response, error) {
  95. Util.getLanguage(function(language) {
  96. var ajax = new enyo.Ajax({
  97. url: constant.librariesUrl+"?lang="+language,
  98. method: "GET",
  99. handleAs: "json"
  100. });
  101. ajax.response(function(sender, data) {
  102. Util.context.libraries = data;
  103. response();
  104. });
  105. ajax.error(error);
  106. ajax.go();
  107. });
  108. }
  109. Util.loadDatabase = function(response, error) {
  110. if (Util.context.library == null)
  111. return;
  112. Util.getLanguage(function(language) {
  113. var url = Util.context.library.database.replace(new RegExp("%language%", "g"),language);
  114. if (document.location.protocol == "https:") {
  115. url = url.replace("http://", "https://");
  116. }
  117. var ajax = new enyo.Ajax({
  118. url: url,
  119. method: "GET",
  120. handleAs: "json"
  121. });
  122. ajax.response(function(sender, data) {
  123. // Store date base loaded
  124. Util.database = data;
  125. // Store categories
  126. Util.categories = [];
  127. for (var i = 0 ; i < data.length ; i++) {
  128. var category = data[i].category;
  129. if (category !== undefined) {
  130. var found = false;
  131. for (var j = 0 ; !found && j < Util.categories.length ; j++) {
  132. if (category == Util.categories[j].id) found = true;
  133. }
  134. if (!found) Util.categories.push({id: category, title: category});
  135. }
  136. }
  137. app.getFilter().setCategories(Util.categories);
  138. response(data);
  139. });
  140. ajax.error(error);
  141. ajax.go();
  142. });
  143. }
  144. Util.getDatabase = function() {
  145. return Util.database;
  146. }
  147. Util.getVideos = function() {
  148. return Util.context.library.videos;
  149. }
  150. Util.getImages = function() {
  151. return Util.context.library.images;
  152. }
  153. Util.setIndex = function(index) {
  154. Util.context.currentindex = index;
  155. }
  156. Util.getIndex = function() {
  157. return Util.context.currentindex;
  158. }
  159. Util.setLibrary = function(library) {
  160. Util.context.library = library;
  161. }
  162. Util.getLibrary = function() {
  163. return Util.context.library;
  164. }
  165. Util.addLibrary = function(library) {
  166. Util.context.libraries.push(library);
  167. }
  168. Util.removeLibrary = function(library) {
  169. if (Util.context.library == library || Util.context.libraries.length == 1)
  170. return;
  171. var newlibraries = [];
  172. for (var i = 0 ; i < Util.context.libraries.length ; i++) {
  173. if (Util.context.libraries[i] != library)
  174. newlibraries.push(Util.context.libraries[i]);
  175. }
  176. Util.context.libraries = newlibraries;
  177. }
  178. // Misc
  179. Util.onSugar = function() {
  180. var getUrlParameter = function(name) {
  181. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  182. return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
  183. };
  184. return getUrlParameter("onsugar");
  185. }
  186. Util.getLanguage = function(callback) {
  187. if (Util.onSugar()) {
  188. callback(navigator.language);
  189. return;
  190. }
  191. if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
  192. chrome.storage.local.get('sugar_settings', function(values) {
  193. callback(JSON.parse(values.sugar_settings).language);
  194. });
  195. } else {
  196. callback(JSON.parse(localStorage.sugar_settings).language);
  197. }
  198. }
  199. // Encoding functions taken from
  200. // https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
  201. function uint6ToB64 (nUint6) {
  202. return nUint6 < 26 ?
  203. nUint6 + 65 : nUint6 < 52 ?
  204. nUint6 + 71 : nUint6 < 62 ?
  205. nUint6 - 4 : nUint6 === 62 ?
  206. 43 : nUint6 === 63 ?
  207. 47 : 65;
  208. }
  209. Util.toBase64 = function(aBytes) {
  210. var eqLen = (3 - (aBytes.length % 3)) % 3, sB64Enc = "";
  211. for (var nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
  212. nMod3 = nIdx % 3;
  213. nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
  214. if (nMod3 === 2 || aBytes.length - nIdx === 1) {
  215. sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
  216. nUint24 = 0;
  217. }
  218. }
  219. return eqLen === 0 ? sB64Enc : sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? "=" : "==");
  220. }