Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

mainHandle.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. cordova.define("cordova-plugin-nativestorage.mainHandle", function(require, exports, module) {
  2. var inBrowser = false;
  3. var NativeStorageError = require('./NativeStorageError');
  4. function isInBrowser() {
  5. inBrowser = (window.cordova && (window.cordova.platformId === 'browser' || window.cordova.platformId === 'osx')) || !(window.phonegap || window.cordova);
  6. return inBrowser;
  7. }
  8. function storageSupportAnalyse() {
  9. if (!isInBrowser()) {
  10. return 0;
  11. //storageHandlerDelegate = exec;
  12. } else {
  13. if (window.localStorage) {
  14. return 1;
  15. //storageHandlerDelegate = localStorageHandle;
  16. } else {
  17. return 2;
  18. //console.log("ALERT! localstorage isn't supported");
  19. }
  20. }
  21. }
  22. //if storage not available gracefully fails, no error message for now
  23. function StorageHandle() {
  24. this.storageSupport = storageSupportAnalyse();
  25. switch (this.storageSupport) {
  26. case 0:
  27. var exec = require('cordova/exec');
  28. this.storageHandlerDelegate = exec;
  29. break;
  30. case 1:
  31. var localStorageHandle = require('./LocalStorageHandle');
  32. this.storageHandlerDelegate = localStorageHandle;
  33. break;
  34. case 2:
  35. console.log("ALERT! localstorage isn't supported");
  36. break;
  37. default:
  38. console.log("StorageSupport Error");
  39. break;
  40. }
  41. }
  42. StorageHandle.prototype.initWithSuiteName = function(suiteName, success, error) {
  43. if (suiteName === null) {
  44. error("Null suiteName isn't supported");
  45. return;
  46. }
  47. this.storageHandlerDelegate(success, error, "NativeStorage", "initWithSuiteName", [suiteName]);
  48. };
  49. StorageHandle.prototype.set = function(reference, value, success, error) {
  50. //if error is null then replace with empty function to silence warnings
  51. if(!error){
  52. error = function(){};
  53. }
  54. if (reference === null) {
  55. error("The reference can't be null");
  56. return;
  57. }
  58. if (value === null) {
  59. error("a Null value isn't supported");
  60. return;
  61. }
  62. switch (typeof value) {
  63. case 'undefined':
  64. error("an undefined type isn't supported");
  65. break;
  66. case 'boolean': {
  67. this.putBoolean(reference, value, success, error);
  68. break;
  69. }
  70. case 'number': {
  71. // Good now check if it's a float or an int
  72. if (value === +value) {
  73. if (value === (value | 0)) {
  74. // it's an int
  75. this.putInt(reference, value, success, error);
  76. } else if (value !== (value | 0)) {
  77. this.putDouble(reference, value, success, error);
  78. }
  79. } else {
  80. error("The value doesn't seem to be a number");
  81. }
  82. break;
  83. }
  84. case 'string': {
  85. this.putString(reference, value, success, error);
  86. break;
  87. }
  88. case 'object': {
  89. this.putObject(reference, value, success, error);
  90. break;
  91. }
  92. default:
  93. error("The type isn't supported or isn't been recognized");
  94. break;
  95. }
  96. };
  97. /* removing */
  98. StorageHandle.prototype.remove = function(reference, success, error) {
  99. //if error is null then replace with empty function to silence warnings
  100. if(!error){
  101. error = function(){};
  102. }
  103. if (reference === null) {
  104. error("Null reference isn't supported");
  105. return;
  106. }
  107. if (inBrowser) {
  108. try {
  109. localStorage.removeItem(reference);
  110. success();
  111. } catch (e) {
  112. error(e);
  113. }
  114. } else {
  115. this.storageHandlerDelegate(success, error, "NativeStorage", "remove", [reference]);
  116. }
  117. };
  118. /* clearing */
  119. StorageHandle.prototype.clear = function(success, error) {
  120. //if error is null then replace with empty function to silence warnings
  121. if(!error){
  122. error = function(){};
  123. }
  124. if (inBrowser) {
  125. try {
  126. localStorage.clear();
  127. success();
  128. } catch (e) {
  129. error(e);
  130. }
  131. } else {
  132. this.storageHandlerDelegate(success, error, "NativeStorage", "clear", []);
  133. }
  134. };
  135. /* boolean storage */
  136. StorageHandle.prototype.putBoolean = function(reference, aBoolean, success, error) {
  137. //if error is null then replace with empty function to silence warnings
  138. if(!error){
  139. error = function(){};
  140. }
  141. if (reference === null) {
  142. error("Null reference isn't supported");
  143. return;
  144. }
  145. if (aBoolean === null) {
  146. error("a Null value isn't supported");
  147. return;
  148. }
  149. if (typeof aBoolean === 'boolean') {
  150. this.storageHandlerDelegate(function(returnedBool) {
  151. if ('string' === typeof returnedBool) {
  152. if ( (returnedBool === 'true') ) {
  153. success(true);
  154. } else if ( (returnedBool === 'false') ) {
  155. success(false);
  156. } else {
  157. error("The returned boolean from SharedPreferences was not recognized: " + returnedBool);
  158. }
  159. } else {
  160. success(returnedBool);
  161. }
  162. }, error, "NativeStorage", "putBoolean", [reference, aBoolean]);
  163. } else {
  164. error("Only boolean types are supported");
  165. }
  166. };
  167. StorageHandle.prototype.getBoolean = function(reference, success, error) {
  168. //if error is null then replace with empty function to silence warnings
  169. if(!error){
  170. error = function(){};
  171. }
  172. if (reference === null) {
  173. error("Null reference isn't supported");
  174. return;
  175. }
  176. this.storageHandlerDelegate(function(returnedBool) {
  177. if ('string' === typeof returnedBool) {
  178. if ( (returnedBool === 'true') ) {
  179. success(true);
  180. } else if ( (returnedBool === 'false') ) {
  181. success(false);
  182. } else {
  183. error("The returned boolean from SharedPreferences was not recognized: " + returnedBool);
  184. }
  185. } else {
  186. success(returnedBool);
  187. }
  188. }, error, "NativeStorage", "getBoolean", [reference]);
  189. };
  190. /* int storage */
  191. StorageHandle.prototype.putInt = function(reference, anInt, success, error) {
  192. //if error is null then replace with empty function to silence warnings
  193. if(!error){
  194. error = function(){};
  195. }
  196. if (reference === null) {
  197. error("Null reference isn't supported");
  198. return;
  199. }
  200. this.storageHandlerDelegate(success, error, "NativeStorage", "putInt", [reference, anInt]);
  201. };
  202. StorageHandle.prototype.getInt = function(reference, success, error) {
  203. //if error is null then replace with empty function to silence warnings
  204. if(!error){
  205. error = function(){};
  206. }
  207. if (reference === null) {
  208. error("Null reference isn't supported");
  209. return;
  210. }
  211. this.storageHandlerDelegate(success, error, "NativeStorage", "getInt", [reference]);
  212. };
  213. /* float storage */
  214. StorageHandle.prototype.putDouble = function(reference, aFloat, success, error) {
  215. //if error is null then replace with empty function to silence warnings
  216. if(!error){
  217. error = function(){};
  218. }
  219. if (reference === null) {
  220. error("Null reference isn't supported");
  221. return;
  222. }
  223. this.storageHandlerDelegate(success, error, "NativeStorage", "putDouble", [reference, aFloat]);
  224. };
  225. StorageHandle.prototype.getDouble = function(reference, success, error) {
  226. //if error is null then replace with empty function to silence warnings
  227. if(!error){
  228. error = function(){};
  229. }
  230. if (reference === null) {
  231. error("Null reference isn't supported");
  232. return;
  233. }
  234. this.storageHandlerDelegate(function(data) {
  235. if (isNaN(data)) {
  236. error('Expected double but got non-number');
  237. } else {
  238. success(parseFloat(data));
  239. }
  240. }, error, "NativeStorage", "getDouble", [reference]);
  241. };
  242. /* string storage */
  243. StorageHandle.prototype.putString = function(reference, s, success, error) {
  244. //if error is null then replace with empty function to silence warnings
  245. if(!error){
  246. error = function(){};
  247. }
  248. if (reference === null) {
  249. error("Null reference isn't supported");
  250. return;
  251. }
  252. this.storageHandlerDelegate(success, error, "NativeStorage", "putString", [reference, s]);
  253. };
  254. StorageHandle.prototype.getString = function(reference, success, error) {
  255. //if error is null then replace with empty function to silence warnings
  256. if(!error){
  257. error = function(){};
  258. }
  259. if (reference === null) {
  260. error("Null reference isn't supported");
  261. return;
  262. }
  263. this.storageHandlerDelegate(success, error, "NativeStorage", "getString", [reference]);
  264. };
  265. /* object storage COMPOSITE AND DOESNT CARE FOR BROWSER*/
  266. StorageHandle.prototype.putObject = function(reference, obj, success, error) {
  267. //if error is null then replace with empty function to silence warnings
  268. if(!error){
  269. error = function(){};
  270. }
  271. var objAsString = "";
  272. try {
  273. objAsString = JSON.stringify(obj);
  274. } catch (err) {
  275. error(err);
  276. }
  277. this.putString(reference, objAsString, function(data) {
  278. var obj = {};
  279. try {
  280. obj = JSON.parse(data);
  281. success(obj);
  282. } catch (err) {
  283. error(err);
  284. }
  285. }, error);
  286. };
  287. StorageHandle.prototype.getObject = function(reference, success, error) {
  288. //if error is null then replace with empty function to silence warnings
  289. if(!error){
  290. error = function(){};
  291. }
  292. this.getString(reference, function(data) {
  293. var obj = {};
  294. try {
  295. obj = JSON.parse(data);
  296. success(obj);
  297. } catch (err) {
  298. error(err);
  299. }
  300. }, error);
  301. };
  302. /* API >= 2 */
  303. StorageHandle.prototype.setItem = function(reference, obj, success, error) {
  304. //if error is null then replace with empty function to silence warnings
  305. if(!error){
  306. error = function(){};
  307. }
  308. var objAsString = "";
  309. try {
  310. objAsString = JSON.stringify(obj);
  311. } catch (err) {
  312. error(new NativeStorageError(NativeStorageError.JSON_ERROR, "JS", err));
  313. return;
  314. }
  315. if (reference === null) {
  316. error(new NativeStorageError(NativeStorageError.NULL_REFERENCE, "JS", ""));
  317. return;
  318. }
  319. this.storageHandlerDelegate(function(data) {
  320. try {
  321. obj = JSON.parse(data);
  322. success(obj);
  323. } catch (err) {
  324. error(new NativeStorageError(NativeStorageError.JSON_ERROR, "JS", err));
  325. }
  326. }, function(code) {
  327. error(new NativeStorageError(code, "Native", ""));
  328. }, "NativeStorage", "setItem", [reference, objAsString]);
  329. };
  330. StorageHandle.prototype.getItem = function(reference, success, error) {
  331. //if error is null then replace with empty function to silence warnings
  332. if(!error){
  333. error = function(){};
  334. }
  335. if (reference === null) {
  336. error(new NativeStorageError(NativeStorageError.NULL_REFERENCE, "JS", ""));
  337. return;
  338. }
  339. var obj = {};
  340. this.storageHandlerDelegate(
  341. function(data) {
  342. try {
  343. obj = JSON.parse(data);
  344. success(obj);
  345. } catch (err) {
  346. error(new NativeStorageError(NativeStorageError.JSON_ERROR, "JS", err));
  347. }
  348. },
  349. function(code) {
  350. error(new NativeStorageError(code, "Native", ""));
  351. }, "NativeStorage", "getItem", [reference]);
  352. };
  353. /* API >= 2 */
  354. StorageHandle.prototype.setSecretItem = function(reference, obj, encryptConfig, success, error) {
  355. //if error is null then replace with empty function to silence warnings
  356. if(!error){
  357. error = function(){};
  358. }
  359. var objAsString = "";
  360. try {
  361. objAsString = JSON.stringify(obj);
  362. } catch (err) {
  363. error(new NativeStorageError(NativeStorageError.JSON_ERROR, "JS", err));
  364. return;
  365. }
  366. if (reference === null) {
  367. error(new NativeStorageError(NativeStorageError.NULL_REFERENCE, "JS", ""));
  368. return;
  369. }
  370. var action = "setItem";
  371. var params = [reference, objAsString];
  372. switch (encryptConfig.mode) {
  373. case "password":
  374. action = "setItemWithPassword";
  375. params = [reference, objAsString, encryptConfig.value];
  376. break;
  377. case "key":
  378. action = "setItemWithKey";
  379. break;
  380. case "none":
  381. break;
  382. default: {
  383. error(new NativeStorageError(NativeStorageError.WRONG_PARAMETER, "JS", ""));
  384. return;
  385. }
  386. }
  387. this.storageHandlerDelegate(function(data) {
  388. try {
  389. obj = JSON.parse(data);
  390. success(obj);
  391. } catch (err) {
  392. error(new NativeStorageError(NativeStorageError.JSON_ERROR, "JS", err));
  393. }
  394. }, function(code) {
  395. error(new NativeStorageError(code, "Native", ""));
  396. }, "NativeStorage", action, params);
  397. };
  398. StorageHandle.prototype.getSecretItem = function(reference, encryptConfig, success, error) {
  399. //if error is null then replace with empty function to silence warnings
  400. if(!error){
  401. error = function(){};
  402. }
  403. if (reference === null) {
  404. error(new NativeStorageError(NativeStorageError.NULL_REFERENCE, "JS", ""));
  405. return;
  406. }
  407. var obj = {};
  408. var action = "getItem";
  409. var params = [reference];
  410. switch (encryptConfig.mode) {
  411. case "password":
  412. action = "getItemWithPassword";
  413. params = [reference, encryptConfig.value];
  414. break;
  415. case "key":
  416. action = "getItemWithKey";
  417. break;
  418. case "none":
  419. break;
  420. default: {
  421. error(new NativeStorageError(NativeStorageError.WRONG_PARAMETER, "JS", ""));
  422. return;
  423. }
  424. }
  425. this.storageHandlerDelegate(
  426. function(data) {
  427. try {
  428. obj = JSON.parse(data);
  429. success(obj);
  430. } catch (err) {
  431. error(new NativeStorageError(NativeStorageError.JSON_ERROR, "JS", err));
  432. }
  433. },
  434. function(code) {
  435. error(new NativeStorageError(code, "Native", ""));
  436. }, "NativeStorage", action, params);
  437. };
  438. /* list keys */
  439. StorageHandle.prototype.keys = function(success, error) {
  440. //if error is null then replace with empty function to silence warnings
  441. if(!error){
  442. error = function(){};
  443. }
  444. this.storageHandlerDelegate(success, error, "NativeStorage", "keys", []);
  445. };
  446. var storageHandle = new StorageHandle();
  447. module.exports = storageHandle;
  448. });