Skip to content

Commit fa4d20a

Browse files
committed
Continue improving the docs.
1 parent 6915ba7 commit fa4d20a

File tree

3 files changed

+39
-50
lines changed

3 files changed

+39
-50
lines changed

docs/developer/index.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Developer documentation
2+
3+
- [Patternslib overview](overview.md)
4+
- [The structure of a pattern](patterns.md)
5+
- [Configuring a pattern](patterns-configuration.md)
6+
- [Styleguide](styleguide.md)

docs/developer/patterns.rst

+31-48
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
1-
Creating a new pattern
2-
======================
1+
The structure of a pattern
2+
==========================
33

4-
Patterns are implemented as JavaScript classes that are registered with the patterns library.
5-
Below is a minimal skeleton for a pattern.
4+
Patterns are implemented as JavaScript classes that are registered with the patterns registry.
5+
Below is a minimalistic skeleton for a pattern with explanations as inline comments.
66

77
.. code-block:: javascript
88
:linenos:
99
1010
import { BasePattern } from "@patternslib/patternslib/src/core/basepattern";
11+
import Parser from "@patternslib/patternslib/src/core/parser";
1112
import registry from "@patternslib/patternslib/src/core/registry";
1213
14+
export const parser = new Parser("test-pattern");
15+
// Define an argument with a default value. You can configure the value via
16+
// data-attributes.
17+
parser.addArgument("example-option", "Hollareidulio");
18+
1319
class Pattern extends BasePattern {
20+
21+
// Define a name for the pattern which is used as key in the pattern
22+
// registry.
1423
static name = "test-pattern";
24+
25+
// Define a CSS selector. The pattern will be initialized on elements
26+
// matching this selector.
1527
static trigger = ".pat-test-pattern";
1628
29+
// The parser instance from above.
30+
static parser = parser;
31+
1732
init() {
33+
import("./test-pattern.scss");
34+
35+
// Try to avoid jQuery, but here is how to import it, asynchronously.
36+
// eslint-disable-next-line no-unused-vars
37+
const $ = (await import("jquery")).default;
38+
39+
// The options are automatically created, if parser is defined.
40+
const example_option = this.options.exampleOption;
41+
this.el.innerHTML = `
42+
<p>${example_option}, this is the ${this.name} pattern!</p>
43+
`;
1844
}
1945
}
2046
@@ -29,55 +55,12 @@ Below is a minimal skeleton for a pattern.
2955
export { Pattern };
3056
3157
32-
This skeleton does several things:
33-
34-
* lines 1-4 use `RequireJS <http://requirejs.org/>`_ to load the patterns
35-
registry.
36-
* lines 5-7 create an object which defines this pattern's specifications.
37-
* line 9 registers the pattern.
38-
39-
40-
Markup patterns
41-
---------------
42-
43-
Most patterns deal with markup: they are activated for content that matches
44-
a specific CSS selector. This is handled by adding two items to the
45-
pattern specification: a ``trigger`` and an ``init`` function.
46-
47-
.. code-block:: javascript
48-
:linenos:
49-
50-
var pattern_spec = {
51-
name: "mypattern",
52-
trigger: ".tooltip, [data-tooltip]",
53-
54-
init: function($el) {
55-
...
56-
},
57-
58-
destroy: function($el) {
59-
...
60-
}
61-
};
62-
63-
The trigger specified on line 3 is a CSS selector which tells the pattern
64-
framework which elements this pattern is interested in. If new items are
65-
discovered in the DOM that match this pattern, the ``init`` function will be
66-
called with a jQuery wrapper around the element.
67-
68-
While not required patterns are encouraged to include a ``destroy`` function
69-
that undos the pattern initialisation. After calling ``destroy`` it should be
70-
possible to call ``init`` again to reactivate the pattern.
71-
72-
Methods must always return ``this`` to facilitate their use as jQuery widgets.
73-
7458
7559
Pattern configuration
7660
---------------------
7761

7862
The configuration of a pattern is generally based on three components: the
79-
default settings, configuration set on a DOM element via a data-attribute, and,
80-
if the jQuery API is used, via options passed in via the jQuery plugin API.
63+
default settings, configuration set on a DOM element via a data-attribute.
8164
The init method for patterns should combine these settings. Let's update our
8265
example pattern to do this:
8366

src/core/basepattern.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Also see: https://github.com/Patternslib/pat-PATTERN_TEMPLATE
1616
import registry from "@patternslib/patternslib/src/core/registry";
1717

1818
export const parser = new Parser("test-pattern");
19-
parser.addArgument("example-option", "Stranger");
19+
parser.addArgument("example-option", "Hollareidulio");
2020

2121
class Pattern extends BasePattern {
2222
static name = "test-pattern";
@@ -33,7 +33,7 @@ Also see: https://github.com/Patternslib/pat-PATTERN_TEMPLATE
3333
// The options are automatically created, if parser is defined.
3434
const example_option = this.options.exampleOption;
3535
this.el.innerHTML = `
36-
<p>hello, ${example_option}, this is pattern ${this.name} speaking.</p>
36+
<p>${example_option}, this is the ${this.name} pattern!</p>
3737
`;
3838
}
3939
}

0 commit comments

Comments
 (0)