refactor the autocomplete usage allowing for more than gmail to provide autocomplete

This commit is contained in:
Shane Caraveo 2010-12-27 18:33:39 -08:00
Родитель 90d38a850b
Коммит 34ceb38d71
3 изменённых файлов: 76 добавлений и 53 удалений

Просмотреть файл

@ -40,6 +40,7 @@ function (rdapi, url, TextCounter) {
this.tabName = this.type+'Tab'; this.tabName = this.type+'Tab';
this.icon = 'i/'+this.type+'Icon.png'; this.icon = 'i/'+this.type+'Icon.png';
this.shorten = false; this.shorten = false;
this.autoCompleteWidget = null;
for (var i in options) { for (var i in options) {
this[i] = options[i]; this[i] = options[i];
@ -88,7 +89,30 @@ function (rdapi, url, TextCounter) {
} }
}, },
clearCache: function(store) { clearCache: function(store) {
delete store[this.type+'Contacts'];
},
getContacts: function(store) {
if (store[this.type+'Contacts'])
return JSON.parse(store[this.type+'Contacts']);
return null;
},
setContacts: function(store, contacts) {
store[this.type+'Contacts'] = JSON.stringify(contacts);
},
getFormattedContacts: function(entries) {
var data = [];
entries.forEach(function (entry) {
if (entry.accounts && entry.accounts.length) {
entry.accounts.forEach(function (account) {
account.displayName = entry.displayName;
// XXX form autocomplete does not really work with this, it wants an
// email field
account.email = account.value;
data.push(account);
});
}
});
return data;
} }
}; };
@ -105,6 +129,20 @@ function (rdapi, url, TextCounter) {
} }
return true; return true;
}; };
emailSvcBase.prototype.getFormattedContacts = function(entries) {
var data = [];
entries.forEach(function (entry) {
if (entry.emails && entry.emails.length) {
entry.emails.forEach(function (email) {
data.push({
displayName: entry.displayName,
email: email.value
});
});
}
});
return data;
}
var svcs = { var svcs = {
showNew: showNew, showNew: showNew,

Просмотреть файл

@ -126,11 +126,11 @@
<span class="username">Gmail User</span> <span class="username">Gmail User</span>
</div> </div>
<div class="addressing"> <div class="addressing">
<input name="to" placeholder="to" type="text" value="" class="ffshareAutoComplete"/> <input name="to" placeholder="to" type="text" value="" class="ffshareAutoComplete foobar" autocompletestore="google.com"/>
<input name="subject" placeholder="subject" type="text" value=""/> <input name="subject" placeholder="subject" type="text" value=""/>
</div> </div>
<div class="message boxFlex"> <div class="message boxFlex">
<textarea class="message urlWithSpace" placeholder="Type your message here, and we'll attach the link" name="message"></textarea> <textarea class="message urlWithSpace" placeholder="Type your messasdfasdfasdfasdfage here, and we'll attach the link" name="message"></textarea>
</div> </div>
<button class="share" type="submit">share</button> <button class="share" type="submit">share</button>
</form> </form>
@ -149,7 +149,7 @@
<span class="username">Domain User</span> <span class="username">Domain User</span>
</div> </div>
<div class="addressing"> <div class="addressing">
<input name="to" placeholder="to" type="text" value="" class="ffshareAutoComplete"/> <input name="to" placeholder="to" type="text" value="" class="ffshareAutoComplete" autocompletestore="googleapps.com"/>
<input name="subject" placeholder="subject" type="text" value=""/> <input name="subject" placeholder="subject" type="text" value=""/>
</div> </div>
<div class="message boxFlex"> <div class="message boxFlex">
@ -172,7 +172,7 @@
<span class="username">Yahoo! User</span> <span class="username">Yahoo! User</span>
</div> </div>
<div class="addressing"> <div class="addressing">
<input name="to" placeholder="to" type="text" value="" class="ffshareAutoComplete"/> <input name="to" placeholder="to" type="text" value="" class="ffshareAutoComplete" autocompletestore="yahoo.com"/>
<input name="subject" placeholder="subject" type="text" value=""/> <input name="subject" placeholder="subject" type="text" value=""/>
</div> </div>
<div class="message boxFlex"> <div class="message boxFlex">

Просмотреть файл

@ -42,7 +42,7 @@ function (require, $, fn, rdapi, oauth, jig, url,
options = {}, options = {},
tabDom, bodyDom, clickBlockDom, timer, tabDom, bodyDom, clickBlockDom, timer,
updateTab = true, tabSelection, accountCache, showNew, updateTab = true, tabSelection, accountCache, showNew,
autoCompleteWidget, store = storage(); store = storage();
jig.addFn({ jig.addFn({
profilePic: function (photos) { profilePic: function (photos) {
@ -190,31 +190,40 @@ function (require, $, fn, rdapi, oauth, jig, url,
* Makes sure there is an autocomplete set up with the latest * Makes sure there is an autocomplete set up with the latest
* store data. * store data.
*/ */
function updateAutoComplete() { function updateAutoComplete(serviceName) {
var toNode = $('#gmail').find('[name="to"]')[0], try {
data = store.gmailContacts; var svc = services.domains[serviceName];
dump("update autocomplete for "+svc.type+"\n");
if (data) { var toNode = $('#'+svc.type).find('[name="to"]')[0],
data = JSON.parse(data); contacts = svc.getContacts(store);
} else { if (!contacts) {
data = []; contacts = [];
} }
dump(" updating "+contacts.length+" contacts to "+toNode+"\n");
if (!autoCompleteWidget) { if (!svc.autoCompleteWidget) {
autoCompleteWidget = new AutoComplete(toNode); svc.autoCompleteWidget = new AutoComplete(toNode);
}
var acdata = {
domain: serviceName,
contacts: contacts
}
dispatch.pub('autoCompleteData', acdata);
dump(" dispatched "+acdata.domain+" contacts\n");
} catch(e) {
dump(e+"\n");
} }
dispatch.pub('autoCompleteData', data);
} }
/** /**
* Use store to save gmail contacts, but fetch from API * Use store to save gmail contacts, but fetch from API
* server if there is no store copy. * server if there is no store copy.
*/ */
function storeGmailContacts(account) { function storeContacts(serviceName, account) {
if (!store.gmailContacts) { var svcAccount = account.accounts[0];
var svcAccount = account.accounts[0]; var svc = services.domains[svcAccount.domain];
var contacts = svc.getContacts(store);
if (!contacts || contacts.length < 1) {
rdapi('contacts/' + svcAccount.domain, { rdapi('contacts/' + svcAccount.domain, {
type: 'POST', type: 'POST',
data: { data: {
@ -229,19 +238,10 @@ function (require, $, fn, rdapi, oauth, jig, url,
var entries = json.result.entry, var entries = json.result.entry,
data = []; data = [];
entries.forEach(function (entry) { data = svc.getFormattedContacts(entries);
if (entry.emails && entry.emails.length) {
entry.emails.forEach(function (email) {
data.push({
displayName: entry.displayName,
email: email.value
});
});
}
});
store.gmailContacts = JSON.stringify(data); svc.setContacts(data);
updateAutoComplete(); updateAutoComplete(svcAccount.domain);
} }
} }
}); });
@ -249,8 +249,8 @@ function (require, $, fn, rdapi, oauth, jig, url,
//This function could be called before window is loaded, or after. In //This function could be called before window is loaded, or after. In
//either case, make sure to let the chrome know about it, since chrome //either case, make sure to let the chrome know about it, since chrome
//listens after the page is loaded (not after just DOM ready) //listens after the page is loaded (not after just DOM ready)
updateAutoComplete(); updateAutoComplete(svcAccount.domain);
$(window).bind('load', updateAutoComplete); //$(window).bind('load', updateAutoComplete);
} }
} }
@ -361,12 +361,7 @@ function (require, $, fn, rdapi, oauth, jig, url,
if (!hasLastSelectionMatch) { if (!hasLastSelectionMatch) {
hasLastSelectionMatch = actions[name].type === store.lastSelection; hasLastSelectionMatch = actions[name].type === store.lastSelection;
} }
name = name.split('.'); userAccounts[actions[name].type] = account;
name = name[name.length - 2];
if (name === 'google') {
name = 'gmail';
}
userAccounts[name] = account;
} }
}); });
} }
@ -383,17 +378,7 @@ function (require, $, fn, rdapi, oauth, jig, url,
for (var act in userAccounts) { for (var act in userAccounts) {
updateAccountDisplay(act, userAccounts[act]); updateAccountDisplay(act, userAccounts[act]);
} storeContacts(act, userAccounts[act]);
if (userAccounts.gmail) {
//Make sure we have contacts for auto-complete
storeGmailContacts(userAccounts.gmail);
} else {
//Make sure there is no cached data hanging around.
if (store.gmailContacts) {
delete store.gmailContacts;
updateAutoComplete();
}
} }
//Session restore, do after form setting above. //Session restore, do after form setting above.