-
Notifications
You must be signed in to change notification settings - Fork 0
/
jviz-queue.html
165 lines (136 loc) · 4.44 KB
/
jviz-queue.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!--
@license
Copyright (c) 2016 The Jviz Project Authors. All rights reserved.
The Jviz Project is under the MIT License. See https://github.com/jviz/jviz/blob/dev/LICENSE
-->
<!-- Import dependencies -->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../jviz/jviz.html">
<link rel="import" href="../jviz-styles/jviz-styles.html">
<!-- Import queue elements -->
<link rel="import" href="./jviz-queue-item.html">
<!-- Queue element -->
<dom-module id="jviz-queue">
<template>
<style>
:host
{
display: block;
}
:host .header
{
display: block;
@apply --jviz-heading-3;
line-height: 50px !important;
height: 50px !important;
margin: 0px !important;
}
</style>
<template is="dom-if" if="{{ header }}">
<div class="header">{{ header }}</div>
</template>
<div id="tasks">
<template id="list" is="dom-repeat" items="{{ tasks }}" index-as="index">
<jviz-queue-item id$="{{ index }}" header$="{{ value(item, index, tasks, numTasks) }}"></jviz-queue-item>
</template>
</div>
</template>
</dom-module>
<!-- Queue logic -->
<script>
//Initialize the queue object
var jviz_queue = { is: 'jviz-queue' };
//Initialize the queue properties
jviz_queue.properties = {};
jviz_queue.properties.visible = { type: Boolean, reflectToAttribute: true, value: true };
jviz_queue.properties.header = { type: String, reflectToAttribute: true, value: '' };
jviz_queue.properties.running = { type: Boolean, reflectToAttribute: true, value: false };
jviz_queue.properties.parse = { type: Function };
jviz_queue.properties.numTasks = { type: Number, value: 0 };
jviz_queue.properties.tasks = { type: Array, value: [], observer: 'reload' };
//Reload method
jviz_queue.reload = function()
{
//Update the number of tasks
this.numTasks = this.tasks.length;
//Render the tasks list again
this.$.list.render();
};
//Reset method
jviz_queue.reset = function()
{
//Reset the tasks list
this.tasks = [];
//Reload the element
this.reload();
};
//Display the task name
jviz_queue.value = function(item, index)
{
//Check the parse method
return (typeof this.parse === 'function') ? this.parse(item, index) : item.toString();
};
//Run the queue
jviz_queue.run = function(handler)
{
//Check the callback element
if(typeof handler !== 'function'){ return console.error('No tasks handler method provided. Queue aborted.'); }
//Save this
var self = this;
//Reload the element
this.reload();
//Set the time out
//This will give time to initialize all jviz-queue-items elements
return jviz.time_out(100, function()
{
//Check if the queue is running
if(self.running === true){ return console.warn('Queue is running'); }
//Check the number of tasks
if(self.numTasks === 0){ return console.warn('No tasks to run'); }
//Update the running value
self.running = true;
//Get the tasks elements list
var tasks = self.$.tasks.querySelectorAll('jviz-queue-item');
//Reset all tasks to waiting status
tasks.forEach(function(el){ el.status = 'waiting'; });
//Emit the queue start event
self.fire('queue-started', {});
//Read the tasks recursive
var tasks_read = function(index)
{
//Check the index
if(index >= tasks.length)
{
//Set running as false
self.running = false;
//Display done in console
//console.info('Queue completed');
//Trigger the completed event
return self.fire('queue-completed', {});
}
//Get the task object
var item = self.tasks[index];
//Run this tasks
return handler(index, item, tasks[index], function(cont)
{
//Check no continue value
if(typeof cont === 'Boolean' && cont === false)
{
//Set queue running as false
self.running = false;
//Display queue aborted in console
//console.warn('Queue aborted');
//Trigger the queue aborted event
return self.fire('queue-aborted', {});
}
//Continue with the next task on the queue
return tasks_read(index + 1);
});
};
//Read and run the tasks
return tasks_read(0);
});
};
//Register the queue element
Polymer(jviz_queue);
</script>