Skip to content

Commit

Permalink
Separate config file for server endpoints, added fuzzy search for nod…
Browse files Browse the repository at this point in the history
…e data, and basic code refactoring.
  • Loading branch information
chaitanyya committed Jun 16, 2017
1 parent 6a39482 commit 065d720
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 15 deletions.
20 changes: 20 additions & 0 deletions app/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const fuzzyConf = {
id: 'id',
shouldSort: true,
threshold: 0.3,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 3,
keys: [
"Q1",
"Q2",
"V1",
"V2"
]
};

export const serverConf = {
apiEndpoint: 'http://localhost:3000/api/project/',
previewEndpoint: 'http://localhost:4200/project?id='
}
87 changes: 75 additions & 12 deletions app/controllers/create.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/* global saveAs */
/* global saveAs, Fuse */

import Ember from 'ember';
import d3 from 'd3';
import _ from 'lodash';
import NProgress from 'ember-cli-nprogress';
import * as config from '../config';

export default Ember.Controller.extend({
charge: 6,

scale: 1,

fuzzyNodes: [],

fuzzyText: null,

firstCreate: true,
Expand Down Expand Up @@ -52,7 +55,28 @@ export default Ember.Controller.extend({
}.observes('labels'),

fuzzySearch: function () {
//
if (this.get('fuzzyText').length > 3) {
var fuse = new Fuse(this.get('fuzzyNodes'), config.fuzzyConf);

var results = fuse.search(this.get('fuzzyText'));

if (results.length) {
this.send(
'nodeFilter',
results
);
} else {
this.send(
'nodeFilter',
false
);
}
} else {
this.send(
'nodeFilter',
false
);
}
}.observes('fuzzyText'),

getNodes: function (frameType) {
Expand Down Expand Up @@ -120,18 +144,20 @@ export default Ember.Controller.extend({
that.get('selectedColumn')
.get('choice')
.forEach(function (type) {
var newNode = {};

// Use existing nodes for first choice.
if (first) {
first = false;

var newNode = {
newNode = {
id: nodeObject.id,
x: nodeObject.x,
y: nodeObject.y,
fill: nodeObject.fill
};
} else {
var newNode = {
newNode = {
id: nodeObject.id + '--' + type,
x: nodeObject.x,
y: nodeObject.y,
Expand Down Expand Up @@ -207,7 +233,7 @@ export default Ember.Controller.extend({
loadProject: function () {
var that = this;

var file = 'http://localhost:3000/api/project/' + this.get('projectId');
var file = config.serverConf.apiEndpoint + this.get('projectId');

Ember.$.get(file, function () {
that.send(
Expand Down Expand Up @@ -476,7 +502,10 @@ export default Ember.Controller.extend({
.createRecord('frame', frameData);

if (first) {
that.send('selectFrame', frame);
that.send(
'selectFrame',
frame
);
first = false;
}
}
Expand Down Expand Up @@ -546,17 +575,28 @@ export default Ember.Controller.extend({

d3.csv(file, function(error, rows) {
rows.forEach(function(row, index) {
var node = {};
var node = {
id: row.V1
};
var fuzzyNode = {};

if (index != 0) {
_.forOwn(row, function(value, key) {
if (value) {
node[key] = value;

// This is specific to Qualtrics and will be replaced in future.
if (_.isNaN(parseInt(value))) {
fuzzyNode[key] = value;
}
}
});

that.get('store')
var storeNode = that.get('store')
.createRecord('node', node);

fuzzyNode['id'] = storeNode.get('id');
that.get('fuzzyNodes').pushObject(fuzzyNode);
}
});
});
Expand Down Expand Up @@ -696,6 +736,31 @@ export default Ember.Controller.extend({
);
},

nodeFilter: function(nodes) {
if (nodes) {
var that = this;

d3.selectAll('circle.node')
.transition()
.duration(200)
.style("opacity", 0.3);

_.map(nodes, function(node) {
return d3.selectAll("[id^=" + node + "]")
.transition()
.duration(500)
.attr("r", that.get('radius') + 3)
.style("opacity", 1);
});
} else {
d3.selectAll('circle.node')
.transition()
.duration(200)
.attr("r", this.get('radius'))
.style("opacity", 0.7);
}
},

nodeInfo: function (nodeId) {
var info = [];

Expand Down Expand Up @@ -769,7 +834,6 @@ export default Ember.Controller.extend({
var center = foci[d[frame.get('id')]];

d.x += (center.x - d.x) * 0.06 * alpha;

d.y += (center.y - d.y) * 0.06 * alpha;
};
}
Expand All @@ -780,7 +844,6 @@ export default Ember.Controller.extend({
var center = that.get('firstFoci')[d.fill];

d.x += (center.x - d.x) * 0.02 * alpha;

d.y += (center.y - d.y) * 0.02 * alpha;
};
}
Expand Down Expand Up @@ -1215,12 +1278,12 @@ export default Ember.Controller.extend({
}

// Open connection.
request.open('POST', 'http://localhost:3000/api/project', true);
request.open('POST', config.serverConf.apiEndpoint, true);

// Show notification on success.
request.onreadystatechange = function () {
if (request.readyState === 4 && request.status === 200) {
var projectLink = 'http://localhost:4200/project?id=' + request.responseText;
var projectLink = config.serverConf.previewEndpoint + request.responseText;

that.send(
'showNotification',
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Ember from 'ember';
import d3 from 'd3';
import _ from 'lodash';
import NProgress from 'ember-cli-nprogress';
import * as config from '../config';

export default Ember.Controller.extend({
queryParams: ['id'],
Expand All @@ -22,7 +23,7 @@ export default Ember.Controller.extend({
Ember.run.next(this, function () {
var that = this;

var file = 'http://localhost:3000/api/project/' + this.get('id');
var file = config.serverConf.apiEndpoint + this.get('id');

Ember.$.get(file, function () {
that.send('loadPorject', file);
Expand Down
1 change: 0 additions & 1 deletion app/models/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
// import attr from 'ember-data/attr';
// import { belongsTo, hasMany } from 'ember-data/relationships';

Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"material-design-lite": "1.3.0",
"animate.css": "^3.5.2",
"nprogress": "alexlafroscia/nprogress#allow-alternate-scheduler",
"file-saver": "^1.3.3"
"file-saver": "^1.3.3",
"fuse.js": "^3.0.5"
}
}
1 change: 1 addition & 0 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function (defaults) {
app.import('vendor/font-awesome.css');
app.import('bower_components/file-saver/FileSaver.js');
app.import('bower_components/animate.css/animate.css');
app.import('bower_components/fuse.js/dist/fuse.js');

return app.toTree();
};

0 comments on commit 065d720

Please sign in to comment.