UNPKG

5.06 kBMarkdownView Raw
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
8The web-js in connection with [web component twig extension](https://github.com/sulu/web-twig)
9gives you simple and efficient way to handle your javascript components over twig.
10
11## Installation
12
13**NPM**
14
15```bash
16npm install @sulu/web
17```
18
19**Yarn**
20
21```bash
22yarn add @sulu/web
23```
24
25## IE Support
26
27To Support IE 11 you need a polyfill for `object.assign` function e.g.:
28
29```bash
30npm install core-js --save-dev
31```
32
33or
34
35```bash
36yarn install core-js --dev
37```
38
39and at the top of your main.js file require the polyfill:
40
41```js
42import 'core-js/features/object/assign';
43```
44
45## Usage
46
47### Create your first component
48
49A 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
60export 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
81module.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
110var test = function() {};
111
112test.prototype.initialize = function(el, options) {
113 this.text = options.text;
114 el.onclick = this.say;
115};
116
117test.prototype.say = function() {
118 alert(this.test);
119};
120
121module.exports = test;
122```
123
124</details>
125
126### Create your first service
127
128Sometimes you want just run js code which is not binded to a dom element for this services where created.
129Typically usages are running tracking code functions.
130
131```js
132// js/services/log.js
133
134module.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
144import web from '@sulu/web';
145import Test from './components/test'
146import Other from './components/more'
147import Log from './services/log';
148
149// services
150web.registerService('logger', Log);
151
152// components
153web.registerComponent('test', Test);
154web.registerComponent('more', Test, { defaultOption: 'defaultValue' });
155```
156
157### Embedding in template
158
159For efficient handling it's recommended to use it with a template engine like twig.
160
161#### Twig
162
163For twig embedding see the [web component twig extension](https://github.com/sulu/web-twig).
164
165#### HTML
166
167You 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
191The `@sulu/web-js` provides some [`components`](packages/components) and [`services`](packages/services)
192which can be registered in your container and be used.
193
194## Web CSS
195
196Beside the `js` the `@sulu/web-js` is also shipped with some `scss` tools to make also creating css
197easier. 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
203Update package.json version on master branch:
204
205```bash
206git checkout master
207git pull origin master
208npm version [ major | minor | patch ] --no-git-tag-version
209# update version in changelog
210git add .
211git commit -m "Release <version>"
212git push origin master
213```
214
215Generate changelog:
216
217```bash
218github_changelog_generator --future-release <version>
219```
220
221Copy the text of the last release into github release description and create new release.
222
223### 2. Publish release
224
225```
226git fetch --tags
227git checkout <version>
228# Test which files get packed by npm
229npm pack --dry-run
230# Publish package
231npm publish
232```