-
-
+
| Name: | -- - | -
| Notes: | -- - | -
| Workspace: | -- - | -
| Assignee: | -- - | -
| - |
-
- Add to Asana
-
- |
-
+
-
+
-
+
+
+
+
+
+
- Please log into Asana to start adding
- tasks.
+
+
+
+
+
diff --git a/popup.js b/popup.js
index 11b2b4b..cf7b5cd 100644
--- a/popup.js
+++ b/popup.js
@@ -1,32 +1,76 @@
-window.addEventListener('load', function() {
-
- // Our default error handler.
- Asana.ServerModel.onError = function(response) {
- showError(response.errors[0].message);
- };
-
- // Ah, the joys of asynchronous programming.
- // To initialize, we've got to gather various bits of information.
- // Starting with a reference to the window and tab that were active when
- // the popup was opened ...
- chrome.windows.getCurrent(function(w) {
+/**
+ * Code for the popup UI.
+ */
+Popup = {
+
+ // Is this an external popup window? (vs. the one from the menu)
+ is_external: false,
+
+ // Options loaded when popup opened.
+ options: null,
+
+ // Info from page we were triggered from
+ page_title: null,
+ page_url: null,
+ page_selection: null,
+ favicon_url: null,
+
+ // State to track so we only log events once.
+ has_edited_name: false,
+ has_edited_notes: false,
+ has_reassigned: false,
+ has_used_page_details: false,
+ is_first_add: true,
+
+ // Data from API cached for this popup.
+ workspaces: null,
+// users: null,
+// user_id: null,
+
+ // Typeahead ui element
+ typeahead: null,
+ SILHOUETTE_URL: "./nopicture.png",
+
+ onLoad: function() {
+ var me = this;
+
+ me.is_external = ('' + window.location.search).indexOf("external=true") !== -1;
+
+ // Our default error handler.
+ Asana.ServerModel.onError = function(response) {
+ me.showError(response.errors[0].message);
+ };
+
+ // Ah, the joys of asynchronous programming.
+ // To initialize, we've got to gather various bits of information.
+ // Starting with a reference to the window and tab that were active when
+ // the popup was opened ...
chrome.tabs.query({
active: true,
- windowId: w.id
+ currentWindow: true
}, function(tabs) {
+ var tab = tabs[0];
// Now load our options ...
Asana.ServerModel.options(function(options) {
+ me.options = options;
// And ensure the user is logged in ...
Asana.ServerModel.isLoggedIn(function(is_logged_in) {
if (is_logged_in) {
if (window.quick_add_request) {
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-Open-QuickAdd"
+ });
// If this was a QuickAdd request (set by the code popping up
// the window in Asana.ExtensionServer), then we have all the
// info we need and should show the add UI right away.
- showAddUi(
+ me.showAddUi(
quick_add_request.url, quick_add_request.title,
- quick_add_request.selected_text, options);
+ quick_add_request.selected_text,
+ quick_add_request.favicon_url);
} else {
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-Open-Button"
+ });
// Otherwise we want to get the selection from the tab that
// was active when we were opened. So we set up a listener
// to listen for the selection send event from the content
@@ -34,190 +78,521 @@ window.addEventListener('load', function() {
var selection = "";
var listener = function(request, sender, sendResponse) {
if (request.type === "selection") {
- chrome.extension.onRequest.removeListener(listener);
+ chrome.runtime.onMessage.removeListener(listener);
console.info("Asana popup got selection");
selection = "\n" + request.value;
}
};
- chrome.extension.onRequest.addListener(listener);
-
- // ... and then we make a request to the content window to
- // send us the selection.
- var tab = tabs[0];
- chrome.tabs.executeScript(tab.id, {
- code: "(Asana && Asana.SelectionClient) ? Asana.SelectionClient.sendSelection() : 0"
- }, function() {
- // The requests appear to be handled synchronously, so the
- // selection should have been sent by the time we get this
- // completion callback. If the timing ever changes, however,
- // that could break and we would never show the add UI.
- // So this could be made more robust.
- showAddUi(tab.url, tab.title, selection, options);
- });
+ chrome.runtime.onMessage.addListener(listener);
+ me.showAddUi(tab.url, tab.title, '', tab.favIconUrl);
}
} else {
// The user is not even logged in. Prompt them to do so!
- showLogin(Asana.Options.loginUrl(options));
+ me.showLogin(
+ Asana.Options.loginUrl(options),
+ Asana.Options.signupUrl(options));
}
});
});
});
- });
-});
-// Helper to show a named view.
-var showView = function(name) {
- ["login", "add", "success"].forEach(function(view_name) {
- $("#" + view_name + "_view").css("display", view_name === name ? "" : "none");
- });
-};
+ // Wire up some events to DOM elements on the page.
-// Show the add UI
-var showAddUi = function(url, title, selected_text, options) {
- var self = this;
- showView("add");
- $("#notes").val(url + selected_text);
- $("#name").val(title);
- $("#name").focus();
- $("#name").select();
- Asana.ServerModel.me(function(user) {
- // Just to cache result.
- Asana.ServerModel.workspaces(function(workspaces) {
- $("#workspace").html("");
- workspaces.forEach(function(workspace) {
- $("#workspace").append(
- "");
- });
- $("#workspace").val(options.default_workspace_id);
- onWorkspaceChanged();
- $("#workspace").change(onWorkspaceChanged);
+ $(window).keydown(function(e) {
+ // Close the popup if the ESCAPE key is pressed.
+ if (e.which === 27) {
+ if (me.is_first_add) {
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-Abort"
+ });
+ }
+ window.close();
+ } else if (e.which === 9) {
+ // Don't let ourselves TAB to focus the document body, so if we're
+ // at the beginning or end of the tab ring, explicitly focus the
+ // other end (setting body.tabindex = -1 does not prevent this)
+ if (e.shiftKey && document.activeElement === me.firstInput().get(0)) {
+ me.lastInput().focus();
+ e.preventDefault();
+ } else if (!e.shiftKey && document.activeElement === me.lastInput().get(0)) {
+ me.firstInput().focus();
+ e.preventDefault();
+ }
+ }
});
- });
-};
-// Enable/disable the add button.
-var setAddEnabled = function(enabled) {
- var button = $("#add_button");
- if (enabled) {
- button.removeClass("disabled");
- button.addClass("enabled");
- button.click(function() {
- createTask();
- return false;
- });
- button.keydown(function(e) {
- if (e.keyCode === 13) {
- createTask();
+ // Close if the X is clicked.
+ $(".close-x").click(function() {
+ if (me.is_first_add) {
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-Abort"
+ });
}
+ window.close();
});
- } else {
- button.removeClass("enabled");
- button.addClass("disabled");
- button.unbind('click');
- button.unbind('keydown');
- }
-};
-// Set the add button as being "working", waiting for the Asana request
-// to complete.
-var setAddWorking = function(working) {
- setAddEnabled(!working);
- $("#add_button").find(".button-text").text(
- working ? "Adding..." : "Add to Asana");
-};
+ $("#name_input").keyup(function() {
+ if (!me.has_edited_name && $("#name_input").val() !== "") {
+ me.has_edited_name = true;
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-ChangedTaskName"
+ });
+ }
+ me.maybeDisablePageDetailsButton();
+ });
+ $("#notes_input").keyup(function() {
+ if (!me.has_edited_notes && $("#notes_input").val() !== "") {
+ me.has_edited_notes= true;
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-ChangedTaskNotes"
+ });
+ }
+ me.maybeDisablePageDetailsButton();
+ });
-// When the user changes the workspace, update the list of users.
-var onWorkspaceChanged = function() {
- var workspace_id = readWorkspaceId();
- $("#assignee").html("");
- setAddEnabled(false);
- Asana.ServerModel.users(workspace_id, function(users) {
- $("#assignee").html("");
- users = users.sort(function(a, b) {
- return (a.name < b.name) ? -1 : ((a.name > b.name) ? 1 : 0);
+ // The page details button fills in fields with details from the page
+ // in the current tab (cached when the popup opened).
+ var use_page_details_button = $("#use_page_details");
+ use_page_details_button.click(function() {
+ if (!(use_page_details_button.hasClass('disabled'))) {
+ // Page title -> task name
+ $("#name_input").val(me.page_title);
+ // Page url + selection -> task notes
+ var notes = $("#notes_input");
+ notes.val(notes.val() + me.page_url + "\n" + me.page_selection);
+ // Disable the page details button once used.
+ use_page_details_button.addClass('disabled');
+ if (!me.has_used_page_details) {
+ me.has_used_page_details = true;
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-UsedPageDetails"
+ });
+ }
+ }
});
- users.forEach(function(user) {
- $("#assignee").append(
- "");
+
+ // Make a typeahead for assignee
+// me.typeahead = new UserTypeahead("assignee");
+
+ },
+
+ maybeDisablePageDetailsButton: function() {
+ if ($("#name_input").val() !== "" || $("#notes_input").val() !== "") {
+ $("#use_page_details").addClass('disabled');
+ } else {
+ $("#use_page_details").removeClass('disabled');
+ }
+ },
+
+ setExpandedUi: function(is_expanded) {
+ if (this.is_external) {
+ window.resizeTo(
+ Asana.POPUP_UI_WIDTH,
+ (is_expanded ? Asana.POPUP_EXPANDED_UI_HEIGHT : Asana.POPUP_UI_HEIGHT)
+ + Asana.CHROME_TITLEBAR_HEIGHT);
+ }
+ },
+
+ showView: function(name) {
+ ["login", "add"].forEach(function(view_name) {
+ $("#" + view_name + "_view").css("display", view_name === name ? "" : "none");
});
+ },
+
+ showAddUi: function(url, title, selected_text, favicon_url) {
+ var me = this;
+
+ // Store off info from page we got triggered from.
+ me.page_url = url;
+ me.page_title = title;
+ me.page_selection = selected_text;
+ me.favicon_url = favicon_url;
+
+ // Populate workspace selector and select default.
Asana.ServerModel.me(function(user) {
- $("#assignee").val(user.id);
+ me.user_id = user.id;
+ Asana.ServerModel.workspaces(function(workspaces) {
+ me.workspaces = workspaces;
+ var select = $("#workspace_select");
+ select.html("");
+ workspaces.forEach(function(workspace) {
+ $("#workspace_select").append(
+ "");
+ });
+ if (workspaces.length > 1) {
+ $("workspace_select_container").show();
+ } else {
+ $("workspace_select_container").hide();
+ }
+ select.val(me.options.default_workspace_id);
+ me.onWorkspaceChanged();
+ select.change(function() {
+ if (select.val() !== me.options.default_workspace_id) {
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-ChangedWorkspace"
+ });
+ }
+ me.onWorkspaceChanged();
+ });
+
+ // Set initial UI state
+ me.resetFields();
+ me.showView("add");
+ var name_input = $("#name_input");
+ name_input.focus();
+ name_input.select();
+
+ if (favicon_url) {
+ $(".icon-use-link").css("background-image", "url(" + favicon_url + ")");
+ } else {
+ $(".icon-use-link").addClass("no-favicon sprite");
+ }
+ });
});
- setAddEnabled(true);
- });
-};
-var readAssignee = function() {
- return $("#assignee").val();
-};
+ },
-var readWorkspaceId = function() {
- return $("#workspace").val();
-};
+ /**
+ * @param enabled {Boolean} True iff the add button should be clickable.
+ */
+ setAddEnabled: function(enabled) {
+ var me = this;
+ var button = $("#add_button");
+ if (enabled) {
+ // Update appearance and add handlers.
+ button.removeClass("disabled");
+ button.addClass("enabled");
+ button.click(function() {
+ me.createTask();
+ return false;
+ });
+ button.keydown(function(e) {
+ if (e.keyCode === 13) {
+ me.createTask();
+ }
+ });
+ } else {
+ // Update appearance and remove handlers.
+ button.removeClass("enabled");
+ button.addClass("disabled");
+ button.unbind('click');
+ button.unbind('keydown');
+ }
+ },
-var createTask = function() {
- console.info("Creating task");
- hideError();
- setAddWorking(true);
- Asana.ServerModel.createTask(
- readWorkspaceId(),
- {
- name: $("#name").val(),
- notes: $("#notes").val(),
- assignee: readAssignee()
+ showError: function(message) {
+ console.log("Error: " + message);
+ $("#error").css("display", "inline-block");
+ },
+
+ hideError: function() {
+ $("#error").css("display", "none");
+ },
+
+ /**
+ * Clear inputs for new task entry.
+ */
+ resetFields: function() {
+ $("#name_input").val("");
+ $("#notes_input").val("");
+ $("#assignee_input").val("");
+ $("#project_input").val("");
+ },
+
+ /**
+ * Set the add button as being "working", waiting for the Asana request
+ * to complete.
+ */
+ setAddWorking: function(working) {
+ this.setAddEnabled(!working);
+ $("#add_button").find(".button-text").text(
+ working ? "Adding..." : "Add to Asana");
+ },
+
+ /**
+ * Creates a Bloodhound suggestion engine which consumes typeahead data from a
+ * given endpoint.
+ *
+ * @param typeahead_endpoint {String} The API endpoint to get typeahead results from.
+ * @param filter_function {Function} Function to filter typeahead results into objects, for UI usage.
+ * @returns {Bloodhound} Initialized engine with typeahead endpoint.
+ * @private
+ */
+ _createBloodhoundObject: function(typeahead_endpoint, filter_function) {
+ var me = this;
+ // Instantiate the Bloodhound suggestion engine
+ // Use the typeahead endpoint and filter function
+ var selectana = new Bloodhound({
+ datumTokenizer: function (datum) {
+ return Bloodhound.tokenizers.whitespace(datum.value);
},
- function(task) {
- setAddWorking(false);
- showSuccess(task);
+ queryTokenizer: Bloodhound.tokenizers.whitespace,
+ remote: {
+ url: Asana.ApiBridge.baseApiUrl() + '/workspaces/' + me.selectedWorkspaceId()
+ + typeahead_endpoint,
+ ajax : {
+ beforeSend: function(jqXhr, settings){
+ // WARNING: This will be deprecated, please see api_bridge.js
+ jqXhr.setRequestHeader('X-Allow-Asana-Client', '1');
+ }
+ },
+ filter: filter_function
},
- function(response) {
- setAddWorking(false);
- showError(response.errors[0].message);
+ limit: 8
+ });
+
+ // Clear suggestions and cache when switching workspaces.
+ selectana.clear();
+ selectana.clearRemoteCache();
+ selectana.clearPrefetchCache();
+ // Initialize the Bloodhound suggestion engine. This is truthy, which
+ // will recreate the engine as if it were the first call.
+ selectana.initialize(true);
+
+ return selectana;
+ },
+
+ /**
+ * Instantiates a Typeahead.JS UI object using a Bloodhound object as the
+ * data source.
+ * @param bloodhound_object {Bloodhound} Bloodhound object with typeahead endpoint.
+ * @param input_element {jQuery Object} Element to associate typeahead object.
+ * @param on_selected_function {Function} Function to update UI on selecting result.
+ * @private
+ */
+ _createTypeaheadUi: function(bloodhound_object, input_element, on_selected_function) {
+ // Remove the existing typeahead, we need a new one.
+ input_element.typeahead('destroy');
+
+ // Instantiate the Typeahead UI
+ input_element.typeahead({
+ hint: true,
+ highlight: true
+ }, {
+ displayKey:
+ function(item) {
+ return item.value;
+ },
+ source: bloodhound_object.ttAdapter()
+ }).on('typeahead:selected', on_selected_function);
+ },
+
+ /**
+ * Creates a typeahead.js object with a bloodhound engine that is attached
+ * to the assignee input.
+ */
+ createUserTypeahead: function() {
+ var me = this;
+ var selectana = me._createBloodhoundObject(
+ '/typeahead?type=user&query=%QUERY&opt_fields=name,photo.image_21x21',
+ function (results) {
+ return $.map(results.data, function (result) {
+ return {
+ value: result.name,
+ id: result.id,
+ photo_url: result.photo ? result.photo.image_21x21 : me.SILHOUETTE_URL
+ };
+ });
+ }
+ );
+
+ me._createTypeaheadUi(selectana, $("#assignee_input"),
+ function (eventObject, suggestionObject, suggestionDataset) {
+ $('#assignee_list').html(suggestionObject.id);
+ }
+ );
+ },
+
+ /**
+ * Creates a typeahead.js object with bloodhound engine that is attached to
+ * the project input.
+ */
+ createProjectTypeahead: function() {
+ var me = this;
+ var selectana = me._createBloodhoundObject(
+ '/typeahead?type=project&query=%QUERY',
+ function (results) {
+ return $.map(results.data, function (result) {
+ return {
+ value: result.name,
+ id: result.id
+ };
+ });
+ }
+ );
+
+ me._createTypeaheadUi(selectana, $("#project_input"),
+ function (eventObject, suggestionObject, suggestionDataset) {
+ $('#project_list').html(suggestionObject.id);
+ }
+ );
+ },
+
+ /**
+ * Update the list of users as a result of setting/changing the workspace.
+ */
+ onWorkspaceChanged: function() {
+ var me = this;
+ var workspace_id = me.selectedWorkspaceId();
+ // Update selected workspace
+ $("#workspace").html($("#workspace_select option:selected").text());
+
+ // Save selection as new default.
+ me.options.default_workspace_id = workspace_id;
+ Asana.ServerModel.saveOptions(me.options, function() {});
+
+ // Disable while we create new typeahead.
+ me.setAddEnabled(false);
+ // Update queries and clear out old data.
+ me.createUserTypeahead();
+ me.createProjectTypeahead();
+ // ... and we're ready!
+ me.setAddEnabled(true);
+
+ },
+
+ /**
+ * @return {Integer} ID of the selected workspace.
+ */
+ selectedWorkspaceId: function() {
+ return parseInt($("#workspace_select").val(), 10);
+ },
+
+ assignTaskToUser: function() {
+ var me = this;
+ var assignee_input = $("#assignee_input");
+ // If an assignee was selected, use that person.
+ if (assignee_input.val() !== "") {
+ return $("#assignee_list").text();
+ }
+ var project_input = $("#project_input");
+ // If there's no assignee AND no project, assign to self.
+ if (assignee_input.val() === "" && project_input.val() === "") {
+ return "me";
+ }
+
+ // Otherwise, let user place task in a project.
+ return "";
+ },
+
+ /**
+ * Create a task in asana using the data in the form.
+ */
+ createTask: function() {
+ var me = this;
+
+ // Update UI to reflect attempt to create task.
+ console.info("Creating task");
+ me.hideError();
+ me.setAddWorking(true);
+
+ if (!me.is_first_add) {
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-CreateTask-MultipleTasks"
});
-};
+ }
-var showError = function(message) {
- console.log("Error: " + message);
- $("#error").css("display", "");
-};
+ Asana.ServerModel.createTask(
+ me.selectedWorkspaceId(),
+ {
+ name: $("#name_input").val(),
+ notes: $("#notes_input").val(),
+ // Default assignee to self
+ assignee: me.assignTaskToUser(),
+ projects: (($("#project_input").val() !== "") ?
+ [$("#project_list").text()] : [])
+ },
+ function(task) {
+ // Success! Show task success, then get ready for another input.
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-CreateTask-Success"
+ });
+ me.setAddWorking(false);
+ me.showSuccess(task);
+ me.resetFields();
+ $("#name_input").focus();
+ },
+ function(response) {
+ // Failure. :( Show error, but leave form available for retry.
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-CreateTask-Failure"
+ });
+ me.setAddWorking(false);
+ me.showError(response.errors[0].message);
+ });
+ },
-var hideError = function() {
- $("#error").css("display", "none");
-};
+ /**
+ * Helper to show a success message after a task is added.
+ */
+ showSuccess: function(task) {
+ var me = this;
+ Asana.ServerModel.taskViewUrl(task, function(url) {
+ var name = task.name.replace(/^\s*/, "").replace(/\s*$/, "");
+ var link = $("#new_task_link");
+ link.attr("href", url);
+ link.text(name !== "" ? name : "Task");
+ link.unbind("click");
+ link.click(function() {
+ chrome.tabs.create({url: url});
+ window.close();
+ return false;
+ });
+
+ // Reset logging for multi-add
+ me.has_edited_name = true;
+ me.has_edited_notes = true;
+ me.has_reassigned = true;
+ me.is_first_add = false;
+
+ $("#success").css("display", "inline-block");
+ });
+ },
-// Helper to show a success message after a task is added.
-var showSuccess = function(task) {
- Asana.ServerModel.taskViewUrl(task, function(url) {
- var name = task.name.replace(/^\s*/, "").replace(/\s*$/, "");
- $("#new_task_link").attr("href", url);
- $("#new_task_link").text(name !== "" ? name : "unnamed task");
- $("#new_task_link").unbind("click");
- $("#new_task_link").click(function() {
- chrome.tabs.create({url: url});
+ /**
+ * Show the login page.
+ */
+ showLogin: function(login_url, signup_url) {
+ var me = this;
+ $("#login_button").click(function() {
+ chrome.tabs.create({url: login_url});
window.close();
return false;
});
- showView("success");
- });
-};
+ $("#signup_button").click(function() {
+ chrome.tabs.create({url: signup_url});
+ window.close();
+ return false;
+ });
+ me.showView("login");
+ },
-// Helper to show the login page.
-var showLogin = function(url) {
- $("#login_link").attr("href", url);
- $("#login_link").unbind("click");
- $("#login_link").click(function() {
- chrome.tabs.create({url: url});
- window.close();
- return false;
- });
- showView("login");
-};
+ firstInput: function() {
+ return $("#workspace_select");
+ },
-// Close the popup if the ESCAPE key is pressed.
-window.addEventListener("keydown", function(e) {
- if (e.keyCode === 27) {
- window.close();
+ lastInput: function() {
+ return $("#add_button");
}
-}, /*capture=*/false);
+};
-$("#close-banner").click(function() { window.close(); });
+$(window).load(function() {
+ Popup.onLoad();
+});
+
+// This is a workaround to add a loading indicator for typeahead.js.
+// See https://github.com/twitter/typeahead.js/issues/284
+$(document).ajaxSend(function(event, jqXHR, settings) {
+ // display spinner
+ $("#spinner").removeClass("indicator-hide");
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-Typeahead-SearchStarted"
+ });
+});
+
+$(document).ajaxComplete(function(event, jqXHR, settings) {
+ $("#spinner").addClass("indicator-hide");
+ Asana.ServerModel.logEvent({
+ name: "ChromeExtension-Typeahead-SearchComplete"
+ });
+});
\ No newline at end of file
diff --git a/popup_style.css b/popup_style.css
deleted file mode 100644
index 15e99c7..0000000
--- a/popup_style.css
+++ /dev/null
@@ -1,128 +0,0 @@
-/* Styles for popup.html */
-body {
- overflow: hidden;
- width: 410px;
- height: 310px; /* keep this correct for the window-based (non-button) version */
- padding: 0px;
- margin: 0px;
- margin-top: 1px;
- background-color: #fff;
- border: 1px solid #AFBCC8;
- -webkit-border-radius: 6px;
- font-size: 14px;
- font-family: "Helvetica Neue", Arial, sans-serif;
-}
-
-
-#banner {
- font-size: 17px;
- font-weight: 800;
- background-color:#f2f2f2;
- padding: 10px 14px 8px;
- color: #596573;
- text-transform: uppercase;
- text-shadow: 0px 1px #fff;
- border-bottom: 1px solid #E5E5E5;
- -webkit-box-shadow: inset 0px -1px rgba(0,0,0,0.1);
- -webkit-border-radius: 5px 5px 0px 0px;
-
-}
-
-#content {
- padding: 10px 14px;
- width: 100%;
-}
-
-#error {
- width: 100%;
- padding: 10px 14px;
- background-color: #434C56;
- font-weight: 500;
- color: #ECF0F3;
-}
-.error-icon {
- background-image: url(sprite.png);
- background-position: 0px 0px;
- background-repeat: no-repeat;
- width: 19px;
- min-width: 19px;
- height: 16px;
- min-height: 16px;
- margin-bottom: -2px;
- display: inline-block;
-
-}
-
-
-#add_view {
- padding-bottom: 12px;
-}
-
-.form {
- border: 0;
- margin: 0;
- padding: 0;
-}
-.field-name {
- width: 80px;
- padding: 6px 2px 2px 0px;
- vertical-align: top;
- font-size: 12px;
- font-weight: bold;
- color: #999999;
-
-}
-.field-value {
- width: 294px;
- padding: 2px 0 2px 2px;
- vertical-align: top;
-}
-#name {
- width: 100%;
-}
-#notes {
- width: 100%;
- height: 100px;
-}
-#add_button {
- margin-top: 10px;
-}
-
-#login_view {
- padding-top: 10px;
- width: 100%;
- text-align: center;
- font-weight: 600;
- width: 382px;
-}
-#success_view {
- padding-top: 84px;
- text-align: center;
- font-weight: 600;
- width: 382px;
-
-}
-.success-icon {
- background-image: url(sprite.png);
- background-position: 0px -50px;
- background-repeat: no-repeat;
- width: 19px;
- min-width: 19px;
- height: 16px;
- min-height: 16px;
- margin-bottom: -2px;
- display: inline-block;
-
-}
-input, textarea {
- -webkit-border-radius: 3px;
- border: 1px solid rgba(0,0,0,0.2);
- color: #212F40;
- padding: 4px 5px 3px;
- -webkit-box-shadow: inset 0px 1px 1px rgba(0,0,0,0.1), 0px 1px #fff;
-}
-select, input, textarea {
- font-size: 14px;
- font-family: "Helvetica Neue", Arial, sans-serif;
- color: #212F40;
-}
\ No newline at end of file
diff --git a/quick_add_client.js b/quick_add_client.js
deleted file mode 100644
index 538dcb3..0000000
--- a/quick_add_client.js
+++ /dev/null
@@ -1,65 +0,0 @@
-/**
- * A module to run in a content window to enable QuickAdd from that window.
- * That is, pressing Tab+Q anywhere in the window will open the same popup
- * as is available from the chrome menu.
- *
- * This is not a perfect solution (though it might be refined further with
- * a little more effort). For one thing, we don't swallow the TAB key because
- * we don't want to interfere with the underlying page (especially if the user
- * wasn't going to press TAB+Q). So using the hotkey may cause you to focus
- * a new input on the page before opening the popup.
- *
- * Also, there's no way to trigger the chrome extension popup itself, so this
- * opens a new window with the popup content in it. This looks slightly
- * different than the real popup (and appears in a different place), but it
- * does the job.
- */
-Asana.QuickAddClient = {
-
- _tab_down_time: 0,
-
- keyDown: function(e) {
- var self = Asana.QuickAddClient;
- if (e.keyCode === 9) {
- // Mark tab key as pressed at the current time.
- // If the Q key gets pressed soon enough afterward, we've hit our
- // hotkey combo.
- self._tab_down_time = new Date().getTime();
- } else if (e.keyCode === 81) {
- if (new Date().getTime() < self._tab_down_time + 5000) { // 5s timeout
- self._tab_down_time = 0;
- // Tab-Q!
- // We cannot open the popup programmatically.
- // http://code.google.com/chrome/extensions/faq.html#faq-open-popups
- // So we do this roundabout thing.
- chrome.extension.sendRequest({
- type: "quick_add",
- url: window.location.href,
- title: document.title,
- selected_text: "" + window.getSelection()
- });
- e.preventDefault();
- return false;
- }
- }
- },
-
- keyUp: function(e) {
- var self = Asana.QuickAddClient;
- if (e.keyCode === 9) {
- // Mark tab key as released.
- self._tab_down_time = 0;
- }
- },
-
- listen: function() {
- // Don't run against Asana, which already has a QuickAdd feature. ;)
- if (!/^https?:\/\/app[.]asana[.]com/.test(window.location.href) &&
- !/^https?:\/\/localhost/.test(window.location.href)) {
- window.addEventListener("keydown", Asana.QuickAddClient.keyDown, true);
- window.addEventListener("keyup", Asana.QuickAddClient.keyUp, true);
- }
- }
-};
-
-Asana.QuickAddClient.listen();
diff --git a/selection_client.js b/selection_client.js
deleted file mode 100644
index 049b979..0000000
--- a/selection_client.js
+++ /dev/null
@@ -1,17 +0,0 @@
-/**
- * Module to install on a page that allows sending of the selection over to
- * the popup window when the user pops up the new task window. The popup
- * calls this method on our content window, and we get the selection
- * and send it back.
- */
-Asana.SelectionClient = {
- // We get called when added to asana.
- sendSelection: function() {
- var selected_text = "" + window.getSelection();
- console.info("Sending selection to Asana popup");
- chrome.extension.sendRequest({
- type: "selection",
- value: selected_text
- });
- }
-};
diff --git a/server_model.js b/server_model.js
index 62ca5a4..a2bcd0c 100644
--- a/server_model.js
+++ b/server_model.js
@@ -7,7 +7,10 @@
*/
Asana.ServerModel = {
- _cached_user: null,
+ // Make requests to API to refresh cache at this interval.
+ CACHE_REFRESH_INTERVAL_MS: 15 * 60 * 1000,
+
+ _url_to_cached_image: {},
/**
* Called by the model whenever a request is made and error occurs.
@@ -23,12 +26,23 @@ Asana.ServerModel = {
* Requests the user's preferences for the extension.
*
* @param callback {Function(options)} Callback on completion.
- * workspaces {dict[]} See Asana.Options for details.
+ * options {dict} See Asana.Options for details.
*/
options: function(callback) {
callback(Asana.Options.loadOptions());
},
+ /**
+ * Saves the user's preferences for the extension.
+ *
+ * @param options {dict} See Asana.Options for details.
+ * @param callback {Function()} Callback on completion.
+ */
+ saveOptions: function(options, callback) {
+ Asana.Options.saveOptions(options);
+ callback();
+ },
+
/**
* Determine if the user is logged in.
*
@@ -65,12 +79,12 @@ Asana.ServerModel = {
* @param callback {Function(workspaces)} Callback on success.
* workspaces {dict[]}
*/
- workspaces: function(callback, errback) {
+ workspaces: function(callback, errback, options) {
var self = this;
Asana.ApiBridge.request("GET", "/workspaces", {},
function(response) {
self._makeCallback(response, callback, errback);
- });
+ }, options);
},
/**
@@ -79,12 +93,14 @@ Asana.ServerModel = {
* @param callback {Function(users)} Callback on success.
* users {dict[]}
*/
- users: function(workspace_id, callback) {
+ users: function(workspace_id, callback, errback, options) {
var self = this;
- Asana.ApiBridge.request("GET", "/workspaces/" + workspace_id + "/users", {},
+ Asana.ApiBridge.request(
+ "GET", "/workspaces/" + workspace_id + "/users",
+ { opt_fields: "name,photo.image_60x60" },
function(response) {
- self._makeCallback(response, callback);
- });
+ self._makeCallback(response, callback, errback);
+ }, options);
},
/**
@@ -93,19 +109,12 @@ Asana.ServerModel = {
* @param callback {Function(user)} Callback on success.
* user {dict[]}
*/
- me: function(callback) {
+ me: function(callback, errback, options) {
var self = this;
- if (self._cached_user !== null) {
- callback(self._cached_user);
- } else {
- Asana.ApiBridge.request("GET", "/users/me", {},
- function(response) {
- if (!response.errors) {
- self._cached_user = response.data;
- }
- self._makeCallback(response, callback);
- });
- }
+ Asana.ApiBridge.request("GET", "/users/me", {},
+ function(response) {
+ self._makeCallback(response, callback, errback);
+ }, options);
},
/**
@@ -116,6 +125,13 @@ Asana.ServerModel = {
*/
createTask: function(workspace_id, task, callback, errback) {
var self = this;
+
+ // For now, if task isn't provided an assignee, remove that field so we
+ // can proceed with our API call.
+ if (task.assignee === "") {
+ delete task.assignee;
+ }
+
Asana.ApiBridge.request(
"POST",
"/workspaces/" + workspace_id + "/tasks",
@@ -125,12 +141,72 @@ Asana.ServerModel = {
});
},
+ logEvent: function(event) {
+ Asana.ApiBridge.request(
+ "POST",
+ "/logs",
+ event,
+ function(response) {});
+ },
+
_makeCallback: function(response, callback, errback) {
if (response.errors) {
(errback || this.onError).call(null, response);
} else {
callback(response.data);
}
+ },
+
+ _cacheUserPhoto: function(user) {
+ var me = this;
+ if (user.photo) {
+ var url = user.photo.image_60x60;
+ if (!(url in me._url_to_cached_image)) {
+ var image = new Image();
+ image.src = url;
+ me._url_to_cached_image[url] = image;
+ }
+ }
+ },
+
+ /**
+ * Start fetching all the data needed by the extension so it is available
+ * whenever a popup is opened.
+ */
+ startPrimingCache: function() {
+ var me = this;
+ me._cache_refresh_interval = setInterval(function() {
+ me.refreshCache();
+ }, me.CACHE_REFRESH_INTERVAL_MS);
+ me.refreshCache();
+ },
+
+ refreshCache: function() {
+ var me = this;
+ // Fetch logged-in user.
+ me.me(function(user) {
+ if (!user.errors) {
+ // Fetch list of workspaces.
+ me.workspaces(function(workspaces) {
+ if (!workspaces.errors) {
+ var i = 0;
+ // Fetch users in each workspace.
+ var fetchUsers = function() {
+ me.users(workspaces[i].id, function(users) {
+ // Prefetch images too
+ users.forEach(function(user) {
+ me._cacheUserPhoto(user);
+ });
+ if (++i < workspaces.length) {
+ fetchUsers();
+ }
+ }, null, { miss_cache: true });
+ };
+ fetchUsers();
+ }
+ }, null, { miss_cache: true })
+ }
+ }, null, { miss_cache: true });
}
};
\ No newline at end of file
diff --git a/spinner.gif b/spinner.gif
new file mode 100644
index 0000000..a89c678
Binary files /dev/null and b/spinner.gif differ
diff --git a/sprite-retina.png b/sprite-retina.png
new file mode 100644
index 0000000..2e9451c
Binary files /dev/null and b/sprite-retina.png differ
diff --git a/sprite.png b/sprite.png
index 7f59048..5ada26e 100644
Binary files a/sprite.png and b/sprite.png differ
diff --git a/typeahead.bundle.js b/typeahead.bundle.js
new file mode 100644
index 0000000..3e2d8d1
--- /dev/null
+++ b/typeahead.bundle.js
@@ -0,0 +1,1782 @@
+/*!
+ * typeahead.js 0.10.4
+ * https://github.com/twitter/typeahead.js
+ * Copyright 2013-2014 Twitter, Inc. and other contributors; Licensed MIT
+ */
+
+(function($) {
+ var _ = function() {
+ "use strict";
+ return {
+ isMsie: function() {
+ return /(msie|trident)/i.test(navigator.userAgent) ? navigator.userAgent.match(/(msie |rv:)(\d+(.\d+)?)/i)[2] : false;
+ },
+ isBlankString: function(str) {
+ return !str || /^\s*$/.test(str);
+ },
+ escapeRegExChars: function(str) {
+ return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
+ },
+ isString: function(obj) {
+ return typeof obj === "string";
+ },
+ isNumber: function(obj) {
+ return typeof obj === "number";
+ },
+ isArray: $.isArray,
+ isFunction: $.isFunction,
+ isObject: $.isPlainObject,
+ isUndefined: function(obj) {
+ return typeof obj === "undefined";
+ },
+ toStr: function toStr(s) {
+ return _.isUndefined(s) || s === null ? "" : s + "";
+ },
+ bind: $.proxy,
+ each: function(collection, cb) {
+ $.each(collection, reverseArgs);
+ function reverseArgs(index, value) {
+ return cb(value, index);
+ }
+ },
+ map: $.map,
+ filter: $.grep,
+ every: function(obj, test) {
+ var result = true;
+ if (!obj) {
+ return result;
+ }
+ $.each(obj, function(key, val) {
+ if (!(result = test.call(null, val, key, obj))) {
+ return false;
+ }
+ });
+ return !!result;
+ },
+ some: function(obj, test) {
+ var result = false;
+ if (!obj) {
+ return result;
+ }
+ $.each(obj, function(key, val) {
+ if (result = test.call(null, val, key, obj)) {
+ return false;
+ }
+ });
+ return !!result;
+ },
+ mixin: $.extend,
+ getUniqueId: function() {
+ var counter = 0;
+ return function() {
+ return counter++;
+ };
+ }(),
+ templatify: function templatify(obj) {
+ return $.isFunction(obj) ? obj : template;
+ function template() {
+ return String(obj);
+ }
+ },
+ defer: function(fn) {
+ setTimeout(fn, 0);
+ },
+ debounce: function(func, wait, immediate) {
+ var timeout, result;
+ return function() {
+ var context = this, args = arguments, later, callNow;
+ later = function() {
+ timeout = null;
+ if (!immediate) {
+ result = func.apply(context, args);
+ }
+ };
+ callNow = immediate && !timeout;
+ clearTimeout(timeout);
+ timeout = setTimeout(later, wait);
+ if (callNow) {
+ result = func.apply(context, args);
+ }
+ return result;
+ };
+ },
+ throttle: function(func, wait) {
+ var context, args, timeout, result, previous, later;
+ previous = 0;
+ later = function() {
+ previous = new Date();
+ timeout = null;
+ result = func.apply(context, args);
+ };
+ return function() {
+ var now = new Date(), remaining = wait - (now - previous);
+ context = this;
+ args = arguments;
+ if (remaining <= 0) {
+ clearTimeout(timeout);
+ timeout = null;
+ previous = now;
+ result = func.apply(context, args);
+ } else if (!timeout) {
+ timeout = setTimeout(later, remaining);
+ }
+ return result;
+ };
+ },
+ noop: function() {}
+ };
+ }();
+ var VERSION = "0.10.4";
+ var tokenizers = function() {
+ "use strict";
+ return {
+ nonword: nonword,
+ whitespace: whitespace,
+ obj: {
+ nonword: getObjTokenizer(nonword),
+ whitespace: getObjTokenizer(whitespace)
+ }
+ };
+ function whitespace(str) {
+ str = _.toStr(str);
+ return str ? str.split(/\s+/) : [];
+ }
+ function nonword(str) {
+ str = _.toStr(str);
+ return str ? str.split(/\W+/) : [];
+ }
+ function getObjTokenizer(tokenizer) {
+ return function setKey() {
+ var args = [].slice.call(arguments, 0);
+ return function tokenize(o) {
+ var tokens = [];
+ _.each(args, function(k) {
+ tokens = tokens.concat(tokenizer(_.toStr(o[k])));
+ });
+ return tokens;
+ };
+ };
+ }
+ }();
+ var LruCache = function() {
+ "use strict";
+ function LruCache(maxSize) {
+ this.maxSize = _.isNumber(maxSize) ? maxSize : 100;
+ this.reset();
+ if (this.maxSize <= 0) {
+ this.set = this.get = $.noop;
+ }
+ }
+ _.mixin(LruCache.prototype, {
+ set: function set(key, val) {
+ var tailItem = this.list.tail, node;
+ if (this.size >= this.maxSize) {
+ this.list.remove(tailItem);
+ delete this.hash[tailItem.key];
+ }
+ if (node = this.hash[key]) {
+ node.val = val;
+ this.list.moveToFront(node);
+ } else {
+ node = new Node(key, val);
+ this.list.add(node);
+ this.hash[key] = node;
+ this.size++;
+ }
+ },
+ get: function get(key) {
+ var node = this.hash[key];
+ if (node) {
+ this.list.moveToFront(node);
+ return node.val;
+ }
+ },
+ reset: function reset() {
+ this.size = 0;
+ this.hash = {};
+ this.list = new List();
+ }
+ });
+ function List() {
+ this.head = this.tail = null;
+ }
+ _.mixin(List.prototype, {
+ add: function add(node) {
+ if (this.head) {
+ node.next = this.head;
+ this.head.prev = node;
+ }
+ this.head = node;
+ this.tail = this.tail || node;
+ },
+ remove: function remove(node) {
+ node.prev ? node.prev.next = node.next : this.head = node.next;
+ node.next ? node.next.prev = node.prev : this.tail = node.prev;
+ },
+ moveToFront: function(node) {
+ this.remove(node);
+ this.add(node);
+ }
+ });
+ function Node(key, val) {
+ this.key = key;
+ this.val = val;
+ this.prev = this.next = null;
+ }
+ return LruCache;
+ }();
+ var PersistentStorage = function() {
+ "use strict";
+ var ls, methods;
+ try {
+ ls = window.localStorage;
+ ls.setItem("~~~", "!");
+ ls.removeItem("~~~");
+ } catch (err) {
+ ls = null;
+ }
+ function PersistentStorage(namespace) {
+ this.prefix = [ "__", namespace, "__" ].join("");
+ this.ttlKey = "__ttl__";
+ this.keyMatcher = new RegExp("^" + _.escapeRegExChars(this.prefix));
+ }
+ if (ls && window.JSON) {
+ methods = {
+ _prefix: function(key) {
+ return this.prefix + key;
+ },
+ _ttlKey: function(key) {
+ return this._prefix(key) + this.ttlKey;
+ },
+ get: function(key) {
+ if (this.isExpired(key)) {
+ this.remove(key);
+ }
+ return decode(ls.getItem(this._prefix(key)));
+ },
+ set: function(key, val, ttl) {
+ if (_.isNumber(ttl)) {
+ ls.setItem(this._ttlKey(key), encode(now() + ttl));
+ } else {
+ ls.removeItem(this._ttlKey(key));
+ }
+ return ls.setItem(this._prefix(key), encode(val));
+ },
+ remove: function(key) {
+ ls.removeItem(this._ttlKey(key));
+ ls.removeItem(this._prefix(key));
+ return this;
+ },
+ clear: function() {
+ var i, key, keys = [], len = ls.length;
+ for (i = 0; i < len; i++) {
+ if ((key = ls.key(i)).match(this.keyMatcher)) {
+ keys.push(key.replace(this.keyMatcher, ""));
+ }
+ }
+ for (i = keys.length; i--; ) {
+ this.remove(keys[i]);
+ }
+ return this;
+ },
+ isExpired: function(key) {
+ var ttl = decode(ls.getItem(this._ttlKey(key)));
+ return _.isNumber(ttl) && now() > ttl ? true : false;
+ }
+ };
+ } else {
+ methods = {
+ get: _.noop,
+ set: _.noop,
+ remove: _.noop,
+ clear: _.noop,
+ isExpired: _.noop
+ };
+ }
+ _.mixin(PersistentStorage.prototype, methods);
+ return PersistentStorage;
+ function now() {
+ return new Date().getTime();
+ }
+ function encode(val) {
+ return JSON.stringify(_.isUndefined(val) ? null : val);
+ }
+ function decode(val) {
+ return JSON.parse(val);
+ }
+ }();
+ var Transport = function() {
+ "use strict";
+ var pendingRequestsCount = 0, pendingRequests = {}, maxPendingRequests = 6, sharedCache = new LruCache(10);
+ function Transport(o) {
+ o = o || {};
+ this.cancelled = false;
+ this.lastUrl = null;
+ this._send = o.transport ? callbackToDeferred(o.transport) : $.ajax;
+ this._get = o.rateLimiter ? o.rateLimiter(this._get) : this._get;
+ this._cache = o.cache === false ? new LruCache(0) : sharedCache;
+ }
+ Transport.setMaxPendingRequests = function setMaxPendingRequests(num) {
+ maxPendingRequests = num;
+ };
+ Transport.resetCache = function resetCache() {
+ sharedCache.reset();
+ };
+ _.mixin(Transport.prototype, {
+ _get: function(url, o, cb) {
+ var that = this, jqXhr;
+ if (this.cancelled || url !== this.lastUrl) {
+ return;
+ }
+ if (jqXhr = pendingRequests[url]) {
+ jqXhr.done(done).fail(fail);
+ } else if (pendingRequestsCount < maxPendingRequests) {
+ pendingRequestsCount++;
+ pendingRequests[url] = this._send(url, o).done(done).fail(fail).always(always);
+ } else {
+ this.onDeckRequestArgs = [].slice.call(arguments, 0);
+ }
+ function done(resp) {
+ cb && cb(null, resp);
+ that._cache.set(url, resp);
+ }
+ function fail() {
+ cb && cb(true);
+ }
+ function always() {
+ pendingRequestsCount--;
+ delete pendingRequests[url];
+ if (that.onDeckRequestArgs) {
+ that._get.apply(that, that.onDeckRequestArgs);
+ that.onDeckRequestArgs = null;
+ }
+ }
+ },
+ get: function(url, o, cb) {
+ var resp;
+ if (_.isFunction(o)) {
+ cb = o;
+ o = {};
+ }
+ this.cancelled = false;
+ this.lastUrl = url;
+ if (resp = this._cache.get(url)) {
+ _.defer(function() {
+ cb && cb(null, resp);
+ });
+ } else {
+ this._get(url, o, cb);
+ }
+ return !!resp;
+ },
+ cancel: function() {
+ this.cancelled = true;
+ }
+ });
+ return Transport;
+ function callbackToDeferred(fn) {
+ return function customSendWrapper(url, o) {
+ var deferred = $.Deferred();
+ fn(url, o, onSuccess, onError);
+ return deferred;
+ function onSuccess(resp) {
+ _.defer(function() {
+ deferred.resolve(resp);
+ });
+ }
+ function onError(err) {
+ _.defer(function() {
+ deferred.reject(err);
+ });
+ }
+ };
+ }
+ }();
+ var SearchIndex = function() {
+ "use strict";
+ function SearchIndex(o) {
+ o = o || {};
+ if (!o.datumTokenizer || !o.queryTokenizer) {
+ $.error("datumTokenizer and queryTokenizer are both required");
+ }
+ this.datumTokenizer = o.datumTokenizer;
+ this.queryTokenizer = o.queryTokenizer;
+ this.reset();
+ }
+ _.mixin(SearchIndex.prototype, {
+ bootstrap: function bootstrap(o) {
+ this.datums = o.datums;
+ this.trie = o.trie;
+ },
+ add: function(data) {
+ var that = this;
+ data = _.isArray(data) ? data : [ data ];
+ _.each(data, function(datum) {
+ var id, tokens;
+ id = that.datums.push(datum) - 1;
+ tokens = normalizeTokens(that.datumTokenizer(datum));
+ _.each(tokens, function(token) {
+ var node, chars, ch;
+ node = that.trie;
+ chars = token.split("");
+ while (ch = chars.shift()) {
+ node = node.children[ch] || (node.children[ch] = newNode());
+ node.ids.push(id);
+ }
+ });
+ });
+ },
+ get: function get(query) {
+ var that = this, tokens, matches;
+ tokens = normalizeTokens(this.queryTokenizer(query));
+ _.each(tokens, function(token) {
+ var node, chars, ch, ids;
+ if (matches && matches.length === 0) {
+ return false;
+ }
+ node = that.trie;
+ chars = token.split("");
+ while (node && (ch = chars.shift())) {
+ node = node.children[ch];
+ }
+ if (node && chars.length === 0) {
+ ids = node.ids.slice(0);
+ matches = matches ? getIntersection(matches, ids) : ids;
+ } else {
+ matches = [];
+ return false;
+ }
+ });
+ return matches ? _.map(unique(matches), function(id) {
+ return that.datums[id];
+ }) : [];
+ },
+ reset: function reset() {
+ this.datums = [];
+ this.trie = newNode();
+ },
+ serialize: function serialize() {
+ return {
+ datums: this.datums,
+ trie: this.trie
+ };
+ }
+ });
+ return SearchIndex;
+ function normalizeTokens(tokens) {
+ tokens = _.filter(tokens, function(token) {
+ return !!token;
+ });
+ tokens = _.map(tokens, function(token) {
+ return token.toLowerCase();
+ });
+ return tokens;
+ }
+ function newNode() {
+ return {
+ ids: [],
+ children: {}
+ };
+ }
+ function unique(array) {
+ var seen = {}, uniques = [];
+ for (var i = 0, len = array.length; i < len; i++) {
+ if (!seen[array[i]]) {
+ seen[array[i]] = true;
+ uniques.push(array[i]);
+ }
+ }
+ return uniques;
+ }
+ function getIntersection(arrayA, arrayB) {
+ var ai = 0, bi = 0, intersection = [];
+ arrayA = arrayA.sort(compare);
+ arrayB = arrayB.sort(compare);
+ var lenArrayA = arrayA.length, lenArrayB = arrayB.length;
+ while (ai < lenArrayA && bi < lenArrayB) {
+ if (arrayA[ai] < arrayB[bi]) {
+ ai++;
+ } else if (arrayA[ai] > arrayB[bi]) {
+ bi++;
+ } else {
+ intersection.push(arrayA[ai]);
+ ai++;
+ bi++;
+ }
+ }
+ return intersection;
+ function compare(a, b) {
+ return a - b;
+ }
+ }
+ }();
+ var oParser = function() {
+ "use strict";
+ return {
+ local: getLocal,
+ prefetch: getPrefetch,
+ remote: getRemote
+ };
+ function getLocal(o) {
+ return o.local || null;
+ }
+ function getPrefetch(o) {
+ var prefetch, defaults;
+ defaults = {
+ url: null,
+ thumbprint: "",
+ ttl: 24 * 60 * 60 * 1e3,
+ filter: null,
+ ajax: {}
+ };
+ if (prefetch = o.prefetch || null) {
+ prefetch = _.isString(prefetch) ? {
+ url: prefetch
+ } : prefetch;
+ prefetch = _.mixin(defaults, prefetch);
+ prefetch.thumbprint = VERSION + prefetch.thumbprint;
+ prefetch.ajax.type = prefetch.ajax.type || "GET";
+ prefetch.ajax.dataType = prefetch.ajax.dataType || "json";
+ !prefetch.url && $.error("prefetch requires url to be set");
+ }
+ return prefetch;
+ }
+ function getRemote(o) {
+ var remote, defaults;
+ defaults = {
+ url: null,
+ cache: true,
+ wildcard: "%QUERY",
+ replace: null,
+ rateLimitBy: "debounce",
+ rateLimitWait: 300,
+ send: null,
+ filter: null,
+ ajax: {}
+ };
+ if (remote = o.remote || null) {
+ remote = _.isString(remote) ? {
+ url: remote
+ } : remote;
+ remote = _.mixin(defaults, remote);
+ remote.rateLimiter = /^throttle$/i.test(remote.rateLimitBy) ? byThrottle(remote.rateLimitWait) : byDebounce(remote.rateLimitWait);
+ remote.ajax.type = remote.ajax.type || "GET";
+ remote.ajax.dataType = remote.ajax.dataType || "json";
+ delete remote.rateLimitBy;
+ delete remote.rateLimitWait;
+ !remote.url && $.error("remote requires url to be set");
+ }
+ return remote;
+ function byDebounce(wait) {
+ return function(fn) {
+ return _.debounce(fn, wait);
+ };
+ }
+ function byThrottle(wait) {
+ return function(fn) {
+ return _.throttle(fn, wait);
+ };
+ }
+ }
+ }();
+ (function(root) {
+ "use strict";
+ var old, keys;
+ old = root.Bloodhound;
+ keys = {
+ data: "data",
+ protocol: "protocol",
+ thumbprint: "thumbprint"
+ };
+ root.Bloodhound = Bloodhound;
+ function Bloodhound(o) {
+ if (!o || !o.local && !o.prefetch && !o.remote) {
+ $.error("one of local, prefetch, or remote is required");
+ }
+ this.limit = o.limit || 5;
+ this.sorter = getSorter(o.sorter);
+ this.dupDetector = o.dupDetector || ignoreDuplicates;
+ this.local = oParser.local(o);
+ this.prefetch = oParser.prefetch(o);
+ this.remote = oParser.remote(o);
+ this.cacheKey = this.prefetch ? this.prefetch.cacheKey || this.prefetch.url : null;
+ this.index = new SearchIndex({
+ datumTokenizer: o.datumTokenizer,
+ queryTokenizer: o.queryTokenizer
+ });
+ this.storage = this.cacheKey ? new PersistentStorage(this.cacheKey) : null;
+ }
+ Bloodhound.noConflict = function noConflict() {
+ root.Bloodhound = old;
+ return Bloodhound;
+ };
+ Bloodhound.tokenizers = tokenizers;
+ _.mixin(Bloodhound.prototype, {
+ _loadPrefetch: function loadPrefetch(o) {
+ var that = this, serialized, deferred;
+ if (serialized = this._readFromStorage(o.thumbprint)) {
+ this.index.bootstrap(serialized);
+ deferred = $.Deferred().resolve();
+ } else {
+ deferred = $.ajax(o.url, o.ajax).done(handlePrefetchResponse);
+ }
+ return deferred;
+ function handlePrefetchResponse(resp) {
+ that.clear();
+ that.add(o.filter ? o.filter(resp) : resp);
+ that._saveToStorage(that.index.serialize(), o.thumbprint, o.ttl);
+ }
+ },
+ _getFromRemote: function getFromRemote(query, cb) {
+ var that = this, url, uriEncodedQuery;
+ if (!this.transport) {
+ return;
+ }
+ query = query || "";
+ uriEncodedQuery = encodeURIComponent(query);
+ url = this.remote.replace ? this.remote.replace(this.remote.url, query) : this.remote.url.replace(this.remote.wildcard, uriEncodedQuery);
+ return this.transport.get(url, this.remote.ajax, handleRemoteResponse);
+ function handleRemoteResponse(err, resp) {
+ err ? cb([]) : cb(that.remote.filter ? that.remote.filter(resp) : resp);
+ }
+ },
+ _cancelLastRemoteRequest: function cancelLastRemoteRequest() {
+ this.transport && this.transport.cancel();
+ },
+ _saveToStorage: function saveToStorage(data, thumbprint, ttl) {
+ if (this.storage) {
+ this.storage.set(keys.data, data, ttl);
+ this.storage.set(keys.protocol, location.protocol, ttl);
+ this.storage.set(keys.thumbprint, thumbprint, ttl);
+ }
+ },
+ _readFromStorage: function readFromStorage(thumbprint) {
+ var stored = {}, isExpired;
+ if (this.storage) {
+ stored.data = this.storage.get(keys.data);
+ stored.protocol = this.storage.get(keys.protocol);
+ stored.thumbprint = this.storage.get(keys.thumbprint);
+ }
+ isExpired = stored.thumbprint !== thumbprint || stored.protocol !== location.protocol;
+ return stored.data && !isExpired ? stored.data : null;
+ },
+ _initialize: function initialize() {
+ var that = this, local = this.local, deferred;
+ deferred = this.prefetch ? this._loadPrefetch(this.prefetch) : $.Deferred().resolve();
+ local && deferred.done(addLocalToIndex);
+ this.transport = this.remote ? new Transport(this.remote) : null;
+ return this.initPromise = deferred.promise();
+ function addLocalToIndex() {
+ that.add(_.isFunction(local) ? local() : local);
+ }
+ },
+ initialize: function initialize(force) {
+ return !this.initPromise || force ? this._initialize() : this.initPromise;
+ },
+ add: function add(data) {
+ this.index.add(data);
+ },
+ get: function get(query, cb) {
+ var that = this, matches = [], cacheHit = false;
+ matches = this.index.get(query);
+ matches = this.sorter(matches).slice(0, this.limit);
+ matches.length < this.limit ? cacheHit = this._getFromRemote(query, returnRemoteMatches) : this._cancelLastRemoteRequest();
+ if (!cacheHit) {
+ (matches.length > 0 || !this.transport) && cb && cb(matches);
+ }
+ function returnRemoteMatches(remoteMatches) {
+ var matchesWithBackfill = matches.slice(0);
+ _.each(remoteMatches, function(remoteMatch) {
+ var isDuplicate;
+ isDuplicate = _.some(matchesWithBackfill, function(match) {
+ return that.dupDetector(remoteMatch, match);
+ });
+ !isDuplicate && matchesWithBackfill.push(remoteMatch);
+ return matchesWithBackfill.length < that.limit;
+ });
+ cb && cb(that.sorter(matchesWithBackfill));
+ }
+ },
+ clear: function clear() {
+ this.index.reset();
+ },
+ clearPrefetchCache: function clearPrefetchCache() {
+ this.storage && this.storage.clear();
+ },
+ clearRemoteCache: function clearRemoteCache() {
+ this.transport && Transport.resetCache();
+ },
+ ttAdapter: function ttAdapter() {
+ return _.bind(this.get, this);
+ }
+ });
+ return Bloodhound;
+ function getSorter(sortFn) {
+ return _.isFunction(sortFn) ? sort : noSort;
+ function sort(array) {
+ return array.sort(sortFn);
+ }
+ function noSort(array) {
+ return array;
+ }
+ }
+ function ignoreDuplicates() {
+ return false;
+ }
+ })(this);
+ var html = function() {
+ return {
+ wrapper: '',
+ dropdown: '',
+ dataset: '',
+ suggestions: '',
+ suggestion: ''
+ };
+ }();
+ var css = function() {
+ "use strict";
+ var css = {
+ wrapper: {
+ position: "relative",
+ display: "inline-block"
+ },
+ hint: {
+ position: "absolute",
+ top: "0",
+ left: "0",
+ borderColor: "transparent",
+ boxShadow: "none",
+ opacity: "1"
+ },
+ input: {
+ position: "relative",
+ verticalAlign: "top",
+ backgroundColor: "transparent"
+ },
+ inputWithNoHint: {
+ position: "relative",
+ verticalAlign: "top"
+ },
+ dropdown: {
+ position: "absolute",
+ top: "100%",
+ left: "0",
+ zIndex: "100",
+ display: "none"
+ },
+ suggestions: {
+ display: "block"
+ },
+ suggestion: {
+ whiteSpace: "nowrap",
+ cursor: "pointer"
+ },
+ suggestionChild: {
+ whiteSpace: "normal"
+ },
+ ltr: {
+ left: "0",
+ right: "auto"
+ },
+ rtl: {
+ left: "auto",
+ right: " 0"
+ }
+ };
+ if (_.isMsie()) {
+ _.mixin(css.input, {
+ backgroundImage: "url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)"
+ });
+ }
+ if (_.isMsie() && _.isMsie() <= 7) {
+ _.mixin(css.input, {
+ marginTop: "-1px"
+ });
+ }
+ return css;
+ }();
+ var EventBus = function() {
+ "use strict";
+ var namespace = "typeahead:";
+ function EventBus(o) {
+ if (!o || !o.el) {
+ $.error("EventBus initialized without el");
+ }
+ this.$el = $(o.el);
+ }
+ _.mixin(EventBus.prototype, {
+ trigger: function(type) {
+ var args = [].slice.call(arguments, 1);
+ this.$el.trigger(namespace + type, args);
+ }
+ });
+ return EventBus;
+ }();
+ var EventEmitter = function() {
+ "use strict";
+ var splitter = /\s+/, nextTick = getNextTick();
+ return {
+ onSync: onSync,
+ onAsync: onAsync,
+ off: off,
+ trigger: trigger
+ };
+ function on(method, types, cb, context) {
+ var type;
+ if (!cb) {
+ return this;
+ }
+ types = types.split(splitter);
+ cb = context ? bindContext(cb, context) : cb;
+ this._callbacks = this._callbacks || {};
+ while (type = types.shift()) {
+ this._callbacks[type] = this._callbacks[type] || {
+ sync: [],
+ async: []
+ };
+ this._callbacks[type][method].push(cb);
+ }
+ return this;
+ }
+ function onAsync(types, cb, context) {
+ return on.call(this, "async", types, cb, context);
+ }
+ function onSync(types, cb, context) {
+ return on.call(this, "sync", types, cb, context);
+ }
+ function off(types) {
+ var type;
+ if (!this._callbacks) {
+ return this;
+ }
+ types = types.split(splitter);
+ while (type = types.shift()) {
+ delete this._callbacks[type];
+ }
+ return this;
+ }
+ function trigger(types) {
+ var type, callbacks, args, syncFlush, asyncFlush;
+ if (!this._callbacks) {
+ return this;
+ }
+ types = types.split(splitter);
+ args = [].slice.call(arguments, 1);
+ while ((type = types.shift()) && (callbacks = this._callbacks[type])) {
+ syncFlush = getFlush(callbacks.sync, this, [ type ].concat(args));
+ asyncFlush = getFlush(callbacks.async, this, [ type ].concat(args));
+ syncFlush() && nextTick(asyncFlush);
+ }
+ return this;
+ }
+ function getFlush(callbacks, context, args) {
+ return flush;
+ function flush() {
+ var cancelled;
+ for (var i = 0, len = callbacks.length; !cancelled && i < len; i += 1) {
+ cancelled = callbacks[i].apply(context, args) === false;
+ }
+ return !cancelled;
+ }
+ }
+ function getNextTick() {
+ var nextTickFn;
+ if (window.setImmediate) {
+ nextTickFn = function nextTickSetImmediate(fn) {
+ setImmediate(function() {
+ fn();
+ });
+ };
+ } else {
+ nextTickFn = function nextTickSetTimeout(fn) {
+ setTimeout(function() {
+ fn();
+ }, 0);
+ };
+ }
+ return nextTickFn;
+ }
+ function bindContext(fn, context) {
+ return fn.bind ? fn.bind(context) : function() {
+ fn.apply(context, [].slice.call(arguments, 0));
+ };
+ }
+ }();
+ var highlight = function(doc) {
+ "use strict";
+ var defaults = {
+ node: null,
+ pattern: null,
+ tagName: "strong",
+ className: null,
+ wordsOnly: false,
+ caseSensitive: false
+ };
+ return function hightlight(o) {
+ var regex;
+ o = _.mixin({}, defaults, o);
+ if (!o.node || !o.pattern) {
+ return;
+ }
+ o.pattern = _.isArray(o.pattern) ? o.pattern : [ o.pattern ];
+ regex = getRegex(o.pattern, o.caseSensitive, o.wordsOnly);
+ traverse(o.node, hightlightTextNode);
+ function hightlightTextNode(textNode) {
+ var match, patternNode, wrapperNode;
+ if (match = regex.exec(textNode.data)) {
+ wrapperNode = doc.createElement(o.tagName);
+ o.className && (wrapperNode.className = o.className);
+ patternNode = textNode.splitText(match.index);
+ patternNode.splitText(match[0].length);
+ wrapperNode.appendChild(patternNode.cloneNode(true));
+ textNode.parentNode.replaceChild(wrapperNode, patternNode);
+ }
+ return !!match;
+ }
+ function traverse(el, hightlightTextNode) {
+ var childNode, TEXT_NODE_TYPE = 3;
+ for (var i = 0; i < el.childNodes.length; i++) {
+ childNode = el.childNodes[i];
+ if (childNode.nodeType === TEXT_NODE_TYPE) {
+ i += hightlightTextNode(childNode) ? 1 : 0;
+ } else {
+ traverse(childNode, hightlightTextNode);
+ }
+ }
+ }
+ };
+ function getRegex(patterns, caseSensitive, wordsOnly) {
+ var escapedPatterns = [], regexStr;
+ for (var i = 0, len = patterns.length; i < len; i++) {
+ escapedPatterns.push(_.escapeRegExChars(patterns[i]));
+ }
+ regexStr = wordsOnly ? "\\b(" + escapedPatterns.join("|") + ")\\b" : "(" + escapedPatterns.join("|") + ")";
+ return caseSensitive ? new RegExp(regexStr) : new RegExp(regexStr, "i");
+ }
+ }(window.document);
+ var Input = function() {
+ "use strict";
+ var specialKeyCodeMap;
+ specialKeyCodeMap = {
+ 9: "tab",
+ 27: "esc",
+ 37: "left",
+ 39: "right",
+ 13: "enter",
+ 38: "up",
+ 40: "down"
+ };
+ function Input(o) {
+ var that = this, onBlur, onFocus, onKeydown, onInput;
+ o = o || {};
+ if (!o.input) {
+ $.error("input is missing");
+ }
+ onBlur = _.bind(this._onBlur, this);
+ onFocus = _.bind(this._onFocus, this);
+ onKeydown = _.bind(this._onKeydown, this);
+ onInput = _.bind(this._onInput, this);
+ this.$hint = $(o.hint);
+ this.$input = $(o.input).on("blur.tt", onBlur).on("focus.tt", onFocus).on("keydown.tt", onKeydown);
+ if (this.$hint.length === 0) {
+ this.setHint = this.getHint = this.clearHint = this.clearHintIfInvalid = _.noop;
+ }
+ if (!_.isMsie()) {
+ this.$input.on("input.tt", onInput);
+ } else {
+ this.$input.on("keydown.tt keypress.tt cut.tt paste.tt", function($e) {
+ if (specialKeyCodeMap[$e.which || $e.keyCode]) {
+ return;
+ }
+ _.defer(_.bind(that._onInput, that, $e));
+ });
+ }
+ this.query = this.$input.val();
+ this.$overflowHelper = buildOverflowHelper(this.$input);
+ }
+ Input.normalizeQuery = function(str) {
+ return (str || "").replace(/^\s*/g, "").replace(/\s{2,}/g, " ");
+ };
+ _.mixin(Input.prototype, EventEmitter, {
+ _onBlur: function onBlur() {
+ this.resetInputValue();
+ this.trigger("blurred");
+ },
+ _onFocus: function onFocus() {
+ this.trigger("focused");
+ },
+ _onKeydown: function onKeydown($e) {
+ var keyName = specialKeyCodeMap[$e.which || $e.keyCode];
+ this._managePreventDefault(keyName, $e);
+ if (keyName && this._shouldTrigger(keyName, $e)) {
+ this.trigger(keyName + "Keyed", $e);
+ }
+ },
+ _onInput: function onInput() {
+ this._checkInputValue();
+ },
+ _managePreventDefault: function managePreventDefault(keyName, $e) {
+ var preventDefault, hintValue, inputValue;
+ switch (keyName) {
+ case "tab":
+ hintValue = this.getHint();
+ inputValue = this.getInputValue();
+ preventDefault = hintValue && hintValue !== inputValue && !withModifier($e);
+ break;
+
+ case "up":
+ case "down":
+ preventDefault = !withModifier($e);
+ break;
+
+ default:
+ preventDefault = false;
+ }
+ preventDefault && $e.preventDefault();
+ },
+ _shouldTrigger: function shouldTrigger(keyName, $e) {
+ var trigger;
+ switch (keyName) {
+ case "tab":
+ trigger = !withModifier($e);
+ break;
+
+ default:
+ trigger = true;
+ }
+ return trigger;
+ },
+ _checkInputValue: function checkInputValue() {
+ var inputValue, areEquivalent, hasDifferentWhitespace;
+ inputValue = this.getInputValue();
+ areEquivalent = areQueriesEquivalent(inputValue, this.query);
+ hasDifferentWhitespace = areEquivalent ? this.query.length !== inputValue.length : false;
+ this.query = inputValue;
+ if (!areEquivalent) {
+ this.trigger("queryChanged", this.query);
+ } else if (hasDifferentWhitespace) {
+ this.trigger("whitespaceChanged", this.query);
+ }
+ },
+ focus: function focus() {
+ this.$input.focus();
+ },
+ blur: function blur() {
+ this.$input.blur();
+ },
+ getQuery: function getQuery() {
+ return this.query;
+ },
+ setQuery: function setQuery(query) {
+ this.query = query;
+ },
+ getInputValue: function getInputValue() {
+ return this.$input.val();
+ },
+ setInputValue: function setInputValue(value, silent) {
+ this.$input.val(value);
+ silent ? this.clearHint() : this._checkInputValue();
+ },
+ resetInputValue: function resetInputValue() {
+ this.setInputValue(this.query, true);
+ },
+ getHint: function getHint() {
+ return this.$hint.val();
+ },
+ setHint: function setHint(value) {
+ this.$hint.val(value);
+ },
+ clearHint: function clearHint() {
+ this.setHint("");
+ },
+ clearHintIfInvalid: function clearHintIfInvalid() {
+ var val, hint, valIsPrefixOfHint, isValid;
+ val = this.getInputValue();
+ hint = this.getHint();
+ valIsPrefixOfHint = val !== hint && hint.indexOf(val) === 0;
+ isValid = val !== "" && valIsPrefixOfHint && !this.hasOverflow();
+ !isValid && this.clearHint();
+ },
+ getLanguageDirection: function getLanguageDirection() {
+ return (this.$input.css("direction") || "ltr").toLowerCase();
+ },
+ hasOverflow: function hasOverflow() {
+ var constraint = this.$input.width() - 2;
+ this.$overflowHelper.text(this.getInputValue());
+ return this.$overflowHelper.width() >= constraint;
+ },
+ isCursorAtEnd: function() {
+ var valueLength, selectionStart, range;
+ valueLength = this.$input.val().length;
+ selectionStart = this.$input[0].selectionStart;
+ if (_.isNumber(selectionStart)) {
+ return selectionStart === valueLength;
+ } else if (document.selection) {
+ range = document.selection.createRange();
+ range.moveStart("character", -valueLength);
+ return valueLength === range.text.length;
+ }
+ return true;
+ },
+ destroy: function destroy() {
+ this.$hint.off(".tt");
+ this.$input.off(".tt");
+ this.$hint = this.$input = this.$overflowHelper = null;
+ }
+ });
+ return Input;
+ function buildOverflowHelper($input) {
+ return $('').css({
+ position: "absolute",
+ visibility: "hidden",
+ whiteSpace: "pre",
+ fontFamily: $input.css("font-family"),
+ fontSize: $input.css("font-size"),
+ fontStyle: $input.css("font-style"),
+ fontVariant: $input.css("font-variant"),
+ fontWeight: $input.css("font-weight"),
+ wordSpacing: $input.css("word-spacing"),
+ letterSpacing: $input.css("letter-spacing"),
+ textIndent: $input.css("text-indent"),
+ textRendering: $input.css("text-rendering"),
+ textTransform: $input.css("text-transform")
+ }).insertAfter($input);
+ }
+ function areQueriesEquivalent(a, b) {
+ return Input.normalizeQuery(a) === Input.normalizeQuery(b);
+ }
+ function withModifier($e) {
+ return $e.altKey || $e.ctrlKey || $e.metaKey || $e.shiftKey;
+ }
+ }();
+ var Dataset = function() {
+ "use strict";
+ var datasetKey = "ttDataset", valueKey = "ttValue", datumKey = "ttDatum";
+ function Dataset(o) {
+ o = o || {};
+ o.templates = o.templates || {};
+ if (!o.source) {
+ $.error("missing source");
+ }
+ if (o.name && !isValidName(o.name)) {
+ $.error("invalid dataset name: " + o.name);
+ }
+ this.query = null;
+ this.highlight = !!o.highlight;
+ this.name = o.name || _.getUniqueId();
+ this.source = o.source;
+ this.displayFn = getDisplayFn(o.display || o.displayKey);
+ this.templates = getTemplates(o.templates, this.displayFn);
+ this.$el = $(html.dataset.replace("%CLASS%", this.name));
+ }
+ Dataset.extractDatasetName = function extractDatasetName(el) {
+ return $(el).data(datasetKey);
+ };
+ Dataset.extractValue = function extractDatum(el) {
+ return $(el).data(valueKey);
+ };
+ Dataset.extractDatum = function extractDatum(el) {
+ return $(el).data(datumKey);
+ };
+ _.mixin(Dataset.prototype, EventEmitter, {
+ _render: function render(query, suggestions) {
+ if (!this.$el) {
+ return;
+ }
+ var that = this, hasSuggestions;
+ this.$el.empty();
+ hasSuggestions = suggestions && suggestions.length;
+ if (!hasSuggestions && this.templates.empty) {
+ this.$el.html(getEmptyHtml()).prepend(that.templates.header ? getHeaderHtml() : null).append(that.templates.footer ? getFooterHtml() : null);
+ } else if (hasSuggestions) {
+ this.$el.html(getSuggestionsHtml()).prepend(that.templates.header ? getHeaderHtml() : null).append(that.templates.footer ? getFooterHtml() : null);
+ }
+ this.trigger("rendered");
+ function getEmptyHtml() {
+ return that.templates.empty({
+ query: query,
+ isEmpty: true
+ });
+ }
+ function getSuggestionsHtml() {
+ var $suggestions, nodes;
+ $suggestions = $(html.suggestions).css(css.suggestions);
+ nodes = _.map(suggestions, getSuggestionNode);
+ $suggestions.append.apply($suggestions, nodes);
+ that.highlight && highlight({
+ className: "tt-highlight",
+ node: $suggestions[0],
+ pattern: query
+ });
+ return $suggestions;
+ function getSuggestionNode(suggestion) {
+ var $el;
+ $el = $(html.suggestion).append(that.templates.suggestion(suggestion)).data(datasetKey, that.name).data(valueKey, that.displayFn(suggestion)).data(datumKey, suggestion);
+ $el.children().each(function() {
+ $(this).css(css.suggestionChild);
+ });
+ return $el;
+ }
+ }
+ function getHeaderHtml() {
+ return that.templates.header({
+ query: query,
+ isEmpty: !hasSuggestions
+ });
+ }
+ function getFooterHtml() {
+ return that.templates.footer({
+ query: query,
+ isEmpty: !hasSuggestions
+ });
+ }
+ },
+ getRoot: function getRoot() {
+ return this.$el;
+ },
+ update: function update(query) {
+ var that = this;
+ this.query = query;
+ this.canceled = false;
+ this.source(query, render);
+ function render(suggestions) {
+ if (!that.canceled && query === that.query) {
+ that._render(query, suggestions);
+ }
+ }
+ },
+ cancel: function cancel() {
+ this.canceled = true;
+ },
+ clear: function clear() {
+ this.cancel();
+ this.$el.empty();
+ this.trigger("rendered");
+ },
+ isEmpty: function isEmpty() {
+ return this.$el.is(":empty");
+ },
+ destroy: function destroy() {
+ this.$el = null;
+ }
+ });
+ return Dataset;
+ function getDisplayFn(display) {
+ display = display || "value";
+ return _.isFunction(display) ? display : displayFn;
+ function displayFn(obj) {
+ return obj[display];
+ }
+ }
+ function getTemplates(templates, displayFn) {
+ return {
+ empty: templates.empty && _.templatify(templates.empty),
+ header: templates.header && _.templatify(templates.header),
+ footer: templates.footer && _.templatify(templates.footer),
+ suggestion: templates.suggestion || suggestionTemplate
+ };
+ function suggestionTemplate(context) {
+ return "
+ You must log in to Asana before you can add tasks.
+ Sign Up
+ Log In
+
" + displayFn(context) + "
"; + } + } + function isValidName(str) { + return /^[_a-zA-Z0-9-]+$/.test(str); + } + }(); + var Dropdown = function() { + "use strict"; + function Dropdown(o) { + var that = this, onSuggestionClick, onSuggestionMouseEnter, onSuggestionMouseLeave; + o = o || {}; + if (!o.menu) { + $.error("menu is required"); + } + this.isOpen = false; + this.isEmpty = true; + this.datasets = _.map(o.datasets, initializeDataset); + onSuggestionClick = _.bind(this._onSuggestionClick, this); + onSuggestionMouseEnter = _.bind(this._onSuggestionMouseEnter, this); + onSuggestionMouseLeave = _.bind(this._onSuggestionMouseLeave, this); + this.$menu = $(o.menu).on("click.tt", ".tt-suggestion", onSuggestionClick).on("mouseenter.tt", ".tt-suggestion", onSuggestionMouseEnter).on("mouseleave.tt", ".tt-suggestion", onSuggestionMouseLeave); + _.each(this.datasets, function(dataset) { + that.$menu.append(dataset.getRoot()); + dataset.onSync("rendered", that._onRendered, that); + }); + } + _.mixin(Dropdown.prototype, EventEmitter, { + _onSuggestionClick: function onSuggestionClick($e) { + this.trigger("suggestionClicked", $($e.currentTarget)); + }, + _onSuggestionMouseEnter: function onSuggestionMouseEnter($e) { + this._removeCursor(); + this._setCursor($($e.currentTarget), true); + }, + _onSuggestionMouseLeave: function onSuggestionMouseLeave() { + this._removeCursor(); + }, + _onRendered: function onRendered() { + this.isEmpty = _.every(this.datasets, isDatasetEmpty); + this.isEmpty ? this._hide() : this.isOpen && this._show(); + this.trigger("datasetRendered"); + function isDatasetEmpty(dataset) { + return dataset.isEmpty(); + } + }, + _hide: function() { + this.$menu.hide(); + }, + _show: function() { + this.$menu.css("display", "block"); + }, + _getSuggestions: function getSuggestions() { + return this.$menu.find(".tt-suggestion"); + }, + _getCursor: function getCursor() { + return this.$menu.find(".tt-cursor").first(); + }, + _setCursor: function setCursor($el, silent) { + $el.first().addClass("tt-cursor"); + !silent && this.trigger("cursorMoved"); + }, + _removeCursor: function removeCursor() { + this._getCursor().removeClass("tt-cursor"); + }, + _moveCursor: function moveCursor(increment) { + var $suggestions, $oldCursor, newCursorIndex, $newCursor; + if (!this.isOpen) { + return; + } + $oldCursor = this._getCursor(); + $suggestions = this._getSuggestions(); + this._removeCursor(); + newCursorIndex = $suggestions.index($oldCursor) + increment; + newCursorIndex = (newCursorIndex + 1) % ($suggestions.length + 1) - 1; + if (newCursorIndex === -1) { + this.trigger("cursorRemoved"); + return; + } else if (newCursorIndex < -1) { + newCursorIndex = $suggestions.length - 1; + } + this._setCursor($newCursor = $suggestions.eq(newCursorIndex)); + this._ensureVisible($newCursor); + }, + _ensureVisible: function ensureVisible($el) { + var elTop, elBottom, menuScrollTop, menuHeight; + elTop = $el.position().top; + elBottom = elTop + $el.outerHeight(true); + menuScrollTop = this.$menu.scrollTop(); + menuHeight = this.$menu.height() + parseInt(this.$menu.css("paddingTop"), 10) + parseInt(this.$menu.css("paddingBottom"), 10); + if (elTop < 0) { + this.$menu.scrollTop(menuScrollTop + elTop); + } else if (menuHeight < elBottom) { + this.$menu.scrollTop(menuScrollTop + (elBottom - menuHeight)); + } + }, + close: function close() { + if (this.isOpen) { + this.isOpen = false; + this._removeCursor(); + this._hide(); + this.trigger("closed"); + } + }, + open: function open() { + if (!this.isOpen) { + this.isOpen = true; + !this.isEmpty && this._show(); + this.trigger("opened"); + } + }, + setLanguageDirection: function setLanguageDirection(dir) { + this.$menu.css(dir === "ltr" ? css.ltr : css.rtl); + }, + moveCursorUp: function moveCursorUp() { + this._moveCursor(-1); + }, + moveCursorDown: function moveCursorDown() { + this._moveCursor(+1); + }, + getDatumForSuggestion: function getDatumForSuggestion($el) { + var datum = null; + if ($el.length) { + datum = { + raw: Dataset.extractDatum($el), + value: Dataset.extractValue($el), + datasetName: Dataset.extractDatasetName($el) + }; + } + return datum; + }, + getDatumForCursor: function getDatumForCursor() { + return this.getDatumForSuggestion(this._getCursor().first()); + }, + getDatumForTopSuggestion: function getDatumForTopSuggestion() { + return this.getDatumForSuggestion(this._getSuggestions().first()); + }, + update: function update(query) { + _.each(this.datasets, updateDataset); + function updateDataset(dataset) { + dataset.update(query); + } + }, + empty: function empty() { + _.each(this.datasets, clearDataset); + this.isEmpty = true; + function clearDataset(dataset) { + dataset.clear(); + } + }, + isVisible: function isVisible() { + return this.isOpen && !this.isEmpty; + }, + destroy: function destroy() { + this.$menu.off(".tt"); + this.$menu = null; + _.each(this.datasets, destroyDataset); + function destroyDataset(dataset) { + dataset.destroy(); + } + } + }); + return Dropdown; + function initializeDataset(oDataset) { + return new Dataset(oDataset); + } + }(); + var Typeahead = function() { + "use strict"; + var attrsKey = "ttAttrs"; + function Typeahead(o) { + var $menu, $input, $hint; + o = o || {}; + if (!o.input) { + $.error("missing input"); + } + this.isActivated = false; + this.autoselect = !!o.autoselect; + this.minLength = _.isNumber(o.minLength) ? o.minLength : 1; + this.$node = buildDom(o.input, o.withHint); + $menu = this.$node.find(".tt-dropdown-menu"); + $input = this.$node.find(".tt-input"); + $hint = this.$node.find(".tt-hint"); + $input.on("blur.tt", function($e) { + var active, isActive, hasActive; + active = document.activeElement; + isActive = $menu.is(active); + hasActive = $menu.has(active).length > 0; + if (_.isMsie() && (isActive || hasActive)) { + $e.preventDefault(); + $e.stopImmediatePropagation(); + _.defer(function() { + $input.focus(); + }); + } + }); + $menu.on("mousedown.tt", function($e) { + $e.preventDefault(); + }); + this.eventBus = o.eventBus || new EventBus({ + el: $input + }); + this.dropdown = new Dropdown({ + menu: $menu, + datasets: o.datasets + }).onSync("suggestionClicked", this._onSuggestionClicked, this).onSync("cursorMoved", this._onCursorMoved, this).onSync("cursorRemoved", this._onCursorRemoved, this).onSync("opened", this._onOpened, this).onSync("closed", this._onClosed, this).onAsync("datasetRendered", this._onDatasetRendered, this); + this.input = new Input({ + input: $input, + hint: $hint + }).onSync("focused", this._onFocused, this).onSync("blurred", this._onBlurred, this).onSync("enterKeyed", this._onEnterKeyed, this).onSync("tabKeyed", this._onTabKeyed, this).onSync("escKeyed", this._onEscKeyed, this).onSync("upKeyed", this._onUpKeyed, this).onSync("downKeyed", this._onDownKeyed, this).onSync("leftKeyed", this._onLeftKeyed, this).onSync("rightKeyed", this._onRightKeyed, this).onSync("queryChanged", this._onQueryChanged, this).onSync("whitespaceChanged", this._onWhitespaceChanged, this); + this._setLanguageDirection(); + } + _.mixin(Typeahead.prototype, { + _onSuggestionClicked: function onSuggestionClicked(type, $el) { + var datum; + if (datum = this.dropdown.getDatumForSuggestion($el)) { + this._select(datum); + } + }, + _onCursorMoved: function onCursorMoved() { + var datum = this.dropdown.getDatumForCursor(); + this.input.setInputValue(datum.value, true); + this.eventBus.trigger("cursorchanged", datum.raw, datum.datasetName); + }, + _onCursorRemoved: function onCursorRemoved() { + this.input.resetInputValue(); + this._updateHint(); + }, + _onDatasetRendered: function onDatasetRendered() { + this._updateHint(); + }, + _onOpened: function onOpened() { + this._updateHint(); + this.eventBus.trigger("opened"); + }, + _onClosed: function onClosed() { + this.input.clearHint(); + this.eventBus.trigger("closed"); + }, + _onFocused: function onFocused() { + this.isActivated = true; + this.dropdown.open(); + }, + _onBlurred: function onBlurred() { + this.isActivated = false; + this.dropdown.empty(); + this.dropdown.close(); + }, + _onEnterKeyed: function onEnterKeyed(type, $e) { + var cursorDatum, topSuggestionDatum; + cursorDatum = this.dropdown.getDatumForCursor(); + topSuggestionDatum = this.dropdown.getDatumForTopSuggestion(); + if (cursorDatum) { + this._select(cursorDatum); + $e.preventDefault(); + } else if (this.autoselect && topSuggestionDatum) { + this._select(topSuggestionDatum); + $e.preventDefault(); + } + }, + _onTabKeyed: function onTabKeyed(type, $e) { + var datum; + if (datum = this.dropdown.getDatumForCursor()) { + this._select(datum); + $e.preventDefault(); + } else { + this._autocomplete(true); + } + }, + _onEscKeyed: function onEscKeyed() { + this.dropdown.close(); + this.input.resetInputValue(); + }, + _onUpKeyed: function onUpKeyed() { + var query = this.input.getQuery(); + this.dropdown.isEmpty && query.length >= this.minLength ? this.dropdown.update(query) : this.dropdown.moveCursorUp(); + this.dropdown.open(); + }, + _onDownKeyed: function onDownKeyed() { + var query = this.input.getQuery(); + this.dropdown.isEmpty && query.length >= this.minLength ? this.dropdown.update(query) : this.dropdown.moveCursorDown(); + this.dropdown.open(); + }, + _onLeftKeyed: function onLeftKeyed() { + this.dir === "rtl" && this._autocomplete(); + }, + _onRightKeyed: function onRightKeyed() { + this.dir === "ltr" && this._autocomplete(); + }, + _onQueryChanged: function onQueryChanged(e, query) { + this.input.clearHintIfInvalid(); + query.length >= this.minLength ? this.dropdown.update(query) : this.dropdown.empty(); + this.dropdown.open(); + this._setLanguageDirection(); + }, + _onWhitespaceChanged: function onWhitespaceChanged() { + this._updateHint(); + this.dropdown.open(); + }, + _setLanguageDirection: function setLanguageDirection() { + var dir; + if (this.dir !== (dir = this.input.getLanguageDirection())) { + this.dir = dir; + this.$node.css("direction", dir); + this.dropdown.setLanguageDirection(dir); + } + }, + _updateHint: function updateHint() { + var datum, val, query, escapedQuery, frontMatchRegEx, match; + datum = this.dropdown.getDatumForTopSuggestion(); + if (datum && this.dropdown.isVisible() && !this.input.hasOverflow()) { + val = this.input.getInputValue(); + query = Input.normalizeQuery(val); + escapedQuery = _.escapeRegExChars(query); + frontMatchRegEx = new RegExp("^(?:" + escapedQuery + ")(.+$)", "i"); + match = frontMatchRegEx.exec(datum.value); + match ? this.input.setHint(val + match[1]) : this.input.clearHint(); + } else { + this.input.clearHint(); + } + }, + _autocomplete: function autocomplete(laxCursor) { + var hint, query, isCursorAtEnd, datum; + hint = this.input.getHint(); + query = this.input.getQuery(); + isCursorAtEnd = laxCursor || this.input.isCursorAtEnd(); + if (hint && query !== hint && isCursorAtEnd) { + datum = this.dropdown.getDatumForTopSuggestion(); + datum && this.input.setInputValue(datum.value); + this.eventBus.trigger("autocompleted", datum.raw, datum.datasetName); + } + }, + _select: function select(datum) { + this.input.setQuery(datum.value); + this.input.setInputValue(datum.value, true); + this._setLanguageDirection(); + this.eventBus.trigger("selected", datum.raw, datum.datasetName); + this.dropdown.close(); + _.defer(_.bind(this.dropdown.empty, this.dropdown)); + }, + open: function open() { + this.dropdown.open(); + }, + close: function close() { + this.dropdown.close(); + }, + setVal: function setVal(val) { + val = _.toStr(val); + if (this.isActivated) { + this.input.setInputValue(val); + } else { + this.input.setQuery(val); + this.input.setInputValue(val, true); + } + this._setLanguageDirection(); + }, + getVal: function getVal() { + return this.input.getQuery(); + }, + destroy: function destroy() { + this.input.destroy(); + this.dropdown.destroy(); + destroyDomStructure(this.$node); + this.$node = null; + } + }); + return Typeahead; + function buildDom(input, withHint) { + var $input, $wrapper, $dropdown, $hint; + $input = $(input); + $wrapper = $(html.wrapper).css(css.wrapper); + $dropdown = $(html.dropdown).css(css.dropdown); + $hint = $input.clone().css(css.hint).css(getBackgroundStyles($input)); + $hint.val("").removeData().addClass("tt-hint").removeAttr("id name placeholder required").prop("readonly", true).attr({ + autocomplete: "off", + spellcheck: "false", + tabindex: -1 + }); + $input.data(attrsKey, { + dir: $input.attr("dir"), + autocomplete: $input.attr("autocomplete"), + spellcheck: $input.attr("spellcheck"), + style: $input.attr("style") + }); + $input.addClass("tt-input").attr({ + autocomplete: "off", + spellcheck: false + }).css(withHint ? css.input : css.inputWithNoHint); + try { + !$input.attr("dir") && $input.attr("dir", "auto"); + } catch (e) {} + return $input.wrap($wrapper).parent().prepend(withHint ? $hint : null).append($dropdown); + } + function getBackgroundStyles($el) { + return { + backgroundAttachment: $el.css("background-attachment"), + backgroundClip: $el.css("background-clip"), + backgroundColor: $el.css("background-color"), + backgroundImage: $el.css("background-image"), + backgroundOrigin: $el.css("background-origin"), + backgroundPosition: $el.css("background-position"), + backgroundRepeat: $el.css("background-repeat"), + backgroundSize: $el.css("background-size") + }; + } + function destroyDomStructure($node) { + var $input = $node.find(".tt-input"); + _.each($input.data(attrsKey), function(val, key) { + _.isUndefined(val) ? $input.removeAttr(key) : $input.attr(key, val); + }); + $input.detach().removeData(attrsKey).removeClass("tt-input").insertAfter($node); + $node.remove(); + } + }(); + (function() { + "use strict"; + var old, typeaheadKey, methods; + old = $.fn.typeahead; + typeaheadKey = "ttTypeahead"; + methods = { + initialize: function initialize(o, datasets) { + datasets = _.isArray(datasets) ? datasets : [].slice.call(arguments, 1); + o = o || {}; + return this.each(attach); + function attach() { + var $input = $(this), eventBus, typeahead; + _.each(datasets, function(d) { + d.highlight = !!o.highlight; + }); + typeahead = new Typeahead({ + input: $input, + eventBus: eventBus = new EventBus({ + el: $input + }), + withHint: _.isUndefined(o.hint) ? true : !!o.hint, + minLength: o.minLength, + autoselect: o.autoselect, + datasets: datasets + }); + $input.data(typeaheadKey, typeahead); + } + }, + open: function open() { + return this.each(openTypeahead); + function openTypeahead() { + var $input = $(this), typeahead; + if (typeahead = $input.data(typeaheadKey)) { + typeahead.open(); + } + } + }, + close: function close() { + return this.each(closeTypeahead); + function closeTypeahead() { + var $input = $(this), typeahead; + if (typeahead = $input.data(typeaheadKey)) { + typeahead.close(); + } + } + }, + val: function val(newVal) { + return !arguments.length ? getVal(this.first()) : this.each(setVal); + function setVal() { + var $input = $(this), typeahead; + if (typeahead = $input.data(typeaheadKey)) { + typeahead.setVal(newVal); + } + } + function getVal($input) { + var typeahead, query; + if (typeahead = $input.data(typeaheadKey)) { + query = typeahead.getVal(); + } + return query; + } + }, + destroy: function destroy() { + return this.each(unattach); + function unattach() { + var $input = $(this), typeahead; + if (typeahead = $input.data(typeaheadKey)) { + typeahead.destroy(); + $input.removeData(typeaheadKey); + } + } + } + }; + $.fn.typeahead = function(method) { + var tts; + if (methods[method] && method !== "initialize") { + tts = this.filter(function() { + return !!$(this).data(typeaheadKey); + }); + return methods[method].apply(tts, [].slice.call(arguments, 1)); + } else { + return methods.initialize.apply(this, arguments); + } + }; + $.fn.typeahead.noConflict = function noConflict() { + $.fn.typeahead = old; + return this; + }; + })(); +})(window.jQuery);