Skip to content

Instantly share code, notes, and snippets.

@olivermt
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivermt/9622251 to your computer and use it in GitHub Desktop.
Save olivermt/9622251 to your computer and use it in GitHub Desktop.
/*--- QUERY ---*/
self.query = {
projects: ko.observableArray(<%= projectIDs %>), //we start with all projects checked
outcrops: ko.observableArray([]),
grossDepositionalEnvironment: ko.observable(""),
depositionalEnvironment: ko.observable(""),
mainClimate: ko.observableArray([]),
depositionalSubEnvironment: ko.observable(""),
architecturalElement: ko.observable(""),
//filters
basinType: ko.observable(),
formation: ko.observable(), //need to figure out something smart with multiple formations some sort of contains / in
country: ko.observable(),
//quality filters
filters: {
publicationType: ko.observable(),
outcropQuality: ko.observable(),
structuralComplexity: ko.observable(),
outcrop3Dcontrol: ko.observable(),
spatialObservationAndSamplingType: ko.observable()
}
};
//this shit aint no worky work
$.map(self.query.filters, function (value, filter) {
self.query.filters[filter].subscribe(function (newValue) {
self.loadOutcrops();
});
});
(function ($) {
var archiTecturalSearchViewModel = function () {
var self = this;
/*--- TARGET URLS ---*/
self.baseUrl = "${g.createLink(controller: "browse", absolute: true)[0..-6]}";
%{--slice to remove /index--}%
self.outcropUrl = "${g.createLink(controller: "browse", action: "outcrop", absolute: true)}/";
%{--slice to remove /index--}%
/*--- CONSTRAINT HOLDERS ---*/
//general structure
self.projectList = ko.observableArray(<%= projects %>);
self.gdeList = ko.observableArray();
self.depEnvList = ko.observableArray([]);
self.subEnvList = ko.observableArray([]);
self.archiEleList = ko.observableArray([]);
self.climateList = ko.observableArray([]);
self.outcropList = ko.observableArray([]);
//filters
self.basinTypeList = ko.observableArray([]);
self.formationList = ko.observableArray([]);
self.countryList = ko.observableArray([]);
//quality filters
self.spatialObservationAndSamplingTypeList = ko.observableArray([]);
self.publicationTypeList = ko.observableArray([]);
self.outcropQualityList = ko.observableArray([]);
self.structuralComplexityList = ko.observableArray([]);
self.outcrop3DcontrolList = ko.observableArray([]);
/*--- QUERY ---*/
self.query = {
projects: ko.observableArray(<%= projectIDs %>), //we start with all projects checked
outcrops: ko.observableArray([]),
grossDepositionalEnvironment: ko.observable(""),
depositionalEnvironment: ko.observable(""),
mainClimate: ko.observableArray([]),
depositionalSubEnvironment: ko.observable(""),
architecturalElement: ko.observable(""),
//filters
basinType: ko.observable(),
formation: ko.observable(), //need to figure out something smart with multiple formations some sort of contains / in
country: ko.observable(),
//quality filters
filters: {
publicationType: ko.observable(),
outcropQuality: ko.observable(),
structuralComplexity: ko.observable(),
outcrop3Dcontrol: ko.observable(),
spatialObservationAndSamplingType: ko.observable()
}
};
/*--- LISTENER HIERARCHY ---*/
self.lastTouched = "";
self.query.projects.subscribe(function (newValue) {
self.loadGde();
switch (self.lastTouched) {
case "grossDepositionalEnvironment":
self.loadDepEnv();
case "depositionalEnvironment":
self.loadSubEnv();
case "depositionalSubEnvironment":
self.loadArchiEle();
}
});
self.query.grossDepositionalEnvironment.subscribe(function (newValue) {
self.query.depositionalEnvironment("");
self.lastTouched = "grossDepositionalEnvironment";
self.loadDepEnv();
self.query.mainClimate([]);
self.loadClimate();
self.loadOutcrops();
});
self.query.depositionalEnvironment.subscribe(function (newValue) {
self.query.depositionalSubEnvironment("");
self.lastTouched = "depositionalEnvironment";
self.loadSubEnv();
self.loadOutcrops();
});
self.query.mainClimate.subscribe(function (newValue) {
self.lastTouched = "mainClimate";
self.query.depositionalSubEnvironment("");
self.loadArchiEle();
self.loadOutcrops();
});
self.query.depositionalSubEnvironment.subscribe(function (newValue) {
self.query.architecturalElement("");
self.lastTouched = "depositionalSubEnvironment";
self.loadArchiEle();
self.loadOutcrops();
});
self.query.architecturalElement.subscribe(function (newValue) {
self.lastTouched = "depositionalSubEnvironment";
$.map(self.query.filters, function (value, filter) {
self.query.filters[filter]("");
});
self.loadFilters();
self.loadOutcrops();
});
$.map(self.query.filters, function (value, filter) {
self.query.filters[filter].subscribe(function (newValue) {
self.loadOutcrops();
});
});
self.query.outcrops.subscribe(function (newValue) {
switch (self.lastTouched) {
case "grossDepositionalEnvironment":
self.loadDepEnv();
break;
case "depositionalEnvironment":
self.loadSubEnv();
break;
case "depositionalSubEnvironment":
self.loadArchiEle();
break;
case "architecturalElement":
$.map(self.filters, function (value, filter) {
self.query.filters[filter]("");
});
self.loadFilters();
break;
}
});
/*--- SPECIAL HANDLERS ---*/
self.resetOutcrops = function () {
self.query.outcrops([]);
self.loadOutcrops();
};
self.selectAllOutcrops = function () {
$('[name="outcrops"]').trigger('click');
};
/*--- LOADERS ---*/
self.loadGde = function () {
$.post(
self.baseUrl + 'grossDepositionalEnvironment',
compileQuery("grossDepositionalEnvironment"),
function (data) {
self.gdeList(ko.toJS(data));
},
"json"
)
};
self.loadDepEnv = function () {
$.post(
self.baseUrl + 'depositionalEnvironment',
compileQuery("depositionalEnvironment"),
function (data) {
self.depEnvList(ko.toJS(data));
}
)
};
self.loadClimate = function () {
$.post(
self.baseUrl + 'climateFilter',
compileQuery("mainClimate"),
function (data) {
self.climateList(ko.toJS(data));
}
)
};
self.loadSubEnv = function () {
$.post(
self.baseUrl + 'subEnvironment',
compileQuery("depositionalSubEnvironment"),
function (data) {
self.subEnvList(ko.toJS(data));
}
)
};
self.loadArchiEle = function () {
$.post(
self.baseUrl + 'architecturalElement',
compileQuery("architecturalElement"),
function (data) {
self.archiEleList(ko.toJS(data));
}
)
};
self.loadOutcrops = function () {
$.post(
self.baseUrl + 'touchedOutcrops',
compileQuery(""),
function (data) {
self.outcropList(ko.toJS(data));
}
)
};
self.loadFilters = function () {
$.post(
self.baseUrl + 'filters',
compileQuery(""),
function (data) {
var decoded = ko.toJS(data)
self.spatialObservationAndSamplingTypeList(decoded.spatialObservationAndSamplingTypeList);
self.publicationTypeList(decoded.publicationTypeList);
self.outcropQualityList(decoded.outcropQualityList);
self.structuralComplexityList(decoded.structuralComplexityList);
self.outcrop3DcontrolList(decoded.outcrop3DcontrolList);
}
)
};
self.compileQuery = function (target) {
var fullQuery = ko.toJS(self.query);
var query = {};
switch (target) {
//Everthing 'below' architectural element
default:
//filters
query.basinType = fullQuery.basinType;
query.formation = fullQuery.formation;
query.country = fullQuery.country;
//quality filters
query.spatialObservationAndSamplingType = fullQuery.spatialObservationAndSamplingType
query.publicationType = fullQuery.publicationType;
query.outcropQuality = fullQuery.outcropQuality;
query.structuralComplexity = fullQuery.structuralComplexity;
query.outcrop3Dcontrol = fullQuery.outcrop3Dcontrol;
query.architecturalElement = fullQuery.architecturalElement;
case "architecturalElement":
query.depositionalSubEnvironment = fullQuery.depositionalSubEnvironment;
case "depositionalSubEnvironment":
query.mainClimate = fullQuery.mainClimate;
query.depositionalEnvironment = fullQuery.depositionalEnvironment;
case "mainClimate":
case "depositionalEnvironment":
query.grossDepositionalEnvironment = fullQuery.grossDepositionalEnvironment;
case "grossDepositionalEnvironment":
query.projects = fullQuery.projects;
}
return query;
};
return self;
};
/*--- INITIAL ---*/
ko.applyBindings(archiTecturalSearchViewModel);
self.loadGde();
//make outcrops move with the page
$(window).scroll(function () {
if ($(window).scrollTop() < ($('#archiEle-left').height() - 200) && $("#outcrop-container").height() < 300) {
$("#outcrop-container").stop().animate({"marginTop": ($(window).scrollTop()) + "px"}, "slow");
}
});
})(jQuery);
@ChrisPappalardo
Copy link

That is some sexy code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment