1 | [![CircleCi](https://circleci.com/gh/sulu/web-js.png?style=shield)](https://circleci.com/gh/sulu/web-js)
|
2 | [![npm](https://img.shields.io/npm/v/@sulu/web.svg)](https://www.npmjs.com/package/@sulu/web)
|
3 | [![Size](https://img.shields.io/github/size/sulu/web-js/packages/core/core.js.svg)](https://github.com/sulu/web-js/blob/master/packages/core/core.js)
|
4 | [![Install Size](https://packagephobia.now.sh/badge?p=@sulu/web)](https://packagephobia.now.sh/result?p=@sulu/web)
|
5 |
|
6 | # Web JS
|
7 |
|
8 | The web-js in connection with [web component twig extension](https://github.com/sulu/web-twig)
|
9 | gives you simple and efficient way to handle your javascript components over twig.
|
10 |
|
11 | ## Installation
|
12 |
|
13 | **NPM**
|
14 |
|
15 | ```bash
|
16 | npm install @sulu/web
|
17 | ```
|
18 |
|
19 | **Yarn**
|
20 |
|
21 | ```bash
|
22 | yarn add @sulu/web
|
23 | ```
|
24 |
|
25 | ## IE Support
|
26 |
|
27 | To Support IE 11 you need a polyfill for `object.assign` function e.g.:
|
28 |
|
29 | ```bash
|
30 | npm install core-js --save-dev
|
31 | ```
|
32 |
|
33 | or
|
34 |
|
35 | ```bash
|
36 | yarn install core-js --dev
|
37 | ```
|
38 |
|
39 | and at the top of your main.js file require the polyfill:
|
40 |
|
41 | ```js
|
42 | import 'core-js/features/object/assign';
|
43 | ```
|
44 |
|
45 | ## Usage
|
46 |
|
47 | ### Create your first component
|
48 |
|
49 | A component can be created using different js patterns:
|
50 |
|
51 | - [JS Class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)
|
52 | - [Revealing pattern](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript)
|
53 | - [Prototype pattern](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#prototypepatternjavascript)
|
54 | - and more which works with multiple instances you simple need to create a initialize method
|
55 |
|
56 | **Using ECMAScript 2015 class**
|
57 |
|
58 | ```js
|
59 | // js/components/test-class.js
|
60 | export default class Test {
|
61 | initialize(el, options) {
|
62 | this.text = options.text;
|
63 | el.onclick = this.say;
|
64 | }
|
65 |
|
66 | say() {
|
67 | alert(this.text);
|
68 | }
|
69 | }
|
70 | ```
|
71 |
|
72 | <details>
|
73 |
|
74 | <summary>
|
75 | **Using Revealing pattern**
|
76 | </summary>
|
77 |
|
78 | ```js
|
79 | // js/components/test-revealing-pattern.js
|
80 |
|
81 | module.exports = (function() {
|
82 | var test = {};
|
83 |
|
84 | test.initialize = function(el, options) {
|
85 | test.text = options.text;
|
86 | el.onclick = this.say;
|
87 | };
|
88 |
|
89 | test.say = function() {
|
90 | alert(test.text);
|
91 | }
|
92 |
|
93 | return {
|
94 | initialize: test.initialize,
|
95 | say: test.say,
|
96 | };
|
97 | });
|
98 | ```
|
99 |
|
100 | </details>
|
101 |
|
102 | <details>
|
103 |
|
104 | <summary>
|
105 | **Using Prototype pattern**
|
106 | </summary>
|
107 |
|
108 | ```js
|
109 | // js/components/test-prototype-pattern.js
|
110 | var test = function() {};
|
111 |
|
112 | test.prototype.initialize = function(el, options) {
|
113 | this.text = options.text;
|
114 | el.onclick = this.say;
|
115 | };
|
116 |
|
117 | test.prototype.say = function() {
|
118 | alert(this.test);
|
119 | };
|
120 |
|
121 | module.exports = test;
|
122 | ```
|
123 |
|
124 | </details>
|
125 |
|
126 | ### Create your first service
|
127 |
|
128 | Sometimes you want just run js code which is not binded to a dom element for this services where created.
|
129 | Typically usages are running tracking code functions.
|
130 |
|
131 | ```js
|
132 | // js/services/log.js
|
133 |
|
134 | module.exports = {
|
135 | log: function(text) {
|
136 | console.log(text);
|
137 | }
|
138 | };
|
139 | ```
|
140 |
|
141 | ### Initialize web.js and registering your components and services
|
142 |
|
143 | ```js
|
144 | import web from '@sulu/web';
|
145 | import Test from './components/test'
|
146 | import Other from './components/more'
|
147 | import Log from './services/log';
|
148 |
|
149 | // services
|
150 | web.registerService('logger', Log);
|
151 |
|
152 | // components
|
153 | web.registerComponent('test', Test);
|
154 | web.registerComponent('more', Test, { defaultOption: 'defaultValue' });
|
155 | ```
|
156 |
|
157 | ### Embedding in template
|
158 |
|
159 | For efficient handling it's recommended to use it with a template engine like twig.
|
160 |
|
161 | #### Twig
|
162 |
|
163 | For twig embedding see the [web component twig extension](https://github.com/sulu/web-twig).
|
164 |
|
165 | #### HTML
|
166 |
|
167 | You can also use without a template engine and by calling the startComponents and callServices.
|
168 |
|
169 | ```html
|
170 | <button id="test-1">
|
171 | Say Hello
|
172 | </button>
|
173 |
|
174 | <button id="test-2">
|
175 | Say Bye
|
176 | </button>
|
177 |
|
178 | <script src="js/main.js"></script>
|
179 | <script>
|
180 | web.startComponents([
|
181 | {name: 'test', id: 'test-1', { text: 'Hello' }},
|
182 | {name: 'test', id: 'test-2', { text: 'Bye' }},
|
183 | ]);
|
184 |
|
185 | web.callServices([
|
186 | {name: 'logger', func: 'log', args: ['Hello']},
|
187 | ]);
|
188 | </script>
|
189 | ```
|
190 |
|
191 | The `@sulu/web-js` provides some [`components`](packages/components) and [`services`](packages/services)
|
192 | which can be registered in your container and be used.
|
193 |
|
194 | ## Web CSS
|
195 |
|
196 | Beside the `js` the `@sulu/web-js` is also shipped with some `scss` tools to make also creating css
|
197 | easier. They can be found in the [`scss`](packages/scss) directory.
|
198 |
|
199 | ## Version Update & Publish to NPM (docs for maintainers)
|
200 |
|
201 | ### 1. Create release on github
|
202 |
|
203 | Update package.json version on master branch:
|
204 |
|
205 | ```bash
|
206 | git checkout master
|
207 | git pull origin master
|
208 | npm version [ major | minor | patch ] --no-git-tag-version
|
209 | # update version in changelog
|
210 | git add .
|
211 | git commit -m "Release <version>"
|
212 | git push origin master
|
213 | ```
|
214 |
|
215 | Generate changelog:
|
216 |
|
217 | ```bash
|
218 | github_changelog_generator --future-release <version>
|
219 | ```
|
220 |
|
221 | Copy the text of the last release into github release description and create new release.
|
222 |
|
223 | ### 2. Publish release
|
224 |
|
225 | ```
|
226 | git fetch --tags
|
227 | git checkout <version>
|
228 | # Test which files get packed by npm
|
229 | npm pack --dry-run
|
230 | # Publish package
|
231 | npm publish
|
232 | ```
|