Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
coffee
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
coffee
Merge requests
!16
Make the AJAX request on-demand.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Make the AJAX request on-demand.
issue/coffee-3494208:3494208-ajax-request-on-demand
into
2.x
Overview
0
Commits
1
Pipelines
2
Changes
1
Merged
catch
requested to merge
issue/coffee-3494208:3494208-ajax-request-on-demand
into
2.x
4 months ago
Overview
0
Commits
1
Pipelines
2
Changes
1
Expand
Closes
#3494208
0
0
Merge request reports
Compare
2.x
version 1
4b473472
4 months ago
2.x (base)
and
latest version
latest version
4b473472
1 commit,
4 months ago
version 1
4b473472
1 commit,
4 months ago
1 file
+
90
−
81
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
js/coffee.js
+
90
−
81
Options
@@ -67,90 +67,9 @@
.
wrapInner
(
'
<div id="coffee-form-inner" />
'
)
.
appendTo
(
DrupalCoffee
.
wrapper
);
// Load autocomplete data set, consider implementing
// caching with local storage.
DrupalCoffee
.
dataset
=
[];
DrupalCoffee
.
isItemSelected
=
false
;
var
autocomplete_data_element
=
'
ui-autocomplete
'
;
var
url
;
if
(
drupalSettings
.
coffee
.
dataPath
)
{
url
=
drupalSettings
.
coffee
.
dataPath
;
}
else
{
url
=
Drupal
.
url
(
'
admin/coffee/get-data
'
);
}
$
.
ajax
({
url
:
url
,
dataType
:
'
json
'
,
success
:
function
(
data
)
{
DrupalCoffee
.
dataset
=
data
;
// Apply autocomplete plugin on show.
var
$autocomplete
=
$
(
DrupalCoffee
.
field
).
autocomplete
({
source
:
DrupalCoffee
.
dataset
,
focus
:
function
(
event
,
ui
)
{
// Prevents replacing the value of the input field.
DrupalCoffee
.
isItemSelected
=
true
;
event
.
preventDefault
();
},
change
:
function
(
event
,
ui
)
{
DrupalCoffee
.
isItemSelected
=
false
;
},
select
:
function
(
event
,
ui
)
{
DrupalCoffee
.
redirect
(
ui
.
item
.
value
,
event
.
metaKey
||
event
.
ctrlKey
);
event
.
preventDefault
();
return
false
;
},
delay
:
0
,
appendTo
:
DrupalCoffee
.
results
});
$autocomplete
.
data
(
autocomplete_data_element
).
_renderItem
=
function
(
ul
,
item
)
{
// Strip the basePath when displaying the link description.
var
description
=
item
.
value
;
if
(
item
.
value
.
indexOf
(
drupalSettings
.
path
.
basePath
)
===
0
)
{
description
=
item
.
value
.
substring
(
drupalSettings
.
path
.
basePath
.
length
);
}
return
$
(
'
<li></li>
'
)
.
data
(
'
item.autocomplete
'
,
item
)
.
append
(
'
<a>
'
+
item
.
label
+
'
<small class="description">
'
+
description
+
'
</small></a>
'
)
.
appendTo
(
ul
);
};
// We want to limit the number of results.
$
(
DrupalCoffee
.
field
).
data
(
autocomplete_data_element
).
_renderMenu
=
function
(
ul
,
items
)
{
var
self
=
this
;
items
=
items
.
slice
(
0
,
drupalSettings
.
coffee
.
maxResults
);
$
.
each
(
items
,
function
(
index
,
item
)
{
self
.
_renderItemData
(
ul
,
item
);
});
};
DrupalCoffee
.
form
.
keydown
(
function
(
event
)
{
if
(
event
.
keyCode
===
13
)
{
var
openInNewWindow
=
false
;
if
(
event
.
metaKey
||
event
.
ctrlKey
)
{
openInNewWindow
=
true
;
}
if
(
!
DrupalCoffee
.
isItemSelected
)
{
var
$firstItem
=
$
(
DrupalCoffee
.
results
).
find
(
'
li:first
'
).
data
(
'
item.autocomplete
'
);
if
(
typeof
$firstItem
===
'
object
'
)
{
DrupalCoffee
.
redirect
(
$firstItem
.
value
,
openInNewWindow
);
event
.
preventDefault
();
}
}
}
});
},
error
:
function
()
{
DrupalCoffee
.
field
.
val
(
'
Could not load data, please refresh the page
'
);
}
});
$
(
'
.toolbar-icon-coffee
'
).
click
(
function
(
event
)
{
event
.
preventDefault
();
DrupalCoffee
.
coffee_show
();
@@ -178,12 +97,102 @@
}
};
/**
* Initializes the autocomplete widget with data.
*/
DrupalCoffee
.
coffee_initialize_search_box
=
function
()
{
// Only do this once per page request to allow for opening and closing the
// dialog multiple times.
if
(
DrupalCoffee
.
dataset
.
length
!==
0
)
{
return
;
}
var
autocomplete_data_element
=
'
ui-autocomplete
'
;
var
url
;
if
(
drupalSettings
.
coffee
.
dataPath
)
{
url
=
drupalSettings
.
coffee
.
dataPath
;
}
else
{
url
=
Drupal
.
url
(
'
admin/coffee/get-data
'
);
}
$
.
ajax
({
url
:
url
,
dataType
:
'
json
'
,
success
:
function
(
data
)
{
DrupalCoffee
.
dataset
=
data
;
// Apply autocomplete plugin on show.
var
$autocomplete
=
$
(
DrupalCoffee
.
field
).
autocomplete
({
source
:
DrupalCoffee
.
dataset
,
focus
:
function
(
event
,
ui
)
{
// Prevents replacing the value of the input field.
DrupalCoffee
.
isItemSelected
=
true
;
event
.
preventDefault
();
},
change
:
function
(
event
,
ui
)
{
DrupalCoffee
.
isItemSelected
=
false
;
},
select
:
function
(
event
,
ui
)
{
DrupalCoffee
.
redirect
(
ui
.
item
.
value
,
event
.
metaKey
||
event
.
ctrlKey
);
event
.
preventDefault
();
return
false
;
},
delay
:
0
,
appendTo
:
DrupalCoffee
.
results
});
$autocomplete
.
data
(
autocomplete_data_element
).
_renderItem
=
function
(
ul
,
item
)
{
// Strip the basePath when displaying the link description.
var
description
=
item
.
value
;
if
(
item
.
value
.
indexOf
(
drupalSettings
.
path
.
basePath
)
===
0
)
{
description
=
item
.
value
.
substring
(
drupalSettings
.
path
.
basePath
.
length
);
}
return
$
(
'
<li></li>
'
)
.
data
(
'
item.autocomplete
'
,
item
)
.
append
(
'
<a>
'
+
item
.
label
+
'
<small class="description">
'
+
description
+
'
</small></a>
'
)
.
appendTo
(
ul
);
};
// We want to limit the number of results.
$
(
DrupalCoffee
.
field
).
data
(
autocomplete_data_element
).
_renderMenu
=
function
(
ul
,
items
)
{
var
self
=
this
;
items
=
items
.
slice
(
0
,
drupalSettings
.
coffee
.
maxResults
);
$
.
each
(
items
,
function
(
index
,
item
)
{
self
.
_renderItemData
(
ul
,
item
);
});
};
DrupalCoffee
.
form
.
keydown
(
function
(
event
)
{
if
(
event
.
keyCode
===
13
)
{
var
openInNewWindow
=
false
;
if
(
event
.
metaKey
||
event
.
ctrlKey
)
{
openInNewWindow
=
true
;
}
if
(
!
DrupalCoffee
.
isItemSelected
)
{
var
$firstItem
=
$
(
DrupalCoffee
.
results
).
find
(
'
li:first
'
).
data
(
'
item.autocomplete
'
);
if
(
typeof
$firstItem
===
'
object
'
)
{
DrupalCoffee
.
redirect
(
$firstItem
.
value
,
openInNewWindow
);
event
.
preventDefault
();
}
}
}
});
},
error
:
function
()
{
DrupalCoffee
.
field
.
val
(
'
Could not load data, please refresh the page
'
);
}
});
}
// Prefix the open and close functions to avoid
// conflicts with autocomplete plugin.
/**
* Open the form and focus on the search field.
*/
DrupalCoffee
.
coffee_show
=
function
()
{
DrupalCoffee
.
coffee_initialize_search_box
();
DrupalCoffee
.
wrapper
.
removeClass
(
'
hide-form
'
);
DrupalCoffee
.
bg
.
show
();
DrupalCoffee
.
field
.
focus
();
Loading