-
Notifications
You must be signed in to change notification settings - Fork 1
/
app-command-behavior.html
125 lines (101 loc) · 3.58 KB
/
app-command-behavior.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!--
@license
Copyright (c) 2017 The expand.js authors. All rights reserved.
This code may only be used under the BSD style license found at https://expandjs.github.io/LICENSE.txt
The complete set of authors may be found at https://expandjs.github.io/AUTHORS.txt
The complete set of contributors may be found at https://expandjs.github.io/CONTRIBUTORS.txt
-->
<!--
A behavior used to add command capabilities on a web component.
@behavior app-command-behavior
@since 1.0.0
@category behaviors
@description A behavior used to add command capabilities on a web component
@keywords expandjs, web app, web components
@homepage https://expandjs.com/components/app-command-behavior
@repository https://github.com/expandjs/app-behaviors
@source https://github.com/expandjs/app-behaviors/blob/master/app-command-behavior.html
@behavior xp-master-slave-behavior /bower_components/xp-elements/xp-master-slave-behavior.html
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../expandjs/expandjs.html">
<link rel="import" href="../xp-elements/xp-master-slave-behavior.html">
<script>
/**
* @polymerBehavior Polymer.APPCommandBehaviorImp
*/
Polymer.APPCommandBehaviorImp = {
// CLASSES
classes: ['command'],
/*********************************************************************/
/**
* Executes the command.
*
* @method execute
* @param {Object} [options]
* @param {Function} [callback]
*/
execute(strategy, options, callback) {
// Preparing
if (XP.isFunction(options)) { callback = options; options = {}; }
if (XP.isVoid(callback)) { callback = function () {}; }
// Asserting
XP.assertArgument(XP.isVoid(options) || XP.isObject(options), 1, 'Object');
XP.assertArgument(XP.isFunction(callback), 2, 'Function');
// Calling
return strategy.call(this, options || {}, callback);
},
/*********************************************************************/
/**
* Must be redefined to set the `execute` strategy.
*
* @method _execute
* @param {Object} options
* @param {Function} callback
* @abstract
*/
_execute(options, callback) {
// Callback
callback(new XP.AbstractError('_execute'));
},
/*********************************************************************/
// PROPERTIES
properties: {
/**
* The command's name.
*
* @attribute name
* @type string
*/
name: {
type: String,
value() { return XP.camelCase(this.tagName.slice(this.tagName.indexOf('-') + 1)); }
},
/**
* The application's store.
*
* @attribute store
* @type Element
* @readonly
*/
store: {
master: '.store',
observer: '__storeChanged',
readOnly: true
}
},
/*********************************************************************/
// OBSERVER
__storeChanged(post) {
// Setting
if (post) { post[this.name] = this.execute.bind(post, this._execute); }
}
};
/**
* @polymerBehavior Polymer.APPCommandBehavior
*/
Polymer.APPCommandBehavior = [
Polymer.XPMasterSlaveBehavior,
Polymer.APPCommandBehaviorImp
];
</script>