simplyview 2.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,189 +0,0 @@
1
- (function(global) {
2
- 'use strict';
3
-
4
- function etag() {
5
- let d = '';
6
- while (d.length < 32) d += Math.random().toString(16).substr(2);
7
- const vr = ((parseInt(d.substr(16, 1), 16) & 0x3) | 0x8).toString(16);
8
- return `${d.substr(0, 8)}-${d.substr(8, 4)}-4${d.substr(13, 3)}-${vr}${d.substr(17, 3)}-${d.substr(20, 12)}`;
9
- }
10
-
11
- function ViewModel(name, data, options) {
12
- this.name = name;
13
- this.data = data || [];
14
- this.data.etag = etag();
15
- this.view = {
16
- options: {},
17
- data: [] //Array.from(this.data).slice()
18
- };
19
- this.options = options || {};
20
- this.plugins = {
21
- start: [],
22
- select: [],
23
- order: [],
24
- render: [],
25
- finish: []
26
- };
27
- }
28
-
29
- ViewModel.prototype.update = function(params) {
30
- if (!params) {
31
- params = {};
32
- }
33
- if (params.data) {
34
- // this.data is a reference to the data passed, so that any changes in it will get applied
35
- // to the original
36
- this.data = params.data;
37
- this.data.etag = etag()
38
- }
39
- // the view is a shallow copy of the array, so that changes in sort order and filtering
40
- // won't get applied to the original, but databindings on its children will still work
41
- this.view.data = Array.from(this.data).slice();
42
- this.view.data.etag = this.data.etag;
43
- let data = this.view.data;
44
- let plugins = this.plugins.start.concat(this.plugins.select, this.plugins.order, this.plugins.render, this.plugins.finish);
45
- plugins.forEach(plugin => {
46
- data = plugin.call(this, params, data);
47
- if (!data) {
48
- data = this.view.data;
49
- }
50
- this.view.data = data
51
- });
52
-
53
- if (global.editor) {
54
- global.editor.addDataSource(this.name,{
55
- load: function(el, callback) {
56
- callback(self.view.data);
57
- }
58
- });
59
- updateDataSource(this.name);
60
- }
61
- };
62
-
63
- ViewModel.prototype.addPlugin = function(pipe, plugin) {
64
- if (typeof this.plugins[pipe] == 'undefined') {
65
- throw new Error('Unknown pipeline '+pipe);
66
- }
67
- this.plugins[pipe].push(plugin);
68
- };
69
-
70
- ViewModel.prototype.removePlugin = function(pipe, plugin) {
71
- if (typeof this.plugins[pipe] == 'undefined') {
72
- throw new Error('Unknown pipeline '+pipe);
73
- }
74
- this.plugins[pipe] = this.plugins[pipe].filter(function(p) {
75
- return p != plugin;
76
- });
77
- };
78
-
79
- var updateDataSource = function(name) {
80
- global.document.querySelectorAll('[data-simply-data="'+name+'"]').forEach(function(list) {
81
- global.editor.list.applyDataSource(list, name);
82
- });
83
- };
84
-
85
- var createSort = function(options) {
86
- var defaultOptions = {
87
- name: 'sort',
88
- getSort: function(params) {
89
- return Array.prototype.sort;
90
- }
91
- };
92
- options = Object.assign(defaultOptions, options || {});
93
-
94
- return function(params) {
95
- this.options[options.name] = options;
96
- if (params[options.name]) {
97
- options = Object.assign(options, params[options.name]);
98
- }
99
- this.view.data.sort(options.getSort.call(this, options));
100
- };
101
- };
102
-
103
- var createPaging = function(options) {
104
- var defaultOptions = {
105
- name: 'paging',
106
- page: 1,
107
- pageSize: 100,
108
- max: 1,
109
- prev: 0,
110
- next: 0
111
- };
112
- options = Object.assign(defaultOptions, options || {});
113
-
114
- return function(params) {
115
- this.options[options.name] = options;
116
- if (this.view.data) {
117
- options.max = Math.max(1, Math.ceil(Array.from(this.view.data).length / options.pageSize));
118
- } else {
119
- options.max = 1;
120
- }
121
- if (this.view.changed) {
122
- options.page = 1; // reset to page 1 when something in the view data has changed
123
- }
124
- if (params[options.name]) {
125
- options = Object.assign(options, params[options.name]);
126
- }
127
- options.page = Math.max(1, Math.min(options.max, options.page)); // clamp page nr
128
- options.prev = options.page - 1; // calculate previous page, 0 is allowed
129
- if (options.page<options.max) {
130
- options.next = options.page + 1;
131
- } else {
132
- options.next = 0; // no next page
133
- }
134
-
135
- var start = (options.page - 1) * options.pageSize;
136
- var end = start + options.pageSize;
137
-
138
- this.view.data = this.view.data.slice(start, end);
139
- };
140
- };
141
-
142
- var createFilter = function(options) {
143
- var defaultOptions = {
144
- name: 'filter',
145
- label: 'A filter',
146
- getMatch: function(entry) {
147
- return false;
148
- }
149
- };
150
- options = Object.assign(defaultOptions, options || {});
151
- if (options.init) {
152
- options.init.call(this, options);
153
- }
154
- return function(params) {
155
- this.options[options.name] = options;
156
- if (params[options.name]) {
157
- options = Object.assign(options, params[options.name]);
158
- }
159
- var match = options.getMatch.call(this, options);
160
- if (match) {
161
- options.enabled = true;
162
- this.view.data = this.view.data.filter(match);
163
- } else if (options.enabled) {
164
- options.enabled = false;
165
- }
166
- }
167
- }
168
-
169
- var viewmodel = {
170
- create: function(name, data, options) {
171
- return new ViewModel(name, data, options);
172
- },
173
- createFilter: createFilter,
174
- createSort: createSort,
175
- createPaging: createPaging,
176
- updateDataSource: updateDataSource,
177
- etag
178
- };
179
-
180
- if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
181
- module.exports = viewmodel;
182
- } else {
183
- if (!global.simply) {
184
- global.simply = {};
185
- }
186
- global.simply.viewmodel = viewmodel;
187
- }
188
-
189
- })(this);