File: storage.js

package info (click to toggle)
webdeveloper 1.2.5%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,364 kB
  • ctags: 1,608
  • sloc: makefile: 10
file content (123 lines) | stat: -rw-r--r-- 2,984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var WebDeveloper = WebDeveloper || {};

WebDeveloper.Storage           = WebDeveloper.Storage || {};
WebDeveloper.Storage.storageId = "web-developer";
var WebDeveloper = WebDeveloper || {};

WebDeveloper.Storage              = WebDeveloper.Storage || {};
WebDeveloper.Storage.sessionStore = null;

// Deletes the features on a tab
WebDeveloper.Storage.deleteFeatures = function(tab)
{
  try
  {
    WebDeveloper.Storage.getSessionStore().deleteTabValue(tab, WebDeveloper.Storage.storageId);
  }
  catch(exception)
  {
    // Ignore
  }
};

// Gets the features on a tab
WebDeveloper.Storage.getFeatures = function(tab)
{
  var features = WebDeveloper.Storage.getSessionStore().getTabValue(tab, WebDeveloper.Storage.storageId);

  // If there are features
  if(features)
  {
    return features.split(",");
  }

  return null;
};

// Returns the session store
WebDeveloper.Storage.getSessionStore = function()
{
  // If the session store is not set
  if(!WebDeveloper.Storage.sessionStore)
  {
    // Try to set the session store
    try
    {
      WebDeveloper.Storage.sessionStore = Components.classes["@mozilla.org/browser/sessionstore;1"].getService(Components.interfaces.nsISessionStore);
    }
    catch(exception)
    {
      WebDeveloper.Storage.sessionStore = Components.classes["@mozilla.org/suite/sessionstore;1"].getService(Components.interfaces.nsISessionStore);
    }
  }

  return WebDeveloper.Storage.sessionStore;
};

// Returns true if any feature is active on the current tab
WebDeveloper.Storage.hasFeatures = function()
{
  // If there are features
  if(WebDeveloper.Storage.getFeatures(WebDeveloper.Common.getTabBrowser().selectedTab))
  {
    return true;
  }

  return false;
};

// Returns true if a feature is active on a tab
WebDeveloper.Storage.isFeatureActive = function(feature)
{
  return WebDeveloper.Common.inArray(feature, WebDeveloper.Storage.getFeatures(WebDeveloper.Common.getTabBrowser().selectedTab));
};

// Sets the features on a tab
WebDeveloper.Storage.setFeatures = function(features)
{
  WebDeveloper.Storage.getSessionStore().setTabValue(WebDeveloper.Common.getTabBrowser().selectedTab, WebDeveloper.Storage.storageId, features.join(","));
};

// Toggles a feature on a tab
WebDeveloper.Storage.toggleFeature = function(feature, tab)
{
  var features = null;
  var position = null;

  // If the tab is not set
  if(!tab)
  {
    tab = WebDeveloper.Common.getTabBrowser().selectedTab;
  }

  features = WebDeveloper.Storage.getFeatures(tab);
  position = WebDeveloper.Common.positionInArray(feature, features);

  // If the feature is not on the tab
  if(position == -1)
  {
    // If there are any features
    if(features)
    {
      features.push(feature);
    }
    else
    {
      features = [feature];
    }
  }
  else
  {
    features.splice(position, 1);
  }

  // If there are features
  if(features.length)
  {
    WebDeveloper.Storage.setFeatures(features);
  }
  else
  {
    WebDeveloper.Storage.deleteFeatures(tab);
  }
};