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

mainHandle.js 13KB

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