summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunternif <hunternif@gmail.com>2019-05-07 23:14:23 +0700
committerHunternif <hunternif@gmail.com>2019-05-07 23:14:23 +0700
commitf1581f8ff0a8bb2a7cefdb444841f641ced34363 (patch)
treecc11ba5444a0817e5ecef7ff2dfea3c993c276e5
parent962307a89c435236256d05fff5a3a1112c2e7f9d (diff)
client: save post id in metric sorter url
-rw-r--r--client/css/metric-sorter-view.styl2
-rw-r--r--client/html/metric_header.tpl2
-rw-r--r--client/html/metric_sorter.tpl4
-rw-r--r--client/js/controllers/metric_sorter_contoller.js44
-rw-r--r--client/js/controls/metric_header_control.js2
-rw-r--r--client/js/util/views.js4
6 files changed, 38 insertions, 20 deletions
diff --git a/client/css/metric-sorter-view.styl b/client/css/metric-sorter-view.styl
index 2d7b94e..5388c6e 100644
--- a/client/css/metric-sorter-view.styl
+++ b/client/css/metric-sorter-view.styl
@@ -48,7 +48,7 @@
@media (max-width: 1000px)
display: flex
- .done-btn
+ .save-btn
margin: auto
margin-right: 1em
.skip-btn
diff --git a/client/html/metric_header.tpl b/client/html/metric_header.tpl
index 59fe038..a711575 100644
--- a/client/html/metric_header.tpl
+++ b/client/html/metric_header.tpl
@@ -7,5 +7,5 @@
checked: ctx.showValuesOnPost,
class: 'append'}) %>
<a class='mousetrap button append close sorting'
- href="<%= ctx.getMetricSorterUrl(ctx.parameters) %>">Start sorting</a>
+ href="<%= ctx.getMetricSorterUrl('random', ctx.parameters) %>">Start sorting</a>
</form>
diff --git a/client/html/metric_sorter.tpl b/client/html/metric_sorter.tpl
index 79bf90e..2641297 100644
--- a/client/html/metric_sorter.tpl
+++ b/client/html/metric_sorter.tpl
@@ -9,7 +9,7 @@
<div class='sorting-buttons'>
<div class='compare-block'>
<% if (window.innerWidth <= 1000) { %>
- <input class='mousetrap done-btn' type='submit' value='Done'>
+ <input class='mousetrap save-btn' type='submit' value='Done'>
<% } %>
<button class='compare left-lt-right'>
<i class='fa fa-less-than'></i>
@@ -31,7 +31,7 @@
<div class='buttons'>
<% if (window.innerWidth > 1000) { %>
- <input class='mousetrap done-btn' type='submit' value='Done'>
+ <input class='mousetrap save-btn' type='submit' value='Save'>
<a href class='mousetrap append skip-btn'>Skip</a>
<% } %>
</div>
diff --git a/client/js/controllers/metric_sorter_contoller.js b/client/js/controllers/metric_sorter_contoller.js
index 02e68f9..e7a8d76 100644
--- a/client/js/controllers/metric_sorter_contoller.js
+++ b/client/js/controllers/metric_sorter_contoller.js
@@ -1,7 +1,10 @@
'use strict';
const api = require('../api.js');
+const router = require('../router.js');
+const views = require('../util/views.js');
const topNavigation = require('../models/top_navigation.js');
+const Post = require('../models/post.js');
const PostMetric = require('../models/post_metric.js');
const PostMetricRange = require('../models/post_metric_range.js');
const PostList = require('../models/post_list.js');
@@ -42,21 +45,36 @@ class MetricSorterController {
this._view.addEventListener('skip', e => this._evtSkip(e));
this._view.addEventListener('changeMetric', e => this._evtChangeMetric(e));
- this.startSortingNewPost();
+ if (ctx.parameters.id === 'random') {
+ this.startSortingRandomPost();
+ } else {
+ this.startSortingPost(ctx.parameters.id);
+ }
}
- startSortingNewPost() {
+ startSortingPost(id) {
this._view.clearMessages();
this._foundExactValue = false;
- this._getRandomUnsortedPost().then(unsortedPost => {
- this._unsortedPost = unsortedPost;
- this._view.installLeftPost(unsortedPost);
+ Post.get(id).then(post => {
+ this._unsortedPost = post;
+ this._view.installLeftPost(post);
this.reloadMedianPost();
}).catch(error => {
this._view.showError(error.message)
});
}
+ startSortingRandomPost() {
+ this._view.clearMessages();
+ this._getRandomUnsortedPostId().then(id => {
+ this._ctx.parameters.id = id;
+ router.replace(views.getMetricSorterUrl(id, this._ctx.parameters));
+ this.startSortingPost(id);
+ }).catch(error => {
+ this._view.showError(error.message)
+ });
+ }
+
reloadMedianPost() {
const metricName = this._primaryMetricName;
let range = this._getOrCreateRange(this._unsortedPost, metricName);
@@ -77,7 +95,7 @@ class MetricSorterController {
});
}
- _getRandomUnsortedPost() {
+ _getRandomUnsortedPostId() {
let unsetMetricsQuery = this._metricNames
.map(m => `${m} -metric:${m}`)
.join(' ');
@@ -85,18 +103,18 @@ class MetricSorterController {
let unsetFullQuery = `${filterQuery} ${unsetMetricsQuery} sort:random`;
return PostList.search(unsetFullQuery,
- this._ctx.parameters.skips || 0, 1, []).then(response => {
+ this._ctx.parameters.skips || 0, 1, ['id']).then(response => {
if (!response.results.length) {
return Promise.reject(new Error('No posts found'));
} else {
- return Promise.resolve(response.results.at(0));
+ return Promise.resolve(response.results.at(0).id);
}
});
}
_tryGetMedianPost(metric, range) {
- let low = range.low + 0.000001;
- let high = range.high - 0.000001;
+ let low = range.low + 0.000000001;
+ let high = range.high - 0.000000001;
let median_query = `metric-${metric}:${low}..${high} sort:metric-${metric}`;
return PostList.getMedian(median_query, []).then(response => {
return Promise.resolve({
@@ -141,7 +159,7 @@ class MetricSorterController {
}
this._unsortedPost.save().then(() => {
if (this._foundExactValue) {
- this.startSortingNewPost();
+ this.startSortingRandomPost();
} else {
this.reloadMedianPost();
}
@@ -152,7 +170,7 @@ class MetricSorterController {
_evtSkip(e) {
this._ctx.parameters.skips = (this._ctx.parameters.skips || 0) + 1;
- this.startSortingNewPost();
+ this.startSortingRandomPost();
}
_evtChangeMetric(e) {
@@ -162,7 +180,7 @@ class MetricSorterController {
module.exports = router => {
router.enter(
- ['posts', 'metric-sorter'],
+ ['post', ':id', 'metric-sorter'],
(ctx, next) => {
ctx.controller = new MetricSorterController(ctx);
});
diff --git a/client/js/controls/metric_header_control.js b/client/js/controls/metric_header_control.js
index 58d77ca..9f62e1f 100644
--- a/client/js/controls/metric_header_control.js
+++ b/client/js/controls/metric_header_control.js
@@ -73,7 +73,7 @@ class MetricHeaderControl extends events.EventTarget {
_refreshStartSortingBtn() {
let btn = this._hostNode.querySelector('a.sorting');
btn.hidden = !this._selectedMetrics.length;
- btn.setAttribute('href', views.getMetricSorterUrl(this._ctx.parameters));
+ btn.setAttribute('href', views.getMetricSorterUrl('random', this._ctx.parameters));
}
}
diff --git a/client/js/util/views.js b/client/js/util/views.js
index 5057d7d..169a6a0 100644
--- a/client/js/util/views.js
+++ b/client/js/util/views.js
@@ -191,9 +191,9 @@ function getPostEditUrl(id, parameters) {
} : {});
}
-function getMetricSorterUrl(parameters) {
+function getMetricSorterUrl(id, parameters) {
return uri.formatClientLink(
- 'posts', 'metric-sorter',
+ 'post', id, 'metric-sorter',
parameters ? {
query: parameters.query,
metrics: parameters.metrics,