You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
7.6 KiB

11 years ago
# Ply
Amazing layer/modal/dialog system. Wow!
11 years ago
## Features
11 years ago
* Demo: [http://rubaxa.github.io/Ply/](http://rubaxa.github.io/Ply/)
11 years ago
* Support browsers: Chrome 20+, FireFox 20+, Safari 6+, Opera 12+, IE8+
11 years ago
* No jQuery (but then need [Promise](https://gist.github.com/RubaXa/8501359) polyfill)
11 years ago
* More than 90% [test code coverage](http://rubaxa.github.io/Ply/tests/)
* Preloading content before displaying
* Smart focus on form elements
11 years ago
* [ES6 syntax](https://github.com/termi/es6-transpiler)
11 years ago
## Base usage
11 years ago
Include [ply.css](ply.css) in `<head/>` (optional)
11 years ago
```html
11 years ago
<link href="./ply.css" rel="stylesheet" type="text/css"/>
11 years ago
```
Create a dialog:
```js
11 years ago
Ply.dialog("alert", "Wow!").always(function (ui) {
11 years ago
ui.state; // true — "OK", false — "cancel"
11 years ago
ui.by; // submit, overlay, esc, "x"
});
//or
11 years ago
Ply.dialog("confirm", "Do you like it?")
.done(function (ui) {
// OK
})
.fail(function (ui) {
// Cancel
})
.always(function (ui) {
if (ui.state) {
// Clicked "OK"
}
else {
// Clicked "Cancel"
// details: `ui.by` — "cancel", "overlay", "esc", "x"
}
});
11 years ago
```
11 years ago
---
## Dialogs
```js
Ply.dialog({
"init-state": {
ui: "alert",
data: "Wow!",
next: "other-step"
nextEffect: "3d-flip[180,-180]"
},
"other-step": {
ui: "confirm",
data: {
text: "What's next?",
ok: "Exit", // button text
cancel: "Back"
},
back: "init-state",
backEffect: "3d-flip[-180,180]"
}
11 years ago
}).always(function (ui) {
11 years ago
if (ui.state) {
// OK
} else {
// Cancel
// ui.by — 'overlay', 'x', 'esc'
}
11 years ago
})
```
---
11 years ago
## Low-level
11 years ago
##### new Ply(el[, options])
* el:`String|HTMLElement` — content of layer
* options:`Object` — layer options
##### new Ply(options)
* options:`Object` — layer options
11 years ago
```js
var ply = new Ply({
el: "...", // HTML-content
11 years ago
effect: "fade", // or ["open-effect:duration", "close-effect:duration"]
11 years ago
layer: {}, // default css
overlay: { // defaults css
opacity: 0.6,
backgroundColor: "#000"
},
flags: { // defaults
11 years ago
closeBtn: true, // presence close button "✖"
11 years ago
bodyScroll: false, // disable scrollbar at body
closeByEsc: true, // close by press on `Esc` key
closeByOverlay: true, // close by click on the overlay
hideLayerInStack: true, // hide the layer if it is not the first in the stack
visibleOverlayInStack: false // visibility overlay, if the layer is not the first in the stack
11 years ago
},
// Callback
init: function () {},
open: function (ply) {},
close: function (ply) {},
destory: function (ply) {},
callback: function (ui) {},
});
// And
ply.open().then(function () {
ply.swap({ el: ".." }, "3d-flip").then(function () {
ply.close();
});
});
```
11 years ago
##### open([effect]):`Promise`
11 years ago
##### close(effect):`Promise`
11 years ago
##### swap(layer[, effect]):`Promise`
Swapping one layer to another
11 years ago
##### innerSwap(layer[, effect]):`Promise`
Swapping the content of one layer to another
##### destroy()
Destroy layer
11 years ago
11 years ago
---
11 years ago
11 years ago
## Preset effects
11 years ago
- fade
- scale
- fall
- slide
- 3d-flip
- 3d-sign
11 years ago
### Combined effects
11 years ago
```js
Ply.dialog("alert", { effect: ["fade", "scale"] }, "Fade & scale");
```
11 years ago
### Custom effects
```js
Ply.effects["my-effect"] = {
open: { layer: "fade-in", overlay: "background-in" },
close: { layer: "fade-out", overlay: "background-out" }
};
Ply.effects["background-in"] = {
"from": { opacity: 0, backgroundColor: "red" },
"to": { opacity: 1, backgroundColor: "white" }
};
Ply.effects["background-out"] = {
"from": { opacity: 1, backgroundColor: "white" },
"to": { opacity: 0, backgroundColor: "green" }
};
```
---
11 years ago
## Ply.stack
11 years ago
* last:`Ply|null`
* length:`Number`
11 years ago
11 years ago
---
## Ply.support
11 years ago
* transition:`String|Boolean`
* transform:`String|Boolean`
* perspective:`String|Boolean`
* transformStyle:`String|Boolean`
* transformOrigin:`String|Boolean`
* backfaceVisibility:`String|Boolean`
11 years ago
---
## Ply.lang (localization)
11 years ago
* ok:`String` — "OK"
11 years ago
* cancel:`String` — "Cancel"
11 years ago
* cross:`String` — "✖"
11 years ago
---
## Ply.defaults
11 years ago
* layer:`Object` — css
* overlay:`Object` — style overlay
* opacity:Number — default `0.6`
* backgroundColor:String — default `rgb(0, 0, 0)'`
11 years ago
* flags:`Object`
11 years ago
* closeBtn:`Boolean` — presence close button "✖"
* bodyScroll:Boolean — disable scrollbars, default `false`
11 years ago
* closeByEsc:`Boolean` — closing the layer by pressing the `esc` key, default `true`
* closeByOverlay:`Boolean` — closing the layer by clicking on the overlay, default `true`
11 years ago
* hideLayerInStack:`Boolean` — hide the layer if it is not the first in the stack
* visibleOverlayInStack:`Boolean` — visibility overlay, if the layer is not the first in the stack
11 years ago
---
## Ply.dom
11 years ago
##### build(tag:`String|Object`):`HTMLElement`
11 years ago
```js
Ply.build(); // <div/>
Ply.build("input"); // <input/>
Ply.build(".foo"); // <div class="foo"/>
Ply.build(".foo.bar"); // <div class="foo bar"/>
Ply.build({ // <input type="password" class="foo" style="padding: 10px" maxlength="32"/>
tag: "input.foo",
type: "password",
css: { padding: "10px" },
maxlength: 32
});
Ply.build({ text: "<i>?</i>" }); // <div>&lt;i&gt;?&lt;/i&gt;</div>
Ply.build({ html: "<i>!</i>" }); // <div><i>!</i></div>
```
11 years ago
##### append(parent:`HTMLElement`, el:`HTMLElement`)
11 years ago
11 years ago
##### remove(el:`HTMLElement`)
11 years ago
11 years ago
##### addEvent(el:`HTMLElement`, name:`String`, fn:`Function`)
11 years ago
11 years ago
##### removeEvent(el:`HTMLElement`, name:`String`, fn:`Function`)
11 years ago
---
## Create a dialog template
11 years ago
##### Ply.ui(name):`HTMLElement`
* name:`String` — ui-element name
```js
var el = Ply.ui("btn", {
title: "click me",
value: "Wow!"
})
```
##### Ply.ui.factory(name, factory)
11 years ago
* name:`String` — ui-element name
* factory:`Function` — callback
```js
Ply.ui.factory("btn", function (data, children) {
11 years ago
// data:`Object`
// children:`HTMLElement`
return {
"tag": ".btn",
"text": data.value
};
});
// or button with icon (optional)
Ply.ui.factory("btn", function (data, children) {
return {
"tag": ".btn",
"title": data.title
"children": [
data.icon && { "tag": "span.glyphicon.glyphicon-" + data.icon },
{ "tag": "span", "text": data.value }
]
};
});
```
##### Ply.factory(name, factory)
11 years ago
* name:`String` — template name
* factory:`Function` — callback
```js
Ply.factory("subscribe", function (options, data, resolve) {
// options — ply options
// data — user data
// resolve — done function
resolve({
"header": "Spam subscribe",
"content": {
"fieldset": {
"name": { label: "Username", value: data.name },
"email": { label: "E-mail", value: data.email },
"agree": true
}
},
ctrls: {
"ok": true,
"cancel": "abort" // for example
}
});
// OR
var element = template(data);
resolve(element);
});
Ply.ui.factory("fieldset", function (data, children) {
return {
tag: ".fieldset",
children: children
};
});
// Default element in `fieldset`
Ply.ui.factory("fieldset *", function (data) {
var uid = Math.round(Math.random() * 1e9).toString(36);
return {
tag: ".field",
children: [
{ tag: "label", forHtml: uid, text: data.label },
{ tag: "input", id: uid, name: data.name, value: data.value }
]
};
});
Ply.ui.factory("fieldset agree", function (data) {
var uid = Math.round(Math.random() * 1e9).toString(36);
return {
tag: ".field",
children: [
{ tag: "input", type: "checkbox", id: uid, name: "agree", value: "Y" },
{ tag: "label", forHtml: uid, text: "I agree." }
]
};
});
// Usage
Ply.dialog("subscribe", {
"name": "RubaXa",
"email": "trash@rubaxa.org",
});
```
11 years ago
---
## Development
* `grunt watch` — dev mode
* `grunt build` — assembly project
11 years ago
---
## Changelog
11 years ago
##### 0.4.0
* + 'always' method (using inheritance «Promise»).
* + Modularization
11 years ago
##### 0.3.0
* #1: Testing and documentation
* #3: Stack features