gist: 17245 Download_button fork
public
Description:
A Ubiquity Command to interact with a TiddlyWiki
Public Clone URL: git://gist.github.com/17245.git
ubiquity_tiddlywiki.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Ubiquity Command to interact with a TiddlyWiki
*
* contact@garyhodgson.com
*/
 
var TiddlyUbiquity = {};
TiddlyUbiquity.SETUP_WARNING = "Warning: You must supply the URL of your TiddlyWiki. Please use tiddlywiki-setup.";
TiddlyUbiquity.NO_URL_MSG = "No TiddlyWiki URL found. Please use tiddlywiki-setup.";
                        
CmdUtils.CreateCommand({
  name: "tw-config",
  description: "Sets the URL of your TiddlyWiki.",
    author: {
        name: "Gary Hodgson",
        homepage: "http://www.garyhodgson.com/",
        email: "contact@garyhodgson.com"
  },
  license: "MPL",
  icon: "http://www.google.com/s2/favicons?domain=www.tiddlywiki.com",
  help: "Setting the URL allows you use the search, create and open functions. If the TiddlyWiki is open in a tab you can select it from the suggestions list.",
  takes: {"url": noun_arb_text},
  modifiers: {"on": noun_type_tab},
  preview: function( previewBlock, url, mods ) {
    
        var subs = new Object();
        var ptemplate = "Sets the TiddlyWiki URL to ${url}<br><br>Current value:<br><font size=2>${current}</font>";
        subs.url = mods.on.summary || url.summary || '';
        subs.current = Application.prefs.getValue("TIDDLYUBIQUITY_URL", "");
   
        previewBlock.innerHTML =CmdUtils.renderTemplate(ptemplate, subs );
  },
  execute: function( url, mods) {
    if ((!mods.on.text) && (!url.text || url.text.length < 1)) {
        displayMessage("No URL given, or tab selected.");
        return;
    }
 
    if (mods.on.text){
        var tabName = mods.on.text;
                var tabs = noun_type_tab.getTabs();
        Application.prefs.setValue("TIDDLYUBIQUITY_URL", tabs[tabName].uri.spec);
    } else {
        Application.prefs.setValue("TIDDLYUBIQUITY_URL", url.text);
    }
 
    displayMessage("TiddlyWiki URL set to: " + Application.prefs.getValue("TIDDLYUBIQUITY_URL", "Error Setting URL."));
    return;
  }
});
    
CmdUtils.CreateCommand({
  name: "tw-new",
  description: "Creates a new Tiddler.",
  author: {
        name: "Gary Hodgson",
        homepage: "http://www.garyhodgson.com/",
        email: "contact@garyhodgson.com"
  },
  license: "MPL",
  icon: "http://www.google.com/s2/favicons?domain=www.tiddlywiki.com",
  help: "Allows you to create Tiddlers in your TiddlyWiki.",
  takes: {"name": noun_arb_text},
  preview: function( previewBlock, name, mods ) {
    var name = name.text || 'NewTiddler';
    var subs = {name: name};
 
    if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
       msg = TiddlyUbiquity.SETUP_WARNING;
    } else {
        msg = "Creates a new Tiddler called ${name}."
    }
    previewBlock.innerHTML = CmdUtils.renderTemplate( msg, subs );
  },
  execute: function( name, mods) {
    if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
       displayMessage(TiddlyUbiquity.NO_URL_MSG);
       return;
    }
    var name = name.text || 'NewTiddler';
 
    var h=Application.prefs.getValue("TIDDLYUBIQUITY_URL", "");
    var p='/#newTiddler:\"' + encodeURIComponent(name)+'\"';
    TiddlyUbiquity.openTiddlyWiki(h,p);
  }
});
 
CmdUtils.CreateCommand({
  name: "tw-search",
  description: "Performs a TiddlyWiki search.",
    author: {
        name: "Gary Hodgson",
        homepage: "http://www.garyhodgson.com/",
        email: "contact@garyhodgson.com"
  },
  license: "MPL",
  icon: "http://www.google.com/s2/favicons?domain=www.tiddlywiki.com",
  help: "Allows you to search for a Tiddler in your TiddlyWiki.",
  takes: {"name": noun_arb_text},
  preview: function( previewBlock, name, mods ) {
      
      //TODO - add live search functionality
      
    var name = name.text || '';
    var subs = {name: name};
 
    if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
       msg = TiddlyUbiquity.SETUP_WARNING;
    } else {
        msg = "Searches for Tiddlers containing : ${name}."
    }
    previewBlock.innerHTML = CmdUtils.renderTemplate( msg, subs );
  },
  execute: function( name, mods) {
    if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
       displayMessage(TiddlyUbiquity.NO_URL_MSG);
       return;
    }
    
    if (!name.text || name.text.length < 1) {
        displayMessage("No name given.");
        return;
    }
    
    var h=Application.prefs.getValue("TIDDLYUBIQUITY_URL", "");
    var p='/#search:\"' + encodeURIComponent(name.text)+'\"';
    TiddlyUbiquity.openTiddlyWiki(h,p);
  }
});
 
CmdUtils.CreateCommand({
  name: "tw-open",
  description: "Opens a TiddlyWiki.",
    author: {
        name: "Gary Hodgson",
        homepage: "http://www.garyhodgson.com/",
        email: "contact@garyhodgson.com"
  },
  license: "MPL",
  icon: "http://www.google.com/s2/favicons?domain=www.tiddlywiki.com",
  help: "Allows you to open a Tiddler in your TiddlyWiki.",
  takes: {"name": noun_arb_text},
  preview: function( previewBlock, name, mods ) {
      
    var name = name.text || '';
    var subs = {name: name};
 
    if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
       msg = TiddlyUbiquity.SETUP_WARNING;
    } else {
        msg = "Opens a Tiddler matching ${name}."
    }
    previewBlock.innerHTML = CmdUtils.renderTemplate( msg, subs );
  },
  execute: function( name, mods) {
    if (!Application.prefs.has("TIDDLYUBIQUITY_URL")){
       displayMessage(TiddlyUbiquity.NO_URL_MSG);
       return;
    }
    
    if (!name.text || name.text.length < 1) {
        displayMessage("No name given.");
        return;
    }
        
    var h=Application.prefs.getValue("TIDDLYUBIQUITY_URL", "");
    var p='/#open:\"' + encodeURIComponent(name.text)+'\"';
 
    TiddlyUbiquity.openTiddlyWiki(h,p);
    
  }
});
 
                    
var noun_type_tab = {
  _name: "tab name",
 
  // Returns all tabs from all windows.
  getTabs: function(){
    var tabs = {};
 
    for( var j=0; j < Application.windows.length; j++ ) {
      var window = Application.windows[j];
      for (var i = 0; i < window.tabs.length; i++) {
        var tab = window.tabs[i];
        tabs[tab.document.title] = tab;
      }
    }
 
    return tabs;
  },
 
  suggest: function( text, html ) {
    
    var suggestions = [];
    var tabs = noun_type_tab.getTabs();
 
    //TODO: implement a better match algorithm
    for ( var tabName in tabs ) {
      if (tabName.match(text, "i"))
     suggestions.push( CmdUtils.makeSugg(tabName) );
    }
 
    // Return a list of input objects, limited to at most five:
    return suggestions.splice(0, 5);
  }
}
 
TiddlyUbiquity.openTiddlyWiki = function(host, path){
    
    var tabs = noun_type_tab.getTabs();
    
    for (var tabName in tabs){
        if (tabs[tabName].uri.spec.match(host)){
 
            var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
 
            if (ios){
                tabs[tabName].load(ios.newURI(host+path, null, null));
                tabs[tabName]._window.focus();
                tabs[tabName].focus();
                if (tabs[tabName].uri.spec.length > host.length) {
                    // modified URL, so force page reload
                    tabs[tabName]._window.BrowserReload();
                }
                return;
            }
        }
    }
 
    Utils.openUrlInBrowser(host+path);
}
 
 
 

Owner

garyhodgson

Revisions