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.icon = 'i/'+this.type+'Icon.png';
this.shorten = false;
this.autoCompleteWidget = null;
for (var i in options) {
this[i] = options[i];
@ -88,7 +89,30 @@ function (rdapi, url, TextCounter) {
}
},
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;
};
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 = {
showNew: showNew,

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

@ -126,11 +126,11 @@
<span class="username">Gmail User</span>
</div>
<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=""/>
</div>
<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>
<button class="share" type="submit">share</button>
</form>
@ -149,7 +149,7 @@
<span class="username">Domain User</span>
</div>
<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=""/>
</div>
<div class="message boxFlex">
@ -172,7 +172,7 @@
<span class="username">Yahoo! User</span>
</div>
<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=""/>
</div>
<div class="message boxFlex">

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

@ -42,7 +42,7 @@ function (require, $, fn, rdapi, oauth, jig, url,
options = {},
tabDom, bodyDom, clickBlockDom, timer,
updateTab = true, tabSelection, accountCache, showNew,
autoCompleteWidget, store = storage();
store = storage();
jig.addFn({
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
* store data.
*/
function updateAutoComplete() {
var toNode = $('#gmail').find('[name="to"]')[0],
data = store.gmailContacts;
if (data) {
data = JSON.parse(data);
} else {
data = [];
function updateAutoComplete(serviceName) {
try {
var svc = services.domains[serviceName];
dump("update autocomplete for "+svc.type+"\n");
var toNode = $('#'+svc.type).find('[name="to"]')[0],
contacts = svc.getContacts(store);
if (!contacts) {
contacts = [];
}
dump(" updating "+contacts.length+" contacts to "+toNode+"\n");
if (!autoCompleteWidget) {
autoCompleteWidget = new AutoComplete(toNode);
if (!svc.autoCompleteWidget) {
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
* server if there is no store copy.
*/
function storeGmailContacts(account) {
if (!store.gmailContacts) {
function storeContacts(serviceName, account) {
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, {
type: 'POST',
data: {
@ -229,19 +238,10 @@ function (require, $, fn, rdapi, oauth, jig, url,
var entries = json.result.entry,
data = [];
entries.forEach(function (entry) {
if (entry.emails && entry.emails.length) {
entry.emails.forEach(function (email) {
data.push({
displayName: entry.displayName,
email: email.value
});
});
}
});
data = svc.getFormattedContacts(entries);
store.gmailContacts = JSON.stringify(data);
updateAutoComplete();
svc.setContacts(data);
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
//either case, make sure to let the chrome know about it, since chrome
//listens after the page is loaded (not after just DOM ready)
updateAutoComplete();
$(window).bind('load', updateAutoComplete);
updateAutoComplete(svcAccount.domain);
//$(window).bind('load', updateAutoComplete);
}
}
@ -361,12 +361,7 @@ function (require, $, fn, rdapi, oauth, jig, url,
if (!hasLastSelectionMatch) {
hasLastSelectionMatch = actions[name].type === store.lastSelection;
}
name = name.split('.');
name = name[name.length - 2];
if (name === 'google') {
name = 'gmail';
}
userAccounts[name] = account;
userAccounts[actions[name].type] = account;
}
});
}
@ -383,17 +378,7 @@ function (require, $, fn, rdapi, oauth, jig, url,
for (var act in userAccounts) {
updateAccountDisplay(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();
}
storeContacts(act, userAccounts[act]);
}
//Session restore, do after form setting above.