Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit d414767

Browse files
adifucankeharper
authored andcommitted
Replace jquery/ui usages with individual jQuery UI components (#5923)
* Replace jquery/ui usages with individual jQuery UI components * Update custom_js.md * Update js_practice.md * Update custom_js.md Correcting linting error
1 parent 71cbebd commit d414767

File tree

2 files changed

+309
-2
lines changed

2 files changed

+309
-2
lines changed

guides/v2.3/javascript-dev-guide/javascript/custom_js.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
---
2+
group: javascript-developer-guide
3+
title: Use custom JavaScript
4+
---
5+
6+
This topic discusses how to use custom [JavaScript](https://glossary.magento.com/javascript) components with the components provided by Magento or custom replacement implementations.
7+
8+
We strongly recommend that you do not change the source code of default Magento components and widgets. All customizations must be implemented in custom modules or themes.
9+
10+
## Add a custom JS component {#custom_js_overview}
11+
12+
To add a custom JS component (module), take the following steps:
13+
14+
1. Place the custom component source file in one of the following locations:
15+
- Your theme JS files: `<theme_dir>/web/js` or `<theme_dir>/<VendorName>_<ModuleName>/web/js`. In this case the component is available in your theme and its [child themes]({{ page.baseurl }}/frontend-dev-guide/themes/theme-inherit.html).
16+
- Your module view JS files: `<module_dir>/view/frontend/web/js`. In this case, the component is available in all modules and themes (if your module is enabled).
17+
18+
1. Optionally, in the corresponding [module](https://glossary.magento.com/module) or theme, create a `requirejs-config.js` configuration file, if it does not yet exist there and set path for your resource. The RequireJS configuration file can be placed in one of the following locations:
19+
20+
- Your theme: `<theme_dir>`
21+
- Module within your theme: `<theme_dir>/<module_dir>`
22+
- Your module (depending on the needed area - **base**, **frontend**, **adminhtml**): `<module_dir>/view/<area>`
23+
24+
## Replace a default JS component {#js_replace}
25+
26+
To use a custom implementation of an existing Magento JS component:
27+
28+
Place the custom component source file in one of the following locations:
29+
30+
- Your theme JS files: `/web/js`
31+
- Your module view JS files: `<module_dir>/view/frontend/web/js`
32+
33+
Create a RequireJS configuration file `requirejs-config.js`, having specified the following:
34+
35+
```javascript
36+
var config = {
37+
"map": {
38+
"*": {
39+
"<default_component>": "<custom_component>"
40+
}
41+
}
42+
};
43+
```
44+
45+
- `<default_component>`: the name of the default component you replace
46+
- `<custom_component>`: the name of the custom component
47+
48+
For example, if you want to use a custom `navigation-menu.js` script instead of the default menu widgets, your `requirejs-config.js` should contain the following:
49+
50+
```javascript
51+
var config = {
52+
"map": {
53+
"*": {
54+
"menu": "js/navigation-menu",
55+
"mage/backend/menu": "js/navigation-menu"
56+
}
57+
}
58+
};
59+
```
60+
61+
Place your `requirejs-config.js` file in one of the following directories (according to the location of your custom script, see step 1 of this procedure):
62+
63+
- Your [theme](https://glossary.magento.com/theme) files: `<theme_dir>`
64+
- Your module view files: `<module_dir>/view/frontend`
65+
66+
This way, your custom JS component is used instead of the [Magento component](https://glossary.magento.com/magento-component) in all entries all over the [frontend](https://glossary.magento.com/frontend) area.
67+
68+
## Extend a default JS component {#extend_js}
69+
70+
You can add a custom JS component/widget, which will extend a default Magento component/widget.
71+
72+
### Extend Magento widget {#extend_js_widget}
73+
74+
To extend a default Magento [jQuery](https://glossary.magento.com/jquery) widget, create `<your_widget_name>.js` with contents similar to the following:
75+
76+
```javascript
77+
define([
78+
'jquery',
79+
'jquery-ui-modules/widget', // use individual jQuery UI component if your widget is for frontend or base areas
80+
// 'jquery/ui', // use all 'jquery/ui' library if your widget is for adminhtml area
81+
'mage/<widget.name>' // usually widget can be found in /lib/web/mage dir
82+
], function($){
83+
84+
$.widget('<your_namespace>.<your_widget_name>', $.mage.<widget.name>, { ... });
85+
86+
return $.<your_namespace>.<your_widget_name>;
87+
});
88+
```
89+
90+
Where the following notation is used:
91+
92+
- `<your_namespace>.<your_widget_name>` - the name of your custom [widget](https://glossary.magento.com/widget). According to the jQuery widgets naming convention, this value must contain a [namespace](https://glossary.magento.com/namespace) and name.
93+
- `mage.<widget.name>` - the name of the Magento widget that you extend.
94+
95+
{:.bs-callout .bs-callout-tip}
96+
All jQuery UI components for frontend and base areas are located in `lib/web/jquery/ui-modules` dir. They can be used in JS widgets by `jquery-ui-modules` path mapping like `jquery-ui-modules/widget` and `jquery-ui-modules/slider`.
97+
Using individual jQuery UI components instead of the monolithic jQuery UI library improves storefront performance.
98+
99+
For information about initializing your custom widget in a `.phtml` template, see the [JavaScript initialization]({{ page.baseurl }}/javascript-dev-guide/javascript/js_init.html) topic.
100+
101+
### Extend a default Ui component {#extend_js_component}
102+
103+
To extend a default JS Ui component, your custom script must contain the following:
104+
105+
```javascript
106+
define([
107+
'<component_path>'
108+
], function(<component_alias>){
109+
110+
return <component_alias>.extend({
111+
112+
defaults: { ... }, // properties with default values
113+
... // methods of your component
114+
});
115+
});
116+
```
117+
118+
Where the following notation is used:
119+
120+
- `<component_path>`: path to the default component that you extend
121+
- `<component_alias>`: variable containing the default component that you extend
122+
123+
For example, `Filters.js` script extends the default `filters.js`:
124+
125+
```javascript
126+
define([
127+
'Magento_Ui/js/grid/filters/filters'
128+
], function(Filters){
129+
130+
return Filters.extend({
131+
132+
defaults: { ... }, // properties with default values
133+
... // methods of your component
134+
});
135+
});
136+
```
137+
138+
For information about initializing your custom JS component in a `.phtml` template, see the [JavaScript initialization] topic.
139+
140+
## Disable default Magento JS {#disable_default_js}
141+
142+
To disable the auto-loading of default Magento JS components and widget initialization:
143+
144+
1. Create a `requirejs-config.js` file with the following content:
145+
146+
```javascript
147+
var config = { deps: [ ] };
148+
```
149+
150+
1. Put the `requirejs-config.js` file in one of the following
151+
locations:
152+
153+
- Your custom theme files: `<theme_dir>`
154+
- Your custom module files: `<module_dir>/view/frontend`
155+
156+
If you need to enable the loading of default Magento JS components and widget initialization on a certain stage, add the following code in your JS script:
157+
158+
```javascript
159+
$(mage.apply);
160+
```
161+
162+
{:.ref-header}
163+
Related topic
164+
165+
- [JavaScript resources in Magento]({{ page.baseurl }}/javascript-dev-guide/javascript/js-resources.html)
166+
- [About AMD modules and RequireJS]({{ page.baseurl }}/javascript-dev-guide/javascript/js-resources.html)
167+
168+
[JavaScript initialization]: {{page.baseurl}}/javascript-dev-guide/javascript/js_init.html
169+
v2.3/performance-best-practices/advanced-js-bundling.html

guides/v2.3/javascript-dev-guide/javascript/js_practice.md

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
group: javascript-developer-guide
3+
title: Customizing JavaScript illustration
4+
---
5+
6+
This topic features a step-by-step illustration of how to customize a [jQuery](https://glossary.magento.com/jquery) [widget](https://glossary.magento.com/widget) and how to use a custom widget instead the default Magento one.
7+
8+
## Customize a default Magento jQuery widget
9+
10+
In their Orange theme, OrangeCo wants to remove the "Click on image to view it full sized" message displayed on the product page.
11+
12+
The high-level steps for this task are the following:
13+
14+
1. Define how the message is output.
15+
1. Add the custom script extending the default one.
16+
1. Update RequireJS configuration to use the custom script instead of the default one.
17+
18+
Let's look at each step in more detail.
19+
20+
### Step 1: Define how the message is output {#define_script1}
21+
22+
OrangeCo needs to define how the message is output. To do this, they take the following steps:
23+
24+
1. Open a product page.
25+
1. Select to view the page source.
26+
1. Search for the "Click on image to view it full sized" string. The illustration of the search result follows: ![Page source search result]
27+
1. View that it is output by [`gallery.js`].
28+
29+
We see that the script which OrangeCo needs to alter is `gallery.js`.
30+
31+
To be able to extend `gallery.js`, OrangeCo needs to know the path to it. To get this info, they refer to `requirejs-config.js`, which [can be reached from the page source view or from the file system]. According to the configuration, the path for `gallery` is `mage/gallery`. The illustration follows:
32+
33+
![RequireJS config file]
34+
35+
### Step 2: Add the custom widget extending the gallery widget {#add_code1}
36+
37+
In the `app/design/frontend/OrangeCo/orange/web/js` OrangeCo adds `orange-gallery.js` with the following content:
38+
39+
```javascript
40+
define([
41+
'jquery',
42+
'jquery-ui-modules/widget',
43+
'mage/gallery'
44+
], function($){
45+
46+
$.widget('orange.gallery', $.mage.gallery, {
47+
_create: function() { // special method of jQuery UI Widgets
48+
this._setOption('controls', {'notice': {}});
49+
}
50+
});
51+
52+
return $.orange.gallery;
53+
});
54+
```
55+
56+
### Step 3: Update the RequireJS configuration {#config1}
57+
58+
OrangeCo adds the custom `app/design/OrangeCo/orange/requirejs-config.js` with the following content:
59+
60+
```javascript
61+
var config = {
62+
"map": {
63+
"*": {
64+
"gallery": "js/orange-gallery"
65+
}
66+
}
67+
};
68+
```
69+
70+
The new behavior is applied once the store pages are reloaded.
71+
72+
## Add and use a custom widget (jCarousel) {#use_custom_widget}
73+
74+
OrangeCo wants to use the [jCarousel widget] to display product images on product pages. The high level steps for this task are the following:
75+
76+
1. Define how product images are displayed by default.
77+
1. Add the custom script to the file system.
78+
1. Update RequireJS configuration to use the custom script instead of the default one.
79+
80+
Let's look at each step in more detail.
81+
82+
### Step 1: Define what is the default implementation
83+
84+
Using the approach described in the previous section, OrangeCo defines that the product images are displayed by [`gallery.js`], and the configuration path for it is `mage/gallery`.
85+
86+
### Step 2: Add the custom script to the file system
87+
88+
For the jCarousel widget to be able to use the configuration passed to the gallery widget,
89+
OrangeCo needs to add a "wrapper" script.
90+
91+
To do this, OrangeCo adds the following files in the `app/design/frontend/OrangeCo/orange/web/js` directory:
92+
93+
- The jCarousel widget source file: `jquery.jcarousel.js`
94+
- A \"wrapper\" `orange-carousel.js` with the following content:
95+
96+
```javascript
97+
define([
98+
'jquery',
99+
'js/jquery.jcarousel'
100+
], function($){
101+
102+
return function (config, element) {
103+
var jCarouselConfig = {
104+
list: '.items.thumbs',
105+
items: '.item.thumb'
106+
};
107+
$(element).jcarousel(jCarouselConfig);
108+
}
109+
});
110+
```
111+
112+
### Step 3: Update RequireJS configuration
113+
114+
In the `app/design/OrangeCo/orange` directory OrangeCo adds `requirejs-config.js` with the following content:
115+
116+
```javascript
117+
var config = {
118+
"map": {
119+
"*": {
120+
"gallery": "js/orange-gallery"
121+
}
122+
},
123+
"shim": {
124+
"js/jquery.jcarousel": ["jquery"] // as jquery.jcarousel isn't an AMD module
125+
}
126+
};
127+
```
128+
129+
{:.ref-header}
130+
Recommended reading
131+
132+
[Use custom JavaScript]
133+
134+
[Page source search result]: {{site.baseurl}}/common/images/fdg_js_pr1.png
135+
[`gallery.js`]: {{ site.mage2bloburl }}/{{ page.guide_version }}/lib/web/mage/gallery/gallery.js
136+
[can be reached from the page source view or from the file system]: {{page.baseurl}}/javascript-dev-guide/javascript/custom_js.html#extend_js
137+
[RequireJS config file]: {{site.baseurl}}/common/images/fdg_pr_2.png
138+
[jCarousel widget]: http://sorgalla.com/jcarousel/
139+
[`gallery.js`]: {{ site.mage2bloburl }}/{{ page.guide_version }}/lib/web/mage/gallery/gallery.js
140+
[Use custom JavaScript]: {{page.baseurl}}/javascript-dev-guide/javascript/custom_js.html

0 commit comments

Comments
 (0)