-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
377 lines (363 loc) · 11.2 KB
/
index.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>D e p a l p a t o r</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.7.1/jquery.contextMenu.min.css" rel="stylesheet">
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.css" rel="stylesheet">
<style type="text/css">
html, body {
font: normal 16px/26px "Roboto", Arial, sans-serif;
font-size: 14px;
margin: 0;
}
.ui-button {
margin: 4px;
}
.find-symbol, .find-symbol:hover {
background: orange;
}
.known-symbol, .known-symbol:hover {
background: lime;
}
#header {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
overflow-y: scroll;
}
#app {
height: 100vh;
}
#network {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
#network-settings {
display: block;
position: absolute;
top: 10px;
right: 15px;
}
#conf {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
overflow-y: auto;
}
#conf > p {
text-align: center;
}
#conf > p > span {
font-size: 18px;
}
.item {
box-sizing: border-box;
border-top: 1px solid #eee;
padding-bottom: 5px;
padding-top: 5px;
display: flex;
align-items: center;
}
.item[is_required] {
background: lime;
}
.item > span {
font-size: 16px;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vue-split-pane.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.7.1/jquery.contextMenu.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.7.1/jquery.ui.position.js"></script>
<script src="https://unpkg.com/[email protected]/dist/graphlib.min.js"></script>
<script>
$(function() {
var all_groups = [];
var app_data = { symbols: [], formulas: [] };
$.ajax({
url: 'graph_data/groups.xml',
dataType: 'xml',
success: function(xmlData){
$(xmlData).find('group').each(function() {
all_groups.push({
id: parseInt($(this).find('id').text()),
image: $(this).find('image').text(),
symbols: $(this).find('name').text().split(', '),
excluded: false
});
});
}
});
var app = new Vue({
el: '#app',
data: app_data,
mounted: function() {
$('input[type=checkbox]').checkboxradio();
$('input[name=depth-group]').checkboxradio({ icon: false });
$('input[name=depth-group]').click(function() {
var obj = $('input[name=radio-1]:checked');
if (obj.length == 0)
return;
vis_data.nodes.clear();
vis_data.edges.clear();
used = [];
MAX_DEPTH = parseInt($('input[name=depth-group]:checked').prev().text()) | 2;
do_graph($('input[name=radio-1]:checked').next().text());
});
},
created: function() {
$.contextMenu({
selector: '.item',
callback: function(key, options) {
if (key == 'exclude') {
var formula_id = parseInt($(this).attr('formula'));
for (let group of all_groups) {
if (group.id == formula_id) {
group.excluded = true;
break;
}
}
solve();
}
},
items: {
exclude: { name: "Исключить из решения" }
}
});
$.contextMenu({
selector: '.context-menu-one',
callback: function(key, options) {
//console.log($(this).text());
if (key == 'find') {
$(this).removeClass('known-symbol', 100);
$(this).toggleClass('find-symbol', 100);
} else {
$(this).removeClass('find-symbol', 100);
$(this).toggleClass('known-symbol', 100);
}
},
items: {
find: { name: "Нужно найти" },
known: { name: "Известен" }
}
});
$.ajax({
url: 'data/symbols.xml',
dataType: 'xml',
success: function(xmlData){
$(xmlData).find('symbol').each(function() {
app_data.symbols.push($(this).find('name').text());
});
Vue.nextTick(function () {
$('input[name=radio-1]').checkboxradio({ icon: false });
$('input[name=radio-1]').click(function() {
vis_data.nodes.clear();
vis_data.edges.clear();
used = [];
MAX_DEPTH = parseInt($('input[name=depth-group]:checked').prev().text()) | 2;
do_graph($(this).next().text());
});
});
}
});
}
});
var MAX_DEPTH = 2;
var used = null;
function do_graph(key, parent=null, gr=null, depth=0) {
//console.log(key, parent, gr, depth);
var node = 0;
if (parent == null) {
vis_data.nodes.add({
id: node,
shape: 'box',
label: key,
level: depth
});
} else {
node = used.indexOf(gr.id) + 1;
if (node == 0) {
used.push(gr.id);
node = used.indexOf(gr.id) + 1;
vis_data.nodes.add({
id: node,
shape: 'image',
image: gr.image,
level: depth
});
}
}
if (parent !== null) {
vis_data.edges.add({
from: parent,
to: node
});
}
if (depth >= MAX_DEPTH) return;
all_groups.forEach(function(group) {
if (group === gr) return;
if (group.symbols.indexOf(key) != -1) {
group.symbols.forEach(function(symbol) {
if (symbol == key) return;
do_graph(symbol, node, group, depth + 1);
});
}
});
}
function solve() {
app_data.formulas = [];
var formulas = [];
var known = $.map($('.known-symbol'), function(element) {
return $(element).text();
});
var find = $.map($('.find-symbol'), function(element) {
return $(element).text();
});
var do1 = true;
while (do1) {
let do2 = true;
for (let group of all_groups) {
if (group.excluded) continue;
let diff = $(group.symbols).not($(known));
if (diff.length != 1) continue;
known.push(diff[0]);
formulas.push({
group: group,
symbol: diff[0],
is_required: find.indexOf(diff[0]) > -1
});
do2 = false;
if ($(find).not($(known)).length == 0) {
do1 = false;
break;
}
}
if (do2) {
break;
}
}
var minimize = $('input[name=checkbox-min]').is(':checked');
if (!minimize) {
app_data.formulas = formulas;
return;
}
// remove extra formulas
var g = new graphlib.Graph({multigraph: true});
for (let formula of formulas) {
for (let symbol of formula.group.symbols) {
if (formula.symbol != symbol) {
g.setNode(formula.symbol);
g.setNode(symbol);
g.setEdge(formula.symbol, symbol, formula.group.id);
}
}
}
var nodes = g.nodes();
var edges = [];
for (let fsymbol of find) {
let d = graphlib.alg.dijkstra(g, fsymbol);
for (let symbol of nodes) {
if (d[symbol].distance == 0)
continue;
if (d[symbol].distance != Number.POSITIVE_INFINITY) {
edges.push(g.edge({v: d[symbol].predecessor, w: symbol}));
}
}
}
for (let formula of formulas)
if (edges.indexOf(formula.group.id) > -1)
app_data.formulas.push(formula);
}
$('#solve').click(function() {
for (let group of all_groups) {
group.excluded = false;
}
solve();
});
var vis_data = {
nodes: new vis.DataSet([]),
edges: new vis.DataSet([])
};
// create a network
var container = document.getElementById('network');
var options = {
layout: {
hierarchical: {
enabled: true,
levelSeparation: 150,
nodeSpacing: 250
}
},
edges: {
arrows: 'to'
},
nodes: {
shapeProperties: {
useImageSize: true,
useBorderWithImage: true
}
},
physics: {
enabled: false
}
};
var network = new vis.Network(container, vis_data, options);
});
</script>
</head>
<body>
<div id="app">
<split-pane split="horizontal" :min-percent='15' :default-percent='25'>
<div id="header" slot="paneL">
<template v-for="(symbol, index) in symbols">
<input type="radio" name="radio-1" :id="'radio-'+index">
<label class="context-menu-one" :for="'radio-'+index">{{symbol}}</label>
</template>
</div>
<split-pane slot="paneR" split="vertical" :min-percent='20' :default-percent='65'>
<div slot="paneL">
<div id="network"></div>
<div id="network-settings">
<fieldset>
<legend>Глубина рекурсии: </legend>
<label for="depth-2">2</label>
<input type="radio" name="depth-group" id="depth-2" checked>
<label for="depth-3">3</label>
<input type="radio" name="depth-group" id="depth-3">
<label for="depth-4">4</label>
<input type="radio" name="depth-group" id="depth-4">
</fieldset>
</div>
</div>
<div id="conf" slot="paneR">
<p>
<span>Выберите символы правым кликом.</span><br>
<label for="checkbox-min">Минимизировать решение</label>
<input type="checkbox" name="checkbox-min" id="checkbox-min">
<button id="solve" class="ui-button ui-widget ui-corner-all">Построить решение</button>
</p>
<div style="padding-left: 5px">
<template v-for="f in formulas">
<div class="item" :is_required="f.is_required" :formula="f.group.id">
<img :src="f.group.image" /><span> => {{f.symbol}}</span>
</div>
</template>
</div>
</div>
</split-pane>
</split-pane>
</div>
</body>
</html>