summaryrefslogtreecommitdiffstats
path: root/services/cloudsync/docs/example.rst
blob: 33d0f05315c97f1874c99be74c0d7602d0e395b8 (plain)
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
124
125
126
127
128
129
130
131
132
.. _cloudsync_example:

=======
Example
=======

.. code-block:: javascript

	Cu.import("resource://gre/modules/CloudSync.jsm");

	let HelloWorld = {
	  onLoad: function() {
	    let cloudSync = CloudSync();
	    console.log("CLOUDSYNC -- hello world", cloudSync.local.id, cloudSync.local.name, cloudSync.adapters);
	    cloudSync.adapters.register('helloworld', {});
	    console.log("CLOUDSYNC -- " + JSON.stringify(cloudSync.adapters.getAdapterNames()));


	    cloudSync.tabs.addEventListener("change", function() {
	      console.log("tab change");
	      cloudSync.tabs.getLocalTabs().then(
	        function(records) {
	          console.log(JSON.stringify(records));
	        }
	      );
	    });

	    cloudSync.tabs.getLocalTabs().then(
	      function(records) {
	        console.log(JSON.stringify(records));
	      }
	    );

	    let remoteClient = {
	      id: "001",
	      name: "FakeClient",
	    };
	    let remoteTabs1 = [
	      {url:"https://www.google.ca",title:"Google",icon:"https://www.google.ca/favicon.ico",lastUsed:Date.now()},
	    ];
	    let remoteTabs2 = [
	      {url:"https://www.google.ca",title:"Google Canada",icon:"https://www.google.ca/favicon.ico",lastUsed:Date.now()},
	      {url:"http://www.reddit.com",title:"Reddit",icon:"http://www.reddit.com/favicon.ico",lastUsed:Date.now()},
	    ];
	    cloudSync.tabs.mergeRemoteTabs(remoteClient, remoteTabs1).then(
	      function() {
	        return cloudSync.tabs.mergeRemoteTabs(remoteClient, remoteTabs2);
	      }
	    ).then(
	      function() {
	        return cloudSync.tabs.getRemoteTabs();
	      }
	    ).then(
	      function(tabs) {
	        console.log("remote tabs:", tabs);
	      }
	    );

	    cloudSync.bookmarks.getRootFolder("Hello World").then(
	      function(rootFolder) {
	        console.log(rootFolder.name, rootFolder.id);
	        rootFolder.addEventListener("add", function(guid) {
	          console.log("CLOUDSYNC -- bookmark item added: " + guid);
	          rootFolder.getLocalItemsById([guid]).then(
	            function(items) {
	              console.log("CLOUDSYNC -- items: " + JSON.stringify(items));
	            }
	          );
	        });
	        rootFolder.addEventListener("remove", function(guid) {
	          console.log("CLOUDSYNC -- bookmark item removed: " + guid);
	          rootFolder.getLocalItemsById([guid]).then(
	            function(items) {
	              console.log("CLOUDSYNC -- items: " + JSON.stringify(items));
	            }
	          );
	        });
	        rootFolder.addEventListener("change", function(guid) {
	          console.log("CLOUDSYNC -- bookmark item changed: " + guid);
	          rootFolder.getLocalItemsById([guid]).then(
	            function(items) {
	              console.log("CLOUDSYNC -- items: " + JSON.stringify(items));
	            }
	          );
	        });
	        rootFolder.addEventListener("move", function(guid) {
	          console.log("CLOUDSYNC -- bookmark item moved: " + guid);
	          rootFolder.getLocalItemsById([guid]).then(
	            function(items) {
	              console.log("CLOUDSYNC -- items: " + JSON.stringify(items));
	            }
	          );
	        });

	        function logLocalItems() {
	          return rootFolder.getLocalItems().then(
	            function(items) {
	              console.log("CLOUDSYNC -- local items: " + JSON.stringify(items));
	            }
	          );
	        }

	        let items = [
	            {"id":"9fdoci2KOME6","type":rootFolder.FOLDER,"parent":rootFolder.id,"title":"My Bookmarks 1"},
	            {"id":"1fdoci2KOME5","type":rootFolder.FOLDER,"parent":rootFolder.id,"title":"My Bookmarks 2"},
	            {"id":"G_UL4ZhOyX8m","type":rootFolder.BOOKMARK,"parent":"1fdoci2KOME5","title":"reddit: the front page of the internet","uri":"http://www.reddit.com/"},
	          ];
	        function mergeSomeItems() {
	          return rootFolder.mergeRemoteItems(items);
	        }

	        logLocalItems().then(
	          mergeSomeItems
	        ).then(
	          function(processedItems) {
	            console.log("!!!", processedItems);
	            console.log("merge complete");
	          },
	          function(error) {
	            console.log("merge failed:", error);
	          }
	        ).then(
	          logLocalItems
	        );
	      }
	    );


	  },
	};

	window.addEventListener("load", function(e) { HelloWorld.onLoad(e); }, false);