PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/scripts/collections/configs.js

https://gitlab.com/dannywillems/laverna
JavaScript | 153 lines | 118 code | 26 blank | 9 comment | 7 complexity | a31f12a7688fac9163ac9d8682c2d9f9 MD5 | raw file
  1. /*global define*/
  2. define([
  3. 'underscore',
  4. 'jquery',
  5. 'backbone',
  6. 'models/config',
  7. 'localStorage'
  8. ], function (_, $, Backbone, Config) {
  9. 'use strict';
  10. var Configs = Backbone.Collection.extend({
  11. localStorage: new Backbone.LocalStorage('vimarkable.configs'),
  12. store : 'configs',
  13. model : Config,
  14. configNames: {
  15. 'appVersion': '0.5.0',
  16. 'appProfiles': JSON.stringify(['notes-db']),
  17. 'appLang': '',
  18. 'cloudStorage': '0',
  19. 'dropboxKey': '',
  20. 'pagination': '10',
  21. 'sortnotebooks': 'name',
  22. 'editMode': 'preview',
  23. // Ecnryption settings
  24. 'encrypt': '0',
  25. 'encryptPass': '',
  26. 'encryptSalt': '',
  27. 'encryptIter': '1000',
  28. 'encryptTag': '64',
  29. 'encryptKeySize': '128',
  30. // Keybindings
  31. 'navigateTop': 'k',
  32. 'navigateBottom': 'j',
  33. 'jumpInbox': 'g i',
  34. 'jumpNotebook': 'g n',
  35. 'jumpFavorite': 'g f',
  36. 'jumpRemoved': 'g t',
  37. 'actionsEdit': 'e',
  38. 'actionsOpen': 'o',
  39. 'actionsRemove': 'shift+3',
  40. 'actionsRotateStar': 's',
  41. 'appCreateNote': 'c',
  42. 'appSearch': '/',
  43. 'appKeyboardHelp': '?'
  44. },
  45. /**
  46. * Creates default set of configs
  47. */
  48. firstStart: function () {
  49. var $d = $.Deferred(),
  50. self = this;
  51. function check() {
  52. if (self.length === self.configNames.length) {
  53. $d.resolve(self);
  54. return;
  55. }
  56. _.forEach(self.configNames, function (item, name) {
  57. if (typeof self.get(name) === 'undefined') {
  58. self.create(new Config({ name: name, value: item }));
  59. }
  60. });
  61. $d.resolve(self);
  62. }
  63. $.when(this.fetch()).done(check);
  64. return $d;
  65. },
  66. getConfigs: function () {
  67. var data = {};
  68. _.forEach(this.models, function ( model ) {
  69. data[model.get('name')] = model.get('value');
  70. });
  71. data.appProfiles = JSON.parse(data.appProfiles || this.configNames.appProfiles);
  72. return data;
  73. },
  74. resetFromJSON: function (jsonSettings) {
  75. var newConfs = [];
  76. _.forEach(jsonSettings, function (val, key) {
  77. newConfs.push({name: key, val: val});
  78. });
  79. this.reset(newConfs);
  80. },
  81. createProfile: function (name) {
  82. var profiles = this.get('appProfiles'),
  83. value = JSON.parse(profiles.get('value'));
  84. if ( !name ) {
  85. return;
  86. }
  87. if (_.contains(value, name) === false) {
  88. value.push(name);
  89. profiles.save({ value: JSON.stringify(value) });
  90. }
  91. },
  92. removeProfile: function (name) {
  93. var profiles = this.get('appProfiles'),
  94. value = JSON.parse(profiles.get('value'));
  95. if ( !name ) {
  96. return;
  97. }
  98. if (_.contains(value, name) === true) {
  99. value = _.without(value, name);
  100. profiles.save({ value: JSON.stringify(value) });
  101. window.indexedDB.deleteDatabase(name);
  102. }
  103. },
  104. shortcuts: function () {
  105. var pattern = /(actions|navigate|jump|appCreateNote|appSearch|appKeyboardHelp)/;
  106. return this.filter(function (m) {
  107. pattern.lastIndex = 0;
  108. return pattern.test(m.get('name'));
  109. });
  110. },
  111. /**
  112. * Filter
  113. */
  114. appShortcuts: function () {
  115. var names = ['appCreateNote', 'appSearch', 'appKeyboardHelp'];
  116. return this.filter(function (m) {
  117. return _.contains(names, m.get('name'));
  118. });
  119. },
  120. filterName: function (str) {
  121. return this.filter(function (m) {
  122. return m.get('name').search(str) >= 0;
  123. });
  124. }
  125. });
  126. return Configs;
  127. });