voluntary 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +48 -0
- data/Rakefile +27 -0
- data/app/assets/javascripts/voluntary/application.js +6 -0
- data/app/assets/javascripts/voluntary/base.js.coffee +34 -0
- data/app/assets/javascripts/voluntary/bootstrap.js.coffee +4 -0
- data/app/assets/javascripts/voluntary/functions.js +24 -0
- data/app/assets/javascripts/voluntary/users.js.coffee +12 -0
- data/app/assets/stylesheets/voluntary/application.css +15 -0
- data/app/assets/stylesheets/voluntary/base.css.scss +40 -0
- data/app/assets/stylesheets/voluntary/bootstrap_and_overrides.css.less +67 -0
- data/app/controllers/areas_controller.rb +66 -0
- data/app/controllers/candidatures_controller.rb +79 -0
- data/app/controllers/comments_controller.rb +70 -0
- data/app/controllers/devise_extensions/registrations_controller.rb +37 -0
- data/app/controllers/home_controller.rb +7 -0
- data/app/controllers/organizations_controller.rb +67 -0
- data/app/controllers/pages_controller.rb +54 -0
- data/app/controllers/products_controller.rb +62 -0
- data/app/controllers/professions_controller.rb +66 -0
- data/app/controllers/projects_controller.rb +56 -0
- data/app/controllers/results_controller.rb +93 -0
- data/app/controllers/stories_controller.rb +96 -0
- data/app/controllers/tasks_controller.rb +88 -0
- data/app/controllers/users_controller.rb +52 -0
- data/app/controllers/vacancies_controller.rb +79 -0
- data/app/controllers/voluntary/application_controller.rb +92 -0
- data/app/controllers/workflow/candidatures_controller.rb +20 -0
- data/app/controllers/workflow/products_controller.rb +6 -0
- data/app/controllers/workflow/project_owner_controller.rb +33 -0
- data/app/controllers/workflow/stories_controller.rb +6 -0
- data/app/controllers/workflow/tasks_controller.rb +147 -0
- data/app/controllers/workflow/user_controller.rb +6 -0
- data/app/controllers/workflow/vacancies_controller.rb +17 -0
- data/app/controllers/workflow_controller.rb +7 -0
- data/app/helpers/application_helper.rb +65 -0
- data/app/helpers/collection_helper.rb +31 -0
- data/app/helpers/comments_helper.rb +11 -0
- data/app/helpers/form_helper.rb +39 -0
- data/app/helpers/language_helper.rb +15 -0
- data/app/helpers/layout_helper.rb +24 -0
- data/app/helpers/product_helper.rb +32 -0
- data/app/helpers/show_helper.rb +65 -0
- data/app/helpers/wizard_helper.rb +27 -0
- data/app/models/area.rb +19 -0
- data/app/models/area_user.rb +19 -0
- data/app/models/candidature.rb +35 -0
- data/app/models/comment.rb +16 -0
- data/app/models/history_tracker.rb +3 -0
- data/app/models/mongo_db_document.rb +11 -0
- data/app/models/organization.rb +19 -0
- data/app/models/page.rb +30 -0
- data/app/models/product.rb +99 -0
- data/app/models/product/new_product.rb +3 -0
- data/app/models/product/project_management.rb +29 -0
- data/app/models/profession.rb +13 -0
- data/app/models/project.rb +66 -0
- data/app/models/project_user.rb +13 -0
- data/app/models/result.rb +55 -0
- data/app/models/role.rb +8 -0
- data/app/models/role/admin.rb +2 -0
- data/app/models/role/master.rb +2 -0
- data/app/models/role/project_owner.rb +2 -0
- data/app/models/role/user.rb +2 -0
- data/app/models/state_machines/candidature.rb +42 -0
- data/app/models/state_machines/page.rb +18 -0
- data/app/models/state_machines/story.rb +51 -0
- data/app/models/state_machines/task.rb +46 -0
- data/app/models/state_machines/vacancy.rb +41 -0
- data/app/models/story.rb +82 -0
- data/app/models/task.rb +85 -0
- data/app/models/user.rb +95 -0
- data/app/models/user_role.rb +6 -0
- data/app/models/vacancy.rb +41 -0
- data/app/observers/candidature_observer.rb +29 -0
- data/app/observers/story_observer.rb +6 -0
- data/app/observers/task_observer.rb +34 -0
- data/app/presenters/layout/application_presenter.rb +3 -0
- data/app/presenters/layout_presenter.rb +9 -0
- data/app/presenters/presenter.rb +46 -0
- data/app/presenters/resource_presenter.rb +30 -0
- data/app/presenters/resources/general/wizard_presenter.rb +17 -0
- data/app/presenters/resources/general/wizards/story/steps/activate_presenter.rb +3 -0
- data/app/presenters/resources/general/wizards/story/steps/setup_tasks_presenter.rb +5 -0
- data/app/presenters/resources/general/wizards/story_presenter.rb +3 -0
- data/app/presenters/resources/user/form_presenter.rb +16 -0
- data/app/presenters/shared/collection/table_presenter.rb +33 -0
- data/app/views/areas/_form.html.erb +11 -0
- data/app/views/areas/edit.html.erb +3 -0
- data/app/views/areas/index.html.erb +5 -0
- data/app/views/areas/new.html.erb +3 -0
- data/app/views/areas/show.html.erb +6 -0
- data/app/views/candidatures/_form.html.erb +13 -0
- data/app/views/candidatures/edit.html.erb +3 -0
- data/app/views/candidatures/index.html.erb +11 -0
- data/app/views/candidatures/new.html.erb +3 -0
- data/app/views/candidatures/show.html.erb +18 -0
- data/app/views/comments/_form.html.erb +16 -0
- data/app/views/comments/_resource.html.erb +16 -0
- data/app/views/comments/edit.html.erb +3 -0
- data/app/views/comments/new.html.erb +3 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +10 -0
- data/app/views/devise/registrations/new.html.erb +5 -0
- data/app/views/general/wizard.html.erb +16 -0
- data/app/views/kaminari/_first_page.html.erb +13 -0
- data/app/views/kaminari/_gap.html.erb +8 -0
- data/app/views/kaminari/_last_page.html.erb +13 -0
- data/app/views/kaminari/_next_page.html.erb +13 -0
- data/app/views/kaminari/_page.html.erb +12 -0
- data/app/views/kaminari/_paginator.html.erb +25 -0
- data/app/views/kaminari/_prev_page.html.erb +13 -0
- data/app/views/layouts/application.html.erb +48 -0
- data/app/views/layouts/shared/_flash_messages.html.erb +7 -0
- data/app/views/layouts/shared/_navigation.html.erb +7 -0
- data/app/views/organizations/_form.html.erb +11 -0
- data/app/views/organizations/edit.html.erb +3 -0
- data/app/views/organizations/index.html.erb +5 -0
- data/app/views/organizations/new.html.erb +3 -0
- data/app/views/organizations/show.html.erb +6 -0
- data/app/views/pages/_form.html.erb +12 -0
- data/app/views/pages/edit.html.erb +3 -0
- data/app/views/pages/index.html.erb +5 -0
- data/app/views/pages/new.html.erb +3 -0
- data/app/views/pages/show.html.erb +10 -0
- data/app/views/products/_form.html.erb +28 -0
- data/app/views/products/edit.html.erb +3 -0
- data/app/views/products/index.html.erb +5 -0
- data/app/views/products/mixins/_keywords.html.erb +4 -0
- data/app/views/products/new.html.erb +3 -0
- data/app/views/products/show.html.erb +11 -0
- data/app/views/products/types/text_creation/stories/_form.html.erb +19 -0
- data/app/views/products/types/text_creation/stories/_task_fields.html.erb +7 -0
- data/app/views/projects/_form.html.erb +16 -0
- data/app/views/projects/edit.html.erb +3 -0
- data/app/views/projects/index.html.erb +5 -0
- data/app/views/projects/new.html.erb +3 -0
- data/app/views/projects/show.html.erb +13 -0
- data/app/views/shared/_comments.html.erb +7 -0
- data/app/views/shared/collection/_list.html.erb +37 -0
- data/app/views/shared/collection/_table.html.erb +36 -0
- data/app/views/shared/form/_error_messages.html.erb +10 -0
- data/app/views/shared/resource/_actions.html.erb +26 -0
- data/app/views/shared/resource/_event_elements.html.erb +7 -0
- data/app/views/stories/_collection.html.erb +8 -0
- data/app/views/stories/_form.html.erb +19 -0
- data/app/views/stories/_task_fields.html.erb +11 -0
- data/app/views/stories/index.html.erb +1 -0
- data/app/views/stories/show.html.erb +13 -0
- data/app/views/stories/steps/_activate.html.erb +17 -0
- data/app/views/stories/steps/_setup_tasks.html.erb +35 -0
- data/app/views/stories/tasks.html.erb +0 -0
- data/app/views/tasks/_collection.html.erb +12 -0
- data/app/views/users/_form.html.erb +43 -0
- data/app/views/users/edit.html.erb +3 -0
- data/app/views/users/index.html.erb +6 -0
- data/app/views/users/new.html.erb +3 -0
- data/app/views/users/preferences.html.erb +11 -0
- data/app/views/users/show.html.erb +6 -0
- data/app/views/vacancies/_form.html.erb +15 -0
- data/app/views/vacancies/edit.html.erb +3 -0
- data/app/views/vacancies/index.html.erb +7 -0
- data/app/views/vacancies/new.html.erb +3 -0
- data/app/views/vacancies/show.html.erb +17 -0
- data/app/views/workflow/index.html.erb +0 -0
- data/app/views/workflow/products/show.html.erb +34 -0
- data/app/views/workflow/project_owner/_candidatures.html.erb +31 -0
- data/app/views/workflow/project_owner/_stories.html.erb +29 -0
- data/app/views/workflow/project_owner/_tasks.html.erb +34 -0
- data/app/views/workflow/project_owner/_vacancies.html.erb +27 -0
- data/app/views/workflow/project_owner/index.html.erb +24 -0
- data/app/views/workflow/tasks/_work_form.html.erb +8 -0
- data/app/views/workflow/tasks/_work_head.html.erb +15 -0
- data/app/views/workflow/tasks/edit.html.erb +3 -0
- data/app/views/workflow/tasks/index.html.erb +31 -0
- data/app/views/workflow/tasks/next.html.erb +0 -0
- data/app/views/workflow/tasks/steps/_complete.html.erb +11 -0
- data/app/views/workflow/tasks/steps/_review.html.erb +13 -0
- data/app/views/workflow/tasks/steps/_work.html.erb +17 -0
- data/app/views/workflow/user/_tasks.html.erb +30 -0
- data/app/views/workflow/user/index.html.erb +14 -0
- data/config/initializers/1_initialize_app_config.rb +5 -0
- data/config/initializers/devise.rb +193 -0
- data/config/initializers/locale_settings.rb +42 -0
- data/config/initializers/mongoid-history.rb +2 -0
- data/config/initializers/recaptcha.rb +7 -0
- data/config/initializers/simple_form.rb +178 -0
- data/config/initializers/simple_navigation.rb +2 -0
- data/config/locales/devise.en.yml +59 -0
- data/config/locales/en.yml +26 -0
- data/config/locales/general/en.yml +71 -0
- data/config/locales/resources/area/en.yml +12 -0
- data/config/locales/resources/candidature/en.yml +19 -0
- data/config/locales/resources/comment/en.yml +11 -0
- data/config/locales/resources/organization/en.yml +11 -0
- data/config/locales/resources/page/en.yml +11 -0
- data/config/locales/resources/product/en.yml +23 -0
- data/config/locales/resources/project/en.yml +9 -0
- data/config/locales/resources/result/en.yml +9 -0
- data/config/locales/resources/story/en.yml +34 -0
- data/config/locales/resources/task/en.yml +36 -0
- data/config/locales/resources/user/en.yml +14 -0
- data/config/locales/resources/vacancy/en.yml +29 -0
- data/config/locales/simple_form.en.yml +26 -0
- data/config/locales/workflow/en.yml +37 -0
- data/config/routes.rb +188 -0
- data/db/migrate/20120907144853_create_schema.rb +217 -0
- data/db/migrate/20120911093743_remove_timestamps_from_habtm_tables.rb +21 -0
- data/db/migrate/20120919161831_add_unique_index_for_one_user_candidature_per_vacancy.rb +9 -0
- data/db/migrate/20120922201955_create_mongo_db_documents.rb +13 -0
- data/db/migrate/20120923140109_add_product_to_project.rb +6 -0
- data/db/migrate/20121004061413_add_extra_user_attributes.rb +9 -0
- data/db/migrate/20121004132105_create_professions.rb +13 -0
- data/db/migrate/20121006162913_add_public_attribute_to_roles.rb +6 -0
- data/db/migrate/20121006170407_add_type_attribute_to_roles.rb +5 -0
- data/db/migrate/20121007071543_add_foreign_languages_to_user.rb +6 -0
- data/db/migrate/20121028073712_create_organizations.rb +13 -0
- data/db/seeds.rb +14 -0
- data/lib/applicat/mvc/controller.rb +171 -0
- data/lib/applicat/mvc/controller/error_handling.rb +40 -0
- data/lib/applicat/mvc/controller/resource.rb +19 -0
- data/lib/applicat/mvc/controller/transition_actions.rb +43 -0
- data/lib/applicat/mvc/model/resource/base.rb +56 -0
- data/lib/applicat/mvc/model/tokenable.rb +24 -0
- data/lib/applicat/mvc/model/tree.rb +82 -0
- data/lib/db_seed.rb +150 -0
- data/lib/environment_configuration.rb +54 -0
- data/lib/generators/voluntary/install/install_generator.rb +118 -0
- data/lib/generators/voluntary/install/templates/app/controllers/application_controller.rb +3 -0
- data/lib/generators/voluntary/install/templates/app/models/ability.rb +49 -0
- data/lib/generators/voluntary/install/templates/app/models/app_config.rb +32 -0
- data/lib/generators/voluntary/install/templates/app/views/layouts/application.html.erb +50 -0
- data/lib/generators/voluntary/install/templates/config/application.yml +147 -0
- data/lib/generators/voluntary/install/templates/config/cucumber.yml +8 -0
- data/lib/generators/voluntary/install/templates/config/database.example.yml +28 -0
- data/lib/generators/voluntary/install/templates/config/deploy.rb +59 -0
- data/lib/generators/voluntary/install/templates/config/email.example.yml +9 -0
- data/lib/generators/voluntary/install/templates/config/initializers/recaptcha_example +4 -0
- data/lib/generators/voluntary/install/templates/config/locale_settings.yml +121 -0
- data/lib/generators/voluntary/install/templates/config/main_navigation.rb +262 -0
- data/lib/generators/voluntary/install/templates/config/mongoid.yml +78 -0
- data/lib/generators/voluntary/install/templates/config/nginx.conf +27 -0
- data/lib/generators/voluntary/install/templates/config/unicorn.rb +9 -0
- data/lib/generators/voluntary/install/templates/config/unicorn_init.sh +84 -0
- data/lib/generators/voluntary/install/templates/db/seeds.rb +14 -0
- data/lib/generators/voluntary/install/templates/features/step_definitions/email_steps.rb +89 -0
- data/lib/generators/voluntary/install/templates/features/step_definitions/factory_steps.rb +120 -0
- data/lib/generators/voluntary/install/templates/features/step_definitions/javascript_steps.rb +15 -0
- data/lib/generators/voluntary/install/templates/features/step_definitions/navigation_steps.rb +3 -0
- data/lib/generators/voluntary/install/templates/features/step_definitions/resources_steps.rb +8 -0
- data/lib/generators/voluntary/install/templates/features/step_definitions/session_steps.rb +35 -0
- data/lib/generators/voluntary/install/templates/features/step_definitions/web_steps.rb +271 -0
- data/lib/generators/voluntary/install/templates/features/support/database_cleaner_patches.rb +24 -0
- data/lib/generators/voluntary/install/templates/features/support/env.rb +83 -0
- data/lib/generators/voluntary/install/templates/features/support/integration_sessions_controller.rb +32 -0
- data/lib/generators/voluntary/install/templates/features/support/integration_sessions_form.html.erb +1 -0
- data/lib/generators/voluntary/install/templates/features/support/paths.rb +98 -0
- data/lib/generators/voluntary/install/templates/features/support/selectors.rb +44 -0
- data/lib/generators/voluntary/install/templates/features/support/spork_env +72 -0
- data/lib/generators/voluntary/install/templates/features/support/user_cuke_helpers.rb +78 -0
- data/lib/generators/voluntary/install/templates/lib/volontariat_seed.rb +36 -0
- data/lib/generators/voluntary/install/templates/spec/spec_helper.rb +72 -0
- data/lib/generators/voluntary/install/templates/spec/support/deferred_garbage_collector.rb +45 -0
- data/lib/generators/voluntary/install/templates/spec/support/devise.rb +3 -0
- data/lib/generators/voluntary/install/templates/spec/support/mongo_database_cleaner.rb +19 -0
- data/lib/generators/voluntary/install/templates/vendor/assets/javascripts/jquery.tokeninput.js +915 -0
- data/lib/generators/voluntary/install/templates/vendor/assets/stylesheets/token-input-facebook.css +122 -0
- data/lib/generators/voluntary/install/templates/vendor/assets/stylesheets/token-input-mac.css +204 -0
- data/lib/generators/voluntary/install/templates/vendor/assets/stylesheets/token-input.css +127 -0
- data/lib/i18n_interpolation_fallbacks.rb +21 -0
- data/lib/model/mongo_db/commentable.rb +17 -0
- data/lib/model/mongo_db/core_extensions.rb +10 -0
- data/lib/model/mongo_db/customizable.rb +23 -0
- data/lib/model/mongo_db/product/keywords.rb +31 -0
- data/lib/model/mongo_db/state_version_attributes.rb +15 -0
- data/lib/tasks/cucumber.rake +65 -0
- data/lib/tasks/voluntary_tasks.rake +4 -0
- data/lib/vendors/active_model/naming.rb +14 -0
- data/lib/vendors/simple_navigation/renderer/breadcrumbs_without_method_links.rb +30 -0
- data/lib/vendors/simple_navigation/renderer/twitter_sidenav.rb +34 -0
- data/lib/voluntary.rb +87 -0
- data/lib/voluntary/engine.rb +11 -0
- data/lib/voluntary/helpers/application.rb +69 -0
- data/lib/voluntary/helpers/collection.rb +35 -0
- data/lib/voluntary/helpers/comments.rb +15 -0
- data/lib/voluntary/helpers/form.rb +43 -0
- data/lib/voluntary/helpers/language.rb +19 -0
- data/lib/voluntary/helpers/layout.rb +28 -0
- data/lib/voluntary/helpers/product.rb +36 -0
- data/lib/voluntary/helpers/show.rb +69 -0
- data/lib/voluntary/helpers/wizard.rb +31 -0
- data/lib/voluntary/version.rb +3 -0
- data/lib/wizard.rb +58 -0
- data/lib/wizard/controller/concerns/paths.rb +31 -0
- data/lib/wizard/controller/concerns/steps.rb +142 -0
- metadata +1217 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
class MongoDatabaseCleaner
|
2
|
+
def self.clean
|
3
|
+
Dir["#{Rails.root}/app/models/**/*.*"].each do |path_name|
|
4
|
+
path_name.gsub!("#{Rails.root.to_s}/app/models/", '')
|
5
|
+
path_name = path_name.split('/')
|
6
|
+
klass = path_name.pop.sub(/\.rb$/,'').camelize
|
7
|
+
|
8
|
+
unless path_name.none?
|
9
|
+
klass = [path_name.map(&:camelize).join('::'), klass].join('::')
|
10
|
+
end
|
11
|
+
|
12
|
+
klass = klass.constantize
|
13
|
+
|
14
|
+
next if klass.respond_to?(:table_name) || !klass.respond_to?(:delete_all)
|
15
|
+
|
16
|
+
klass.delete_all
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/generators/voluntary/install/templates/vendor/assets/javascripts/jquery.tokeninput.js
ADDED
@@ -0,0 +1,915 @@
|
|
1
|
+
/*
|
2
|
+
* jQuery Plugin: Tokenizing Autocomplete Text Entry
|
3
|
+
* Version 1.6.0
|
4
|
+
*
|
5
|
+
* Copyright (c) 2009 James Smith (http://loopj.com)
|
6
|
+
* Licensed jointly under the GPL and MIT licenses,
|
7
|
+
* choose which one suits your project best!
|
8
|
+
*
|
9
|
+
*/
|
10
|
+
|
11
|
+
(function ($) {
|
12
|
+
// Default settings
|
13
|
+
var DEFAULT_SETTINGS = {
|
14
|
+
// Search settings
|
15
|
+
method: "GET",
|
16
|
+
queryParam: "q",
|
17
|
+
searchDelay: 300,
|
18
|
+
minChars: 1,
|
19
|
+
propertyToSearch: "name",
|
20
|
+
jsonContainer: null,
|
21
|
+
contentType: "json",
|
22
|
+
|
23
|
+
// Prepopulation settings
|
24
|
+
prePopulate: null,
|
25
|
+
processPrePopulate: false,
|
26
|
+
|
27
|
+
// Display settings
|
28
|
+
hintText: "Type in a search term",
|
29
|
+
noResultsText: "No results",
|
30
|
+
searchingText: "Searching...",
|
31
|
+
deleteText: "×",
|
32
|
+
animateDropdown: true,
|
33
|
+
theme: null,
|
34
|
+
zindex: 999,
|
35
|
+
resultsFormatter: function(item){ return "<li>" + item[this.propertyToSearch]+ "</li>" },
|
36
|
+
tokenFormatter: function(item) { return "<li><p>" + item[this.propertyToSearch] + "</p></li>" },
|
37
|
+
|
38
|
+
// Tokenization settings
|
39
|
+
tokenLimit: null,
|
40
|
+
tokenDelimiter: ",",
|
41
|
+
preventDuplicates: false,
|
42
|
+
tokenValue: "id",
|
43
|
+
|
44
|
+
// Callbacks
|
45
|
+
onResult: null,
|
46
|
+
onAdd: null,
|
47
|
+
onDelete: null,
|
48
|
+
onReady: null,
|
49
|
+
|
50
|
+
// Other settings
|
51
|
+
idPrefix: "token-input-",
|
52
|
+
|
53
|
+
// Keep track if the input is currently in disabled mode
|
54
|
+
disabled: false
|
55
|
+
};
|
56
|
+
|
57
|
+
// Default classes to use when theming
|
58
|
+
var DEFAULT_CLASSES = {
|
59
|
+
tokenList: "token-input-list",
|
60
|
+
token: "token-input-token",
|
61
|
+
tokenDelete: "token-input-delete-token",
|
62
|
+
selectedToken: "token-input-selected-token",
|
63
|
+
highlightedToken: "token-input-highlighted-token",
|
64
|
+
dropdown: "token-input-dropdown",
|
65
|
+
dropdownItem: "token-input-dropdown-item",
|
66
|
+
dropdownItem2: "token-input-dropdown-item2",
|
67
|
+
selectedDropdownItem: "token-input-selected-dropdown-item",
|
68
|
+
inputToken: "token-input-input-token",
|
69
|
+
focused: "token-input-focused",
|
70
|
+
disabled: "token-input-disabled"
|
71
|
+
};
|
72
|
+
|
73
|
+
// Input box position "enum"
|
74
|
+
var POSITION = {
|
75
|
+
BEFORE: 0,
|
76
|
+
AFTER: 1,
|
77
|
+
END: 2
|
78
|
+
};
|
79
|
+
|
80
|
+
// Keys "enum"
|
81
|
+
var KEY = {
|
82
|
+
BACKSPACE: 8,
|
83
|
+
TAB: 9,
|
84
|
+
ENTER: 13,
|
85
|
+
ESCAPE: 27,
|
86
|
+
SPACE: 32,
|
87
|
+
PAGE_UP: 33,
|
88
|
+
PAGE_DOWN: 34,
|
89
|
+
END: 35,
|
90
|
+
HOME: 36,
|
91
|
+
LEFT: 37,
|
92
|
+
UP: 38,
|
93
|
+
RIGHT: 39,
|
94
|
+
DOWN: 40,
|
95
|
+
NUMPAD_ENTER: 108,
|
96
|
+
COMMA: 188
|
97
|
+
};
|
98
|
+
|
99
|
+
// Additional public (exposed) methods
|
100
|
+
var methods = {
|
101
|
+
init: function(url_or_data_or_function, options) {
|
102
|
+
var settings = $.extend({}, DEFAULT_SETTINGS, options || {});
|
103
|
+
|
104
|
+
return this.each(function () {
|
105
|
+
$(this).data("tokenInputObject", new $.TokenList(this, url_or_data_or_function, settings));
|
106
|
+
});
|
107
|
+
},
|
108
|
+
clear: function() {
|
109
|
+
this.data("tokenInputObject").clear();
|
110
|
+
return this;
|
111
|
+
},
|
112
|
+
add: function(item) {
|
113
|
+
this.data("tokenInputObject").add(item);
|
114
|
+
return this;
|
115
|
+
},
|
116
|
+
remove: function(item) {
|
117
|
+
this.data("tokenInputObject").remove(item);
|
118
|
+
return this;
|
119
|
+
},
|
120
|
+
get: function() {
|
121
|
+
return this.data("tokenInputObject").getTokens();
|
122
|
+
},
|
123
|
+
toggleDisabled: function(disable) {
|
124
|
+
this.data("tokenInputObject").toggleDisabled(disable);
|
125
|
+
return this;
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
// Expose the .tokenInput function to jQuery as a plugin
|
130
|
+
$.fn.tokenInput = function (method) {
|
131
|
+
// Method calling and initialization logic
|
132
|
+
if(methods[method]) {
|
133
|
+
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
|
134
|
+
} else {
|
135
|
+
return methods.init.apply(this, arguments);
|
136
|
+
}
|
137
|
+
};
|
138
|
+
|
139
|
+
// TokenList class for each input
|
140
|
+
$.TokenList = function (input, url_or_data, settings) {
|
141
|
+
//
|
142
|
+
// Initialization
|
143
|
+
//
|
144
|
+
|
145
|
+
// Configure the data source
|
146
|
+
if($.type(url_or_data) === "string" || $.type(url_or_data) === "function") {
|
147
|
+
// Set the url to query against
|
148
|
+
settings.url = url_or_data;
|
149
|
+
|
150
|
+
// If the URL is a function, evaluate it here to do our initalization work
|
151
|
+
var url = computeURL();
|
152
|
+
|
153
|
+
// Make a smart guess about cross-domain if it wasn't explicitly specified
|
154
|
+
if(settings.crossDomain === undefined && typeof url === "string") {
|
155
|
+
if(url.indexOf("://") === -1) {
|
156
|
+
settings.crossDomain = false;
|
157
|
+
} else {
|
158
|
+
settings.crossDomain = (location.href.split(/\/+/g)[1] !== url.split(/\/+/g)[1]);
|
159
|
+
}
|
160
|
+
}
|
161
|
+
} else if(typeof(url_or_data) === "object") {
|
162
|
+
// Set the local data to search through
|
163
|
+
settings.local_data = url_or_data;
|
164
|
+
}
|
165
|
+
|
166
|
+
// Build class names
|
167
|
+
if(settings.classes) {
|
168
|
+
// Use custom class names
|
169
|
+
settings.classes = $.extend({}, DEFAULT_CLASSES, settings.classes);
|
170
|
+
} else if(settings.theme) {
|
171
|
+
// Use theme-suffixed default class names
|
172
|
+
settings.classes = {};
|
173
|
+
$.each(DEFAULT_CLASSES, function(key, value) {
|
174
|
+
settings.classes[key] = value + "-" + settings.theme;
|
175
|
+
});
|
176
|
+
} else {
|
177
|
+
settings.classes = DEFAULT_CLASSES;
|
178
|
+
}
|
179
|
+
|
180
|
+
|
181
|
+
// Save the tokens
|
182
|
+
var saved_tokens = [];
|
183
|
+
|
184
|
+
// Keep track of the number of tokens in the list
|
185
|
+
var token_count = 0;
|
186
|
+
|
187
|
+
// Basic cache to save on db hits
|
188
|
+
var cache = new $.TokenList.Cache();
|
189
|
+
|
190
|
+
// Keep track of the timeout, old vals
|
191
|
+
var timeout;
|
192
|
+
var input_val;
|
193
|
+
|
194
|
+
// Create a new text input an attach keyup events
|
195
|
+
var input_box = $("<input type=\"text\" autocomplete=\"off\">")
|
196
|
+
.css({
|
197
|
+
outline: "none"
|
198
|
+
})
|
199
|
+
.attr("id", settings.idPrefix + input.id)
|
200
|
+
.focus(function () {
|
201
|
+
if (settings.disabled) {
|
202
|
+
return false;
|
203
|
+
} else
|
204
|
+
if (settings.tokenLimit === null || settings.tokenLimit !== token_count) {
|
205
|
+
show_dropdown_hint();
|
206
|
+
}
|
207
|
+
token_list.addClass(settings.classes.focused);
|
208
|
+
})
|
209
|
+
.blur(function () {
|
210
|
+
hide_dropdown();
|
211
|
+
$(this).val("");
|
212
|
+
token_list.removeClass(settings.classes.focused);
|
213
|
+
})
|
214
|
+
.bind("keyup keydown blur update", resize_input)
|
215
|
+
.keydown(function (event) {
|
216
|
+
var previous_token;
|
217
|
+
var next_token;
|
218
|
+
|
219
|
+
switch(event.keyCode) {
|
220
|
+
case KEY.LEFT:
|
221
|
+
case KEY.RIGHT:
|
222
|
+
case KEY.UP:
|
223
|
+
case KEY.DOWN:
|
224
|
+
if(!$(this).val()) {
|
225
|
+
previous_token = input_token.prev();
|
226
|
+
next_token = input_token.next();
|
227
|
+
|
228
|
+
if((previous_token.length && previous_token.get(0) === selected_token) || (next_token.length && next_token.get(0) === selected_token)) {
|
229
|
+
// Check if there is a previous/next token and it is selected
|
230
|
+
if(event.keyCode === KEY.LEFT || event.keyCode === KEY.UP) {
|
231
|
+
deselect_token($(selected_token), POSITION.BEFORE);
|
232
|
+
} else {
|
233
|
+
deselect_token($(selected_token), POSITION.AFTER);
|
234
|
+
}
|
235
|
+
} else if((event.keyCode === KEY.LEFT || event.keyCode === KEY.UP) && previous_token.length) {
|
236
|
+
// We are moving left, select the previous token if it exists
|
237
|
+
select_token($(previous_token.get(0)));
|
238
|
+
} else if((event.keyCode === KEY.RIGHT || event.keyCode === KEY.DOWN) && next_token.length) {
|
239
|
+
// We are moving right, select the next token if it exists
|
240
|
+
select_token($(next_token.get(0)));
|
241
|
+
}
|
242
|
+
} else {
|
243
|
+
var dropdown_item = null;
|
244
|
+
|
245
|
+
if(event.keyCode === KEY.DOWN || event.keyCode === KEY.RIGHT) {
|
246
|
+
dropdown_item = $(selected_dropdown_item).next();
|
247
|
+
} else {
|
248
|
+
dropdown_item = $(selected_dropdown_item).prev();
|
249
|
+
}
|
250
|
+
|
251
|
+
if(dropdown_item.length) {
|
252
|
+
select_dropdown_item(dropdown_item);
|
253
|
+
}
|
254
|
+
}
|
255
|
+
return false;
|
256
|
+
break;
|
257
|
+
|
258
|
+
case KEY.BACKSPACE:
|
259
|
+
previous_token = input_token.prev();
|
260
|
+
|
261
|
+
if(!$(this).val().length) {
|
262
|
+
if(selected_token) {
|
263
|
+
delete_token($(selected_token));
|
264
|
+
hidden_input.change();
|
265
|
+
} else if(previous_token.length) {
|
266
|
+
select_token($(previous_token.get(0)));
|
267
|
+
}
|
268
|
+
|
269
|
+
return false;
|
270
|
+
} else if($(this).val().length === 1) {
|
271
|
+
hide_dropdown();
|
272
|
+
} else {
|
273
|
+
// set a timeout just long enough to let this function finish.
|
274
|
+
setTimeout(function(){do_search();}, 5);
|
275
|
+
}
|
276
|
+
break;
|
277
|
+
|
278
|
+
case KEY.TAB:
|
279
|
+
case KEY.ENTER:
|
280
|
+
case KEY.NUMPAD_ENTER:
|
281
|
+
case KEY.COMMA:
|
282
|
+
if(selected_dropdown_item) {
|
283
|
+
add_token($(selected_dropdown_item).data("tokeninput"));
|
284
|
+
hidden_input.change();
|
285
|
+
return false;
|
286
|
+
}
|
287
|
+
break;
|
288
|
+
|
289
|
+
case KEY.ESCAPE:
|
290
|
+
hide_dropdown();
|
291
|
+
return true;
|
292
|
+
|
293
|
+
default:
|
294
|
+
if(String.fromCharCode(event.which)) {
|
295
|
+
// set a timeout just long enough to let this function finish.
|
296
|
+
setTimeout(function(){do_search();}, 5);
|
297
|
+
}
|
298
|
+
break;
|
299
|
+
}
|
300
|
+
});
|
301
|
+
|
302
|
+
// Keep a reference to the original input box
|
303
|
+
var hidden_input = $(input)
|
304
|
+
.hide()
|
305
|
+
.val("")
|
306
|
+
.focus(function () {
|
307
|
+
focus_with_timeout(input_box);
|
308
|
+
})
|
309
|
+
.blur(function () {
|
310
|
+
input_box.blur();
|
311
|
+
});
|
312
|
+
|
313
|
+
// Keep a reference to the selected token and dropdown item
|
314
|
+
var selected_token = null;
|
315
|
+
var selected_token_index = 0;
|
316
|
+
var selected_dropdown_item = null;
|
317
|
+
|
318
|
+
// The list to store the token items in
|
319
|
+
var token_list = $("<ul />")
|
320
|
+
.addClass(settings.classes.tokenList)
|
321
|
+
.click(function (event) {
|
322
|
+
var li = $(event.target).closest("li");
|
323
|
+
if(li && li.get(0) && $.data(li.get(0), "tokeninput")) {
|
324
|
+
toggle_select_token(li);
|
325
|
+
} else {
|
326
|
+
// Deselect selected token
|
327
|
+
if(selected_token) {
|
328
|
+
deselect_token($(selected_token), POSITION.END);
|
329
|
+
}
|
330
|
+
|
331
|
+
// Focus input box
|
332
|
+
focus_with_timeout(input_box);
|
333
|
+
}
|
334
|
+
})
|
335
|
+
.mouseover(function (event) {
|
336
|
+
var li = $(event.target).closest("li");
|
337
|
+
if(li && selected_token !== this) {
|
338
|
+
li.addClass(settings.classes.highlightedToken);
|
339
|
+
}
|
340
|
+
})
|
341
|
+
.mouseout(function (event) {
|
342
|
+
var li = $(event.target).closest("li");
|
343
|
+
if(li && selected_token !== this) {
|
344
|
+
li.removeClass(settings.classes.highlightedToken);
|
345
|
+
}
|
346
|
+
})
|
347
|
+
.insertBefore(hidden_input);
|
348
|
+
|
349
|
+
// The token holding the input box
|
350
|
+
var input_token = $("<li />")
|
351
|
+
.addClass(settings.classes.inputToken)
|
352
|
+
.appendTo(token_list)
|
353
|
+
.append(input_box);
|
354
|
+
|
355
|
+
// The list to store the dropdown items in
|
356
|
+
var dropdown = $("<div>")
|
357
|
+
.addClass(settings.classes.dropdown)
|
358
|
+
.appendTo("body")
|
359
|
+
.hide();
|
360
|
+
|
361
|
+
// Magic element to help us resize the text input
|
362
|
+
var input_resizer = $("<tester/>")
|
363
|
+
.insertAfter(input_box)
|
364
|
+
.css({
|
365
|
+
position: "absolute",
|
366
|
+
top: -9999,
|
367
|
+
left: -9999,
|
368
|
+
width: "auto",
|
369
|
+
fontSize: input_box.css("fontSize"),
|
370
|
+
fontFamily: input_box.css("fontFamily"),
|
371
|
+
fontWeight: input_box.css("fontWeight"),
|
372
|
+
letterSpacing: input_box.css("letterSpacing"),
|
373
|
+
whiteSpace: "nowrap"
|
374
|
+
});
|
375
|
+
|
376
|
+
// Pre-populate list if items exist
|
377
|
+
hidden_input.val("");
|
378
|
+
var li_data = settings.prePopulate || hidden_input.data("pre");
|
379
|
+
if(settings.processPrePopulate && $.isFunction(settings.onResult)) {
|
380
|
+
li_data = settings.onResult.call(hidden_input, li_data);
|
381
|
+
}
|
382
|
+
if(li_data && li_data.length) {
|
383
|
+
$.each(li_data, function (index, value) {
|
384
|
+
insert_token(value);
|
385
|
+
checkTokenLimit();
|
386
|
+
});
|
387
|
+
}
|
388
|
+
|
389
|
+
// Check if widget should initialize as disabled
|
390
|
+
if (settings.disabled) {
|
391
|
+
toggleDisabled(true);
|
392
|
+
}
|
393
|
+
|
394
|
+
// Initialization is done
|
395
|
+
if($.isFunction(settings.onReady)) {
|
396
|
+
settings.onReady.call();
|
397
|
+
}
|
398
|
+
|
399
|
+
//
|
400
|
+
// Public functions
|
401
|
+
//
|
402
|
+
|
403
|
+
this.clear = function() {
|
404
|
+
token_list.children("li").each(function() {
|
405
|
+
if ($(this).children("input").length === 0) {
|
406
|
+
delete_token($(this));
|
407
|
+
}
|
408
|
+
});
|
409
|
+
}
|
410
|
+
|
411
|
+
this.add = function(item) {
|
412
|
+
add_token(item);
|
413
|
+
}
|
414
|
+
|
415
|
+
this.remove = function(item) {
|
416
|
+
token_list.children("li").each(function() {
|
417
|
+
if ($(this).children("input").length === 0) {
|
418
|
+
var currToken = $(this).data("tokeninput");
|
419
|
+
var match = true;
|
420
|
+
for (var prop in item) {
|
421
|
+
if (item[prop] !== currToken[prop]) {
|
422
|
+
match = false;
|
423
|
+
break;
|
424
|
+
}
|
425
|
+
}
|
426
|
+
if (match) {
|
427
|
+
delete_token($(this));
|
428
|
+
}
|
429
|
+
}
|
430
|
+
});
|
431
|
+
}
|
432
|
+
|
433
|
+
this.getTokens = function() {
|
434
|
+
return saved_tokens;
|
435
|
+
}
|
436
|
+
|
437
|
+
this.toggleDisabled = function(disable) {
|
438
|
+
toggleDisabled(disable);
|
439
|
+
}
|
440
|
+
|
441
|
+
//
|
442
|
+
// Private functions
|
443
|
+
//
|
444
|
+
|
445
|
+
// Toggles the widget between enabled and disabled state, or according
|
446
|
+
// to the [disable] parameter.
|
447
|
+
function toggleDisabled(disable) {
|
448
|
+
if (typeof disable === 'boolean') {
|
449
|
+
settings.disabled = disable
|
450
|
+
} else {
|
451
|
+
settings.disabled = !settings.disabled;
|
452
|
+
}
|
453
|
+
input_box.prop('disabled', settings.disabled);
|
454
|
+
token_list.toggleClass(settings.classes.disabled, settings.disabled);
|
455
|
+
// if there is any token selected we deselect it
|
456
|
+
if(selected_token) {
|
457
|
+
deselect_token($(selected_token), POSITION.END);
|
458
|
+
}
|
459
|
+
hidden_input.prop('disabled', settings.disabled);
|
460
|
+
}
|
461
|
+
|
462
|
+
function checkTokenLimit() {
|
463
|
+
if(settings.tokenLimit !== null && token_count >= settings.tokenLimit) {
|
464
|
+
input_box.hide();
|
465
|
+
hide_dropdown();
|
466
|
+
return;
|
467
|
+
}
|
468
|
+
}
|
469
|
+
|
470
|
+
function resize_input() {
|
471
|
+
if(input_val === (input_val = input_box.val())) {return;}
|
472
|
+
|
473
|
+
// Enter new content into resizer and resize input accordingly
|
474
|
+
var escaped = input_val.replace(/&/g, '&').replace(/\s/g,' ').replace(/</g, '<').replace(/>/g, '>');
|
475
|
+
input_resizer.html(escaped);
|
476
|
+
input_box.width(input_resizer.width() + 30);
|
477
|
+
}
|
478
|
+
|
479
|
+
function is_printable_character(keycode) {
|
480
|
+
return ((keycode >= 48 && keycode <= 90) || // 0-1a-z
|
481
|
+
(keycode >= 96 && keycode <= 111) || // numpad 0-9 + - / * .
|
482
|
+
(keycode >= 186 && keycode <= 192) || // ; = , - . / ^
|
483
|
+
(keycode >= 219 && keycode <= 222)); // ( \ ) '
|
484
|
+
}
|
485
|
+
|
486
|
+
// Inner function to a token to the list
|
487
|
+
function insert_token(item) {
|
488
|
+
var this_token = settings.tokenFormatter(item);
|
489
|
+
this_token = $(this_token)
|
490
|
+
.addClass(settings.classes.token)
|
491
|
+
.insertBefore(input_token);
|
492
|
+
|
493
|
+
// The 'delete token' button
|
494
|
+
$("<span>" + settings.deleteText + "</span>")
|
495
|
+
.addClass(settings.classes.tokenDelete)
|
496
|
+
.appendTo(this_token)
|
497
|
+
.click(function () {
|
498
|
+
if (!settings.disabled) {
|
499
|
+
delete_token($(this).parent());
|
500
|
+
hidden_input.change();
|
501
|
+
return false;
|
502
|
+
}
|
503
|
+
});
|
504
|
+
|
505
|
+
// Store data on the token
|
506
|
+
var token_data = item;
|
507
|
+
$.data(this_token.get(0), "tokeninput", item);
|
508
|
+
|
509
|
+
// Save this token for duplicate checking
|
510
|
+
saved_tokens = saved_tokens.slice(0,selected_token_index).concat([token_data]).concat(saved_tokens.slice(selected_token_index));
|
511
|
+
selected_token_index++;
|
512
|
+
|
513
|
+
// Update the hidden input
|
514
|
+
update_hidden_input(saved_tokens, hidden_input);
|
515
|
+
|
516
|
+
token_count += 1;
|
517
|
+
|
518
|
+
// Check the token limit
|
519
|
+
if(settings.tokenLimit !== null && token_count >= settings.tokenLimit) {
|
520
|
+
input_box.hide();
|
521
|
+
hide_dropdown();
|
522
|
+
}
|
523
|
+
|
524
|
+
return this_token;
|
525
|
+
}
|
526
|
+
|
527
|
+
// Add a token to the token list based on user input
|
528
|
+
function add_token (item) {
|
529
|
+
var callback = settings.onAdd;
|
530
|
+
|
531
|
+
// See if the token already exists and select it if we don't want duplicates
|
532
|
+
if(token_count > 0 && settings.preventDuplicates) {
|
533
|
+
var found_existing_token = null;
|
534
|
+
token_list.children().each(function () {
|
535
|
+
var existing_token = $(this);
|
536
|
+
var existing_data = $.data(existing_token.get(0), "tokeninput");
|
537
|
+
if(existing_data && existing_data.id === item.id) {
|
538
|
+
found_existing_token = existing_token;
|
539
|
+
return false;
|
540
|
+
}
|
541
|
+
});
|
542
|
+
|
543
|
+
if(found_existing_token) {
|
544
|
+
select_token(found_existing_token);
|
545
|
+
input_token.insertAfter(found_existing_token);
|
546
|
+
focus_with_timeout(input_box);
|
547
|
+
return;
|
548
|
+
}
|
549
|
+
}
|
550
|
+
|
551
|
+
// Insert the new tokens
|
552
|
+
if(settings.tokenLimit == null || token_count < settings.tokenLimit) {
|
553
|
+
insert_token(item);
|
554
|
+
checkTokenLimit();
|
555
|
+
}
|
556
|
+
|
557
|
+
// Clear input box
|
558
|
+
input_box.val("");
|
559
|
+
|
560
|
+
// Don't show the help dropdown, they've got the idea
|
561
|
+
hide_dropdown();
|
562
|
+
|
563
|
+
// Execute the onAdd callback if defined
|
564
|
+
if($.isFunction(callback)) {
|
565
|
+
callback.call(hidden_input,item);
|
566
|
+
}
|
567
|
+
}
|
568
|
+
|
569
|
+
// Select a token in the token list
|
570
|
+
function select_token (token) {
|
571
|
+
if (!settings.disabled) {
|
572
|
+
token.addClass(settings.classes.selectedToken);
|
573
|
+
selected_token = token.get(0);
|
574
|
+
|
575
|
+
// Hide input box
|
576
|
+
input_box.val("");
|
577
|
+
|
578
|
+
// Hide dropdown if it is visible (eg if we clicked to select token)
|
579
|
+
hide_dropdown();
|
580
|
+
}
|
581
|
+
}
|
582
|
+
|
583
|
+
// Deselect a token in the token list
|
584
|
+
function deselect_token (token, position) {
|
585
|
+
token.removeClass(settings.classes.selectedToken);
|
586
|
+
selected_token = null;
|
587
|
+
|
588
|
+
if(position === POSITION.BEFORE) {
|
589
|
+
input_token.insertBefore(token);
|
590
|
+
selected_token_index--;
|
591
|
+
} else if(position === POSITION.AFTER) {
|
592
|
+
input_token.insertAfter(token);
|
593
|
+
selected_token_index++;
|
594
|
+
} else {
|
595
|
+
input_token.appendTo(token_list);
|
596
|
+
selected_token_index = token_count;
|
597
|
+
}
|
598
|
+
|
599
|
+
// Show the input box and give it focus again
|
600
|
+
focus_with_timeout(input_box);
|
601
|
+
}
|
602
|
+
|
603
|
+
// Toggle selection of a token in the token list
|
604
|
+
function toggle_select_token(token) {
|
605
|
+
var previous_selected_token = selected_token;
|
606
|
+
|
607
|
+
if(selected_token) {
|
608
|
+
deselect_token($(selected_token), POSITION.END);
|
609
|
+
}
|
610
|
+
|
611
|
+
if(previous_selected_token === token.get(0)) {
|
612
|
+
deselect_token(token, POSITION.END);
|
613
|
+
} else {
|
614
|
+
select_token(token);
|
615
|
+
}
|
616
|
+
}
|
617
|
+
|
618
|
+
// Delete a token from the token list
|
619
|
+
function delete_token (token) {
|
620
|
+
// Remove the id from the saved list
|
621
|
+
var token_data = $.data(token.get(0), "tokeninput");
|
622
|
+
var callback = settings.onDelete;
|
623
|
+
|
624
|
+
var index = token.prevAll().length;
|
625
|
+
if(index > selected_token_index) index--;
|
626
|
+
|
627
|
+
// Delete the token
|
628
|
+
token.remove();
|
629
|
+
selected_token = null;
|
630
|
+
|
631
|
+
// Show the input box and give it focus again
|
632
|
+
focus_with_timeout(input_box);
|
633
|
+
|
634
|
+
// Remove this token from the saved list
|
635
|
+
saved_tokens = saved_tokens.slice(0,index).concat(saved_tokens.slice(index+1));
|
636
|
+
if(index < selected_token_index) selected_token_index--;
|
637
|
+
|
638
|
+
// Update the hidden input
|
639
|
+
update_hidden_input(saved_tokens, hidden_input);
|
640
|
+
|
641
|
+
token_count -= 1;
|
642
|
+
|
643
|
+
if(settings.tokenLimit !== null) {
|
644
|
+
input_box
|
645
|
+
.show()
|
646
|
+
.val("");
|
647
|
+
focus_with_timeout(input_box);
|
648
|
+
}
|
649
|
+
|
650
|
+
// Execute the onDelete callback if defined
|
651
|
+
if($.isFunction(callback)) {
|
652
|
+
callback.call(hidden_input,token_data);
|
653
|
+
}
|
654
|
+
}
|
655
|
+
|
656
|
+
// Update the hidden input box value
|
657
|
+
function update_hidden_input(saved_tokens, hidden_input) {
|
658
|
+
var token_values = $.map(saved_tokens, function (el) {
|
659
|
+
if(typeof settings.tokenValue == 'function')
|
660
|
+
return settings.tokenValue.call(this, el);
|
661
|
+
|
662
|
+
return el[settings.tokenValue];
|
663
|
+
});
|
664
|
+
hidden_input.val(token_values.join(settings.tokenDelimiter));
|
665
|
+
|
666
|
+
}
|
667
|
+
|
668
|
+
// Hide and clear the results dropdown
|
669
|
+
function hide_dropdown () {
|
670
|
+
dropdown.hide().empty();
|
671
|
+
selected_dropdown_item = null;
|
672
|
+
}
|
673
|
+
|
674
|
+
function show_dropdown() {
|
675
|
+
dropdown
|
676
|
+
.css({
|
677
|
+
position: "absolute",
|
678
|
+
top: $(token_list).offset().top + $(token_list).outerHeight(),
|
679
|
+
left: $(token_list).offset().left,
|
680
|
+
width: $(token_list).outerWidth(),
|
681
|
+
'z-index': settings.zindex
|
682
|
+
})
|
683
|
+
.show();
|
684
|
+
}
|
685
|
+
|
686
|
+
function show_dropdown_searching () {
|
687
|
+
if(settings.searchingText) {
|
688
|
+
dropdown.html("<p>"+settings.searchingText+"</p>");
|
689
|
+
show_dropdown();
|
690
|
+
}
|
691
|
+
}
|
692
|
+
|
693
|
+
function show_dropdown_hint () {
|
694
|
+
if(settings.hintText) {
|
695
|
+
dropdown.html("<p>"+settings.hintText+"</p>");
|
696
|
+
show_dropdown();
|
697
|
+
}
|
698
|
+
}
|
699
|
+
|
700
|
+
// Highlight the query part of the search term
|
701
|
+
function highlight_term(value, term) {
|
702
|
+
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>");
|
703
|
+
}
|
704
|
+
|
705
|
+
function find_value_and_highlight_term(template, value, term) {
|
706
|
+
return template.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + value + ")(?![^<>]*>)(?![^&;]+;)", "g"), highlight_term(value, term));
|
707
|
+
}
|
708
|
+
|
709
|
+
// Populate the results dropdown with some results
|
710
|
+
function populate_dropdown (query, results) {
|
711
|
+
if(results && results.length) {
|
712
|
+
dropdown.empty();
|
713
|
+
var dropdown_ul = $("<ul>")
|
714
|
+
.appendTo(dropdown)
|
715
|
+
.mouseover(function (event) {
|
716
|
+
select_dropdown_item($(event.target).closest("li"));
|
717
|
+
})
|
718
|
+
.mousedown(function (event) {
|
719
|
+
add_token($(event.target).closest("li").data("tokeninput"));
|
720
|
+
hidden_input.change();
|
721
|
+
return false;
|
722
|
+
})
|
723
|
+
.hide();
|
724
|
+
|
725
|
+
$.each(results, function(index, value) {
|
726
|
+
var this_li = settings.resultsFormatter(value);
|
727
|
+
|
728
|
+
this_li = find_value_and_highlight_term(this_li ,value[settings.propertyToSearch], query);
|
729
|
+
|
730
|
+
this_li = $(this_li).appendTo(dropdown_ul);
|
731
|
+
|
732
|
+
if(index % 2) {
|
733
|
+
this_li.addClass(settings.classes.dropdownItem);
|
734
|
+
} else {
|
735
|
+
this_li.addClass(settings.classes.dropdownItem2);
|
736
|
+
}
|
737
|
+
|
738
|
+
if(index === 0) {
|
739
|
+
select_dropdown_item(this_li);
|
740
|
+
}
|
741
|
+
|
742
|
+
$.data(this_li.get(0), "tokeninput", value);
|
743
|
+
});
|
744
|
+
|
745
|
+
show_dropdown();
|
746
|
+
|
747
|
+
if(settings.animateDropdown) {
|
748
|
+
dropdown_ul.slideDown("fast");
|
749
|
+
} else {
|
750
|
+
dropdown_ul.show();
|
751
|
+
}
|
752
|
+
} else {
|
753
|
+
if(settings.noResultsText) {
|
754
|
+
dropdown.html("<p>"+settings.noResultsText+"</p>");
|
755
|
+
show_dropdown();
|
756
|
+
}
|
757
|
+
}
|
758
|
+
}
|
759
|
+
|
760
|
+
// Highlight an item in the results dropdown
|
761
|
+
function select_dropdown_item (item) {
|
762
|
+
if(item) {
|
763
|
+
if(selected_dropdown_item) {
|
764
|
+
deselect_dropdown_item($(selected_dropdown_item));
|
765
|
+
}
|
766
|
+
|
767
|
+
item.addClass(settings.classes.selectedDropdownItem);
|
768
|
+
selected_dropdown_item = item.get(0);
|
769
|
+
}
|
770
|
+
}
|
771
|
+
|
772
|
+
// Remove highlighting from an item in the results dropdown
|
773
|
+
function deselect_dropdown_item (item) {
|
774
|
+
item.removeClass(settings.classes.selectedDropdownItem);
|
775
|
+
selected_dropdown_item = null;
|
776
|
+
}
|
777
|
+
|
778
|
+
// Do a search and show the "searching" dropdown if the input is longer
|
779
|
+
// than settings.minChars
|
780
|
+
function do_search() {
|
781
|
+
var query = input_box.val();
|
782
|
+
|
783
|
+
if(query && query.length) {
|
784
|
+
if(selected_token) {
|
785
|
+
deselect_token($(selected_token), POSITION.AFTER);
|
786
|
+
}
|
787
|
+
|
788
|
+
if(query.length >= settings.minChars) {
|
789
|
+
show_dropdown_searching();
|
790
|
+
clearTimeout(timeout);
|
791
|
+
|
792
|
+
timeout = setTimeout(function(){
|
793
|
+
run_search(query);
|
794
|
+
}, settings.searchDelay);
|
795
|
+
} else {
|
796
|
+
hide_dropdown();
|
797
|
+
}
|
798
|
+
}
|
799
|
+
}
|
800
|
+
|
801
|
+
// Do the actual search
|
802
|
+
function run_search(query) {
|
803
|
+
var cache_key = query + computeURL();
|
804
|
+
var cached_results = cache.get(cache_key);
|
805
|
+
if(cached_results) {
|
806
|
+
populate_dropdown(query, cached_results);
|
807
|
+
} else {
|
808
|
+
// Are we doing an ajax search or local data search?
|
809
|
+
if(settings.url) {
|
810
|
+
var url = computeURL();
|
811
|
+
// Extract exisiting get params
|
812
|
+
var ajax_params = {};
|
813
|
+
ajax_params.data = {};
|
814
|
+
if(url.indexOf("?") > -1) {
|
815
|
+
var parts = url.split("?");
|
816
|
+
ajax_params.url = parts[0];
|
817
|
+
|
818
|
+
var param_array = parts[1].split("&");
|
819
|
+
$.each(param_array, function (index, value) {
|
820
|
+
var kv = value.split("=");
|
821
|
+
ajax_params.data[kv[0]] = kv[1];
|
822
|
+
});
|
823
|
+
} else {
|
824
|
+
ajax_params.url = url;
|
825
|
+
}
|
826
|
+
|
827
|
+
// Prepare the request
|
828
|
+
ajax_params.data[settings.queryParam] = query;
|
829
|
+
ajax_params.type = settings.method;
|
830
|
+
ajax_params.dataType = settings.contentType;
|
831
|
+
if(settings.crossDomain) {
|
832
|
+
ajax_params.dataType = "jsonp";
|
833
|
+
}
|
834
|
+
|
835
|
+
// Attach the success callback
|
836
|
+
ajax_params.success = function(results) {
|
837
|
+
if($.isFunction(settings.onResult)) {
|
838
|
+
results = settings.onResult.call(hidden_input, results);
|
839
|
+
}
|
840
|
+
cache.add(cache_key, settings.jsonContainer ? results[settings.jsonContainer] : results);
|
841
|
+
|
842
|
+
// only populate the dropdown if the results are associated with the active search query
|
843
|
+
if(input_box.val() === query) {
|
844
|
+
populate_dropdown(query, settings.jsonContainer ? results[settings.jsonContainer] : results);
|
845
|
+
}
|
846
|
+
};
|
847
|
+
|
848
|
+
// Make the request
|
849
|
+
$.ajax(ajax_params);
|
850
|
+
} else if(settings.local_data) {
|
851
|
+
// Do the search through local data
|
852
|
+
var results = $.grep(settings.local_data, function (row) {
|
853
|
+
return row[settings.propertyToSearch].toLowerCase().indexOf(query.toLowerCase()) > -1;
|
854
|
+
});
|
855
|
+
|
856
|
+
if($.isFunction(settings.onResult)) {
|
857
|
+
results = settings.onResult.call(hidden_input, results);
|
858
|
+
}
|
859
|
+
cache.add(cache_key, results);
|
860
|
+
populate_dropdown(query, results);
|
861
|
+
}
|
862
|
+
}
|
863
|
+
}
|
864
|
+
|
865
|
+
// compute the dynamic URL
|
866
|
+
function computeURL() {
|
867
|
+
var url = settings.url;
|
868
|
+
if(typeof settings.url == 'function') {
|
869
|
+
url = settings.url.call(settings);
|
870
|
+
}
|
871
|
+
return url;
|
872
|
+
}
|
873
|
+
|
874
|
+
// Bring browser focus to the specified object.
|
875
|
+
// Use of setTimeout is to get around an IE bug.
|
876
|
+
// (See, e.g., http://stackoverflow.com/questions/2600186/focus-doesnt-work-in-ie)
|
877
|
+
//
|
878
|
+
// obj: a jQuery object to focus()
|
879
|
+
function focus_with_timeout(obj) {
|
880
|
+
setTimeout(function() { obj.focus(); }, 50);
|
881
|
+
}
|
882
|
+
|
883
|
+
};
|
884
|
+
|
885
|
+
// Really basic cache for the results
|
886
|
+
$.TokenList.Cache = function (options) {
|
887
|
+
var settings = $.extend({
|
888
|
+
max_size: 500
|
889
|
+
}, options);
|
890
|
+
|
891
|
+
var data = {};
|
892
|
+
var size = 0;
|
893
|
+
|
894
|
+
var flush = function () {
|
895
|
+
data = {};
|
896
|
+
size = 0;
|
897
|
+
};
|
898
|
+
|
899
|
+
this.add = function (query, results) {
|
900
|
+
if(size > settings.max_size) {
|
901
|
+
flush();
|
902
|
+
}
|
903
|
+
|
904
|
+
if(!data[query]) {
|
905
|
+
size += 1;
|
906
|
+
}
|
907
|
+
|
908
|
+
data[query] = results;
|
909
|
+
};
|
910
|
+
|
911
|
+
this.get = function (query) {
|
912
|
+
return data[query];
|
913
|
+
};
|
914
|
+
};
|
915
|
+
}(jQuery));
|