PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/facete-client/src/main/webapp/resources/js/org/aksw/ssb/facets/mvc/models.js

https://github.com/GeoKnow/Facete
JavaScript | 525 lines | 242 code | 121 blank | 162 comment | 14 complexity | b2ff41df939353f3557fe6df17a3c9aa MD5 | raw file
  1. (function() {
  2. /*
  3. var backend = Namespace("org.aksw.ssb.backend");
  4. var backboneUtils = Namespace("org.aksw.utils.backbone");
  5. var xsd = Namespace("org.aksw.ssb.vocabs.xsd");
  6. var labelUtils = Namespace("org.aksw.ssb.utils");
  7. */
  8. /*
  9. * On the relation of Sub facet state, child cache ...
  10. *
  11. * The thing is, that there is a distinction between the
  12. * complete facet tree, the fragment of it fetched and stored in the models, and the
  13. * fragment being shown in the UI.
  14. *
  15. * For each facet node, whenever we expand an item, we need to save the state of it.
  16. * This should be the purpose of the child cache, and is thus local to the facet tree!
  17. * The children attribute carries the actual shown facets, which depend on the subFacetSpec
  18. *
  19. *
  20. *
  21. */
  22. var widgetNs = Namespace("org.aksw.ssb.widgets");
  23. var facets = Namespace("org.aksw.ssb.facets");
  24. var ns = facets;
  25. ns.ModelSubFacetState = Backbone.Model.extend({
  26. defaults: {
  27. strategy: 'exact' // How to compute the facets. Supported values: "exact", "partitioned"
  28. // Following attributes only apply to strategy partitioned
  29. //tableModel: null //new widget.TabelModel()// Paginator on the partitions
  30. }
  31. });
  32. ns.ModelFacetNode = Backbone.Model.extend({
  33. defaults : {
  34. //step: null, // The step leading to this node (null for root nodes)
  35. // baseQueryFactory: null, // Query factory for the instances (can
  36. // be seen as facet values)
  37. // facetsQueryFactory: null, // Query factory for the facets (of the
  38. // instances)
  39. // constraints: [], // constraints applying to this node
  40. isExpanded : false, // when changing to true, the controller will
  41. // start loading the children
  42. isLoading : false,
  43. childState: null, //new ns.ModelSubFacetState();
  44. childCache: null, // A mapping from id to child object which holds the state of ALL encountered children - i.e. even those not visible right now
  45. children: null, // The currently visible children
  46. hasSubFacets : true, // null means: not checked yet
  47. /*
  48. * children: new Backbone.Collection({ //model: ns.ModelFacetNode
  49. * }), // subFacets - NOT facet values!
  50. */
  51. /////facetFacadeNode : null, // Reference to a (non-model)
  52. facetNode: null
  53. // FacetFacadeNode
  54. },
  55. initialize : function() {
  56. // console.log("Initializing ModelFacetNode", this);
  57. this.set({
  58. children : new ns.CollectionFacetNode()
  59. });
  60. this.childState = widgetNs.createQueryBrowser();//sparqlService, labelFetcher);
  61. },
  62. forPath: function(path) {
  63. var steps = path.getSteps();
  64. var result = this;
  65. for(var i = 0; i < steps.length; ++i) {
  66. var step = steps[i];
  67. var children = result.get('children');
  68. var subNode = children.find(function(child) {
  69. ///// var facetFacadeNode = child.get('facetFacadeNode');
  70. ///// var childStep = facetFacadeNode.getFacetNode().getStep();
  71. var facetNode = child.get('facetNode');
  72. var childStep = facetNode.getStep();
  73. var result = childStep.equals(step);
  74. return result;
  75. });
  76. result = subNode;
  77. if(!result) {
  78. console.log("Path not found: " + path);
  79. break;
  80. }
  81. }
  82. return result;
  83. }
  84. });
  85. ns.CollectionFacetNode = Backbone.Collection.extend({
  86. model : ns.ModelFacetNode
  87. });
  88. /**
  89. * ModelConstraints should be treated immutable!
  90. *
  91. * TODO: I think we should use the complex model here too:
  92. * a constraint has a type and then some extra fields depending on it
  93. * e.g. {type: 'equals', path: somePath, value: node}
  94. */
  95. ns.ModelConstraint = Backbone.Model.extend({
  96. defaults : {
  97. //type: null,
  98. //path : null, // The path the contraint applies to
  99. // TODO: Not sure if the constraint should be a sub-object
  100. constraint : null
  101. }
  102. });
  103. ns.ModelConstraint.fromJSON = function(json) {
  104. var serializer = Namespace('org.aksw.serializer').Serializer.singleton;
  105. var data = serializer.deserialize(json);
  106. var result = new ns.ModelConstraint(data);
  107. return result;
  108. };
  109. ns.ModelColumn = Backbone.Model.extend({
  110. defaults: {
  111. path: null
  112. }
  113. });
  114. ns.CollectionColumns = Backbone.Collection.extend({
  115. model: ns.ModelColumn,
  116. /*
  117. toTree: function() {
  118. var rootNode = facets.FacetNode.createRoot(varName, generator);
  119. this.each(function(model) {
  120. var p = model.get('path');
  121. rootNode.forPath(p);
  122. });
  123. return rootNode;
  124. },
  125. createElementFromNodeRec: function(elements, facetNode) {
  126. var result = [];
  127. var steps = facetNode.getSteps();
  128. var elements = facetNode.getDirectTriples();
  129. if(elements.length > 0) {
  130. var tmp = new sparql.ElementGroup(elements);
  131. result.push(new sparql.ElementOptional(tmp));
  132. }
  133. for(var i = 0; i < steps.length; ++i) {
  134. var step = steps[i];
  135. }
  136. },
  137. createElementRec: function(facetNode) {
  138. },
  139. */
  140. createProjection: function(facetNode) {
  141. var result = new sparql.VarExprList();
  142. this.each(function(model) {
  143. var p = model.get('path');
  144. var subFacetNode = facetNode.forPath(p);
  145. var v = subFacetNode.getVariable();
  146. result.add(v);
  147. });
  148. return result;
  149. },
  150. /**
  151. *
  152. * @returns Array of sparql.Element objects
  153. */
  154. createElements: function(facetNode) {
  155. var result = [];
  156. this.each(function(model) {
  157. var p = model.get('path');
  158. var subFacetNode = facetNode.forPath(p);
  159. var triples = subFacetNode.getTriples();
  160. if(triples.length === 0) {
  161. return;
  162. }
  163. var block = new sparql.ElementTriplesBlock(triples);
  164. var optional = new sparql.ElementOptional(block);
  165. result.push(optional);
  166. });
  167. return result;
  168. },
  169. findByPath: function(path) {
  170. var result = this.find(function(model) {
  171. var p = model.get('path');
  172. return p.equals(path);
  173. });
  174. return result;
  175. },
  176. containsPath: function(path) {
  177. var model = this.findByPath(path);
  178. var result = model ? true : false;
  179. return result;
  180. },
  181. /**
  182. * Avoids duplicates
  183. *
  184. */
  185. addPath: function(path) {
  186. var contained = this.containsPath(path);
  187. if(contained) {
  188. return false;
  189. }
  190. this.add({
  191. path: path
  192. });
  193. return true;
  194. },
  195. removePath: function(path) {
  196. var model = this.findByPath(path);
  197. this.remove(model);
  198. }
  199. });
  200. /**
  201. * A collection for contraints.
  202. *
  203. * For simplicity this is a flat collection. TODO Explain how to update a
  204. * query factory from it
  205. *
  206. */
  207. ns.ConstraintCollection2 = Backbone.Collection.extend({
  208. model: ns.ModelConstraint,
  209. /*
  210. fromJson: function(json) {
  211. var items = [];
  212. for(var i = 0; i < json.length; ++i) {
  213. var item = this.model.fromJson(json);
  214. items.push(item);
  215. };
  216. this.reset(items);
  217. },*/
  218. clone: function() {
  219. var result = new ns.ConstraintCollection2();
  220. this.each(function(model) {
  221. var data = model.attributes;
  222. result.add(data);
  223. });
  224. return result;
  225. },
  226. /**
  227. *
  228. * @param path
  229. * @param node
  230. * @return the model holding this constraint or null
  231. */
  232. findModelEquals: function(path, node) {
  233. var result = this.find(function(model) {
  234. //console.log("Comparing path and node to model:", path, node, model);
  235. var constraint = model.get('constraint');
  236. //var constraint = model;
  237. /*
  238. console.log("Comparing path and node to model:", path, node, model);
  239. console.log(constraint.type === 'equals');
  240. console.log(path.equals(constraint.path));
  241. console.log(node.equals(constraint.node));
  242. */
  243. var test
  244. = constraint.type === 'equals'
  245. && path.equals(constraint.path)
  246. && node.equals(constraint.node);
  247. return test;
  248. });
  249. return result;
  250. },
  251. setEqualsConstraint: function(path, node, isEnabled) {
  252. var modelData = {
  253. constraint: {
  254. type : "equals",
  255. path : path,
  256. node : node
  257. }
  258. };
  259. //console.log("Setting constraint: ", modelData);
  260. var priorModel = this.findModelEquals(path, node);
  261. if(isEnabled && !priorModel) {
  262. this.add(modelData);
  263. }
  264. else if(!isEnabled && priorModel) {
  265. this.remove(priorModel);
  266. }
  267. },
  268. toggleEqualsConstraint: function(path, node) {
  269. var priorModel = this.findModelEquals(path, node);
  270. var isEnabled = priorModel ? false : true;
  271. this.setEqualsConstraint(path, node, isEnabled);
  272. },
  273. /**
  274. * Tests whether an equals constraint exists
  275. *
  276. * @param path
  277. * @param node
  278. * @returns
  279. */
  280. existsEquals: function(path, node) {
  281. var model = this.findModelEquals(path, node);
  282. var result = model ? true : false;
  283. return result;
  284. },
  285. existsEqualsDeprecated: function(path, node) {
  286. var result = this.some(function(model) {
  287. //console.log("Comparing path and node to model:", path, node, model);
  288. var constraint = model.get('constraint');
  289. //var constraint = model;
  290. /*
  291. console.log("Comparing path and node to model:", path, node, model);
  292. console.log(constraint.type === 'equals');
  293. console.log(path.equals(constraint.path));
  294. console.log(node.equals(constraint.node));
  295. */
  296. var test
  297. = constraint.type === 'equals'
  298. && path.equals(constraint.path)
  299. && node.equals(constraint.node);
  300. return test;
  301. });
  302. return result;
  303. },
  304. createConstraintManager: function(rootFacetNode) {
  305. var constraintManager = new facets.ConstraintManager();
  306. var facetFacadeNode = new facets.SimpleFacetFacade(constraintManager, rootFacetNode);
  307. this.each(function(model) {
  308. var constraint = model.get('constraint');
  309. var path = constraint.path;
  310. var node = constraint.node;
  311. checkNotNull(constraint);
  312. if(constraint.type !== 'equals') {
  313. logger.log("Only equals supported right now");
  314. return
  315. }
  316. facetFacadeNode.forPath(path).addConstraint({
  317. type: 'equals',
  318. path: path,
  319. node: node
  320. });
  321. });
  322. return constraintManager;
  323. // var nodeValue = sparql.NodeValue.makeNode(sparql.Node
  324. // .uri("http://dbpedia.org/resource/year/2008"));
  325. // var constraint = facets.ConstraintUtils.createEquals(yearPath,
  326. // nodeValue);
  327. }
  328. });
  329. ns.ConstraintCollection2.classLabel = 'ConstraintCollection';
  330. ns.createFacetNodes = function(constraintCollection, rootFacetNode) {
  331. constraintCollection.each(function(model) {
  332. var path = model.get("path");
  333. var constraint = model.get("constraint");
  334. var node = rootFaceNode.forPath(path);
  335. node.addConstraint(constraint);
  336. });
  337. };
  338. /**
  339. * These models are created
  340. *
  341. */
  342. ns.ModelItemCheckConstraint = Backbone.Model.extend({
  343. defaults: {
  344. isChecked: false
  345. //path: null,
  346. //node: null
  347. }
  348. });
  349. /**
  350. * This collection is
  351. *
  352. */
  353. ns.CollectionItemCheckConstraint = Backbone.Collection.extend({
  354. model: ns.ModelItemCheckConstraint
  355. });
  356. ns.MapItemModel = Backbone.Model.extend({
  357. defaults: {
  358. isHidden: false, // Whether the item is hidden
  359. coords: null,
  360. label: null
  361. }
  362. });
  363. ns.MapItemCollection = Backbone.Collection.extend({
  364. model: ns.MapItemModel
  365. });
  366. ns.MapBoxModel = Backbone.Model.extend({
  367. defaults: {
  368. bounds: null
  369. // other attributes...
  370. }
  371. });
  372. ns.MapBoxCollection = Backbone.Collection.extend({
  373. model: ns.MapBoxModel
  374. });
  375. ns.MapModel = Backbone.Model.extend({
  376. defaults: {
  377. center: {lat: 0, lon: 0},
  378. zoom: 10,
  379. /**
  380. * The collection of resources that should be displayed.
  381. */
  382. //uris: [],
  383. //resources: new ResourceCollection(),
  384. /**
  385. * Rdf data about these resources in Talis Json format
  386. */
  387. //json: {},
  388. items: new ns.MapItemCollection(),
  389. boxes: new ns.MapBoxCollection()
  390. }
  391. });
  392. })();