PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/public/bower/src/main.js

https://gitlab.com/bandana/Astro-Veda
JavaScript | 459 lines | 373 code | 85 blank | 1 comment | 10 complexity | f11add75fe7fa9377c96a49b14b6e486 MD5 | raw file
  1. (function($, undefined) {
  2. 'use strict';
  3. var
  4. EventHandler = _.extend({}, Backbone.Events),
  5. EVENTS = {
  6. PLANET_CHANGED: 'planet-selected',
  7. SELECT2_SELECT: 'select2:select',
  8. SELECT2_UNSELECT: 'select2:unselect',
  9. SET_CHART_BUILDER_VIEW: 'set-chart-builder-view',
  10. SET_FINAL_CHART_VIEW: 'set-final-chart-view',
  11. },
  12. SCALE = 5,
  13. PLANETS = [
  14. {id: 'sun', text: 'Sun', abbr: 'Su'},
  15. {id: 'moon', text: 'Moon', abbr: 'Mo'},
  16. {id: 'mercury', text: 'Mercury', abbr: 'Mer'},
  17. {id: 'venus', text: 'Venus', abbr: 'Ve'},
  18. {id: 'mars', text: 'Mars', abbr: 'Ma'},
  19. {id: 'jupiter', text: 'Jupiter', abbr: 'Ju'},
  20. {id: 'saturn', text: 'Saturn', abbr: 'Sa'},
  21. {id: 'uranus', text: 'Uranus', abbr: 'Ur'},
  22. {id: 'neptune', text: 'Neptune', abbr: 'Nep'},
  23. {id: 'pluto', text: 'Pluto', abbr: 'Pl'}
  24. ],
  25. PolygonM = Backbone.Model.extend({
  26. defaults: {
  27. planets: [],
  28. planetoryHouseId: 0
  29. }
  30. }),
  31. PolygonC = Backbone.Collection.extend({
  32. model: PolygonM,
  33. url: CONFIG.base_url + 'public/bower/src/data/polygon.json'
  34. }),
  35. polygonC = new PolygonC();
  36. var
  37. AppRoutes = Backbone.Router.extend({
  38. initialize: function() {
  39. },
  40. routes: {
  41. 'builder': 'loadBuildView',
  42. 'confirm': 'loadConfirmationView'
  43. },
  44. loadBuildView: function() {
  45. EventHandler.trigger(EVENTS.SET_CHART_BUILDER_VIEW);
  46. },
  47. loadConfirmationView: function() {
  48. EventHandler.trigger(EVENTS.SET_FINAL_CHART_VIEW);
  49. }
  50. }),
  51. appRoutes = new AppRoutes();
  52. var
  53. BaseChartView = Backbone.View.extend({
  54. mapPolygonPoints: function (scale, polygon) {
  55. return polygon.points.map(function (point) {
  56. point = point.map(function (val) {
  57. return val * scale;
  58. })
  59. return point.join(', ')
  60. }).join(' ');
  61. }
  62. }),
  63. ChartBuilderView = BaseChartView.extend({
  64. initialize: function() {
  65. var baseTemplate = _.template(
  66. $('#builder-template').html()
  67. );
  68. this.$el = $(baseTemplate());
  69. this.modal = null;
  70. this.svgContainer = d3.select(this.$el.find('.svg-container')[0])
  71. .append('svg')
  72. .attr('width', 500)
  73. .attr('height', 500);
  74. polygonC.fetch({
  75. reset: true
  76. });
  77. this.listenTo(
  78. EventHandler,
  79. EVENTS.PLANET_CHANGED,
  80. this.updateSelectablePlanets
  81. );
  82. this.listenTo(polygonC, 'sync', this.postPolygonSyncHandler);
  83. this.$el.find('#btn-set-planets')
  84. .on('click', $.proxy(this.selectPlanets, this));
  85. },
  86. updateSelectablePlanets: function(data) {
  87. var select2s, new_data, selectedPlanets = [];
  88. select2s = this.modal.find('.planets');
  89. select2s.each(function() {
  90. var val = $(this).select2('val');
  91. if (val) {
  92. selectedPlanets = selectedPlanets.concat(val);
  93. }
  94. });
  95. new_data = PLANETS.map(function(planet) {
  96. planet = (_.contains(selectedPlanets, planet.id)) ? _.extend({disabled: true}, planet) :
  97. planet;
  98. return planet;
  99. });
  100. select2s.each(function() {
  101. var val, data;
  102. val = $(this).select2('val');
  103. data = $(this).select2('data');
  104. $(this).empty()
  105. .select2({
  106. data: new_data.concat(data)
  107. });
  108. });
  109. },
  110. selectPlanets: function () {
  111. this.modal.modal('show');
  112. },
  113. postPolygonSyncHandler: function() {
  114. this.populatePolygons();
  115. this.initializeModal();
  116. },
  117. initializeModal: function() {
  118. var elems, self;
  119. self = this;
  120. this.modal = $('#modal-template').html();
  121. this.modal = _.template(this.modal);
  122. this.modal = $(this.modal({
  123. planetoryHouse: polygonC.toJSON()
  124. }));
  125. elems = this.modal.find('.planets');
  126. elems.each(function(index, elem) {
  127. self.bindSelect2($(elem), PLANETS);
  128. });
  129. this.modal.find('#save-chart')
  130. .on('click', $.proxy(this.saveChart, this));
  131. },
  132. saveChart: function() {
  133. var planetDump = {}, startinPlanet, houseId, tempPolygon;
  134. $(this.modal.find('form').serializeArray())
  135. .each(function(index, val) {
  136. if (val.name == 'set-start') {
  137. startinPlanet = val.value;
  138. return;
  139. }
  140. if (planetDump[val.name]) {
  141. planetDump[val.name].push(val.value);
  142. } else {
  143. planetDump[val.name] = [val.value];
  144. }
  145. });
  146. polygonC.forEach(function(polygon) {
  147. var polygonId;
  148. polygonId = polygon.get('id');
  149. if (planetDump[polygonId]) {
  150. polygon.set('planets', planetDump[polygonId]);
  151. }
  152. });
  153. houseId = 1;
  154. tempPolygon = polygonC.findWhere({id: startinPlanet});
  155. tempPolygon.set('planetoryHouseId', houseId);
  156. while (houseId < 12) {
  157. houseId++;
  158. tempPolygon = polygonC.findWhere({
  159. id: tempPolygon.get('next')
  160. });
  161. tempPolygon.set('planetoryHouseId', houseId);
  162. }
  163. this.modal.modal('hide');
  164. appRoutes.navigate('confirm', {trigger: true});
  165. },
  166. bindSelect2 : function(elem, data) {
  167. var self = this;
  168. elem.select2({
  169. width: '100%',
  170. data: data
  171. })
  172. .on(EVENTS.SELECT2_SELECT, function (e) {
  173. EventHandler
  174. .trigger(EVENTS.PLANET_CHANGED, e.params.data);
  175. })
  176. .on(EVENTS.SELECT2_UNSELECT, function(e) {
  177. EventHandler
  178. .trigger(EVENTS.PLANET_CHANGED, e.params.data);
  179. });
  180. },
  181. populatePolygons: function () {
  182. var polygons, texts;
  183. polygons = this.svgContainer
  184. .selectAll('polygon')
  185. .data(polygonC.toJSON())
  186. .enter()
  187. .append('polygon')
  188. .attr('fill', '#f0f0f0')
  189. .attr("stroke", "black")
  190. .attr("stroke-width", 1.5);
  191. polygons.attr('points', _.partial(this.mapPolygonPoints, SCALE));
  192. texts = this.svgContainer
  193. .selectAll('text')
  194. .data(polygonC.toJSON())
  195. .enter()
  196. .append('text');
  197. texts
  198. .attr('x', function (d) {
  199. return (d.text_cordinate[0] * SCALE);
  200. })
  201. .attr('y', function (d) {
  202. return (d.text_cordinate[1] * SCALE);
  203. })
  204. .text(function (d) {
  205. return d.id;
  206. });
  207. },
  208. render: function() {
  209. return this;
  210. }
  211. }),
  212. FinalChartView = BaseChartView.extend({
  213. initialize: function() {
  214. var baseTemplate = _.template(
  215. $('#confirmation-template').html()
  216. );
  217. this.$el = $(baseTemplate());
  218. this.svgContainer = d3.select(this.$el.find('.svg-container')[0])
  219. .append('svg')
  220. .attr("version", 1.1)
  221. .attr("xmlns", "http://www.w3.org/2000/svg")
  222. .attr('width', 500)
  223. .attr('height', 500);
  224. this.$el.find('button#export-to-png')
  225. .on('click', $.proxy(this.exportSVGToPng, this));
  226. },
  227. render: function() {
  228. this.populatePolygons();
  229. return this;
  230. },
  231. exportSVGToPng: function() {
  232. var html, canvas, context, DOMURL, image, pngimg, canvasdata, svg,
  233. url;
  234. html = new XMLSerializer().serializeToString(this.svgContainer.node());
  235. canvas = document.createElement('canvas');
  236. canvas.width = 500;
  237. canvas.height = 500;
  238. context = canvas.getContext("2d");
  239. DOMURL = URL || webkitURL;
  240. svg = new Blob([html], {type: "image/svg+xml;charset=utf-8"});
  241. url = DOMURL.createObjectURL(svg);
  242. image = new Image;
  243. image.src = url;
  244. image.onload = function() {
  245. context.drawImage(image, 0, 0);
  246. canvasdata = canvas.toDataURL("image/png");
  247. pngimg = '<img src="'+canvasdata+'">';
  248. canvasdata = canvasdata.replace(/^data:image\/(png|jpg);base64,/, "");
  249. // Sending the image data to Server
  250. $.ajax({
  251. type: 'POST',
  252. url: CONFIG.url,
  253. data: {
  254. imageData: canvasdata
  255. },
  256. success: function (msg) {
  257. alert("Done, Image Uploaded.");
  258. window.location.href = CONFIG.base_url + 'admin/natal_charts/index/' + CONFIG.user_id;
  259. }
  260. });
  261. };
  262. },
  263. populatePolygons: function() {
  264. var groups, style;
  265. style = document.createElement('style');
  266. style.innerHTML = "/* <![CDATA[ */text {font-size: 14px;color: black; overflow: auto;}/* ]]> */";
  267. this.svgContainer.node()
  268. .insertBefore(style, this.svgContainer.node().firstChild);
  269. groups = this.svgContainer
  270. .selectAll('g')
  271. .data(polygonC.toJSON())
  272. .enter()
  273. .append('g');
  274. groups.append('polygon')
  275. .attr('fill', '#f0f0f0')
  276. .attr("stroke", "black")
  277. .attr("stroke-width", 1.5)
  278. .attr('points', _.partial(this.mapPolygonPoints, SCALE));
  279. groups.append('text')
  280. .attr('x', function (d) {
  281. return (d.planetory_house_coordinate[0] * SCALE);
  282. })
  283. .attr('y', function (d) {
  284. return (d.planetory_house_coordinate[1] * SCALE);
  285. })
  286. .attr("dy", ".35em")
  287. .text(function (d) {
  288. return d.planetoryHouseId;
  289. });
  290. var planetGroup = groups.append('g');
  291. var ptext1 = planetGroup.selectAll('text')
  292. .data(function(d) {
  293. var _planets, planetGroup = [], temp, accumulator, text_cordinate;
  294. _planets = _.filter(PLANETS, function(planet) {
  295. return _.contains(this.planets, planet.id);
  296. }, d);
  297. _planets = _.pluck(_planets, 'abbr');
  298. accumulator = (_planets.length > 2) ? -5 : 0;
  299. while (_planets.length > 0) {
  300. temp = _planets.splice(2);
  301. planetGroup.push({
  302. text_cordinate: [d.text_cordinate[0] - 5, (d.text_cordinate[1] + accumulator)],
  303. planets: _planets
  304. });
  305. _planets = temp;
  306. accumulator += 5;
  307. }
  308. return planetGroup;
  309. })
  310. .enter()
  311. .append('text');
  312. ptext1
  313. .attr('x', function (d) {
  314. return (d.text_cordinate[0] * SCALE);
  315. })
  316. .attr('y', function (d) {
  317. return (d.text_cordinate[1] * SCALE);
  318. })
  319. .attr("dy", ".35em")
  320. .text(function (d) {
  321. return d.planets.join(', ');
  322. });
  323. }
  324. }),
  325. AppView = Backbone.View.extend({
  326. el: '#app-wrapper',
  327. initialize: function() {
  328. this.activeView = null;
  329. this.listenTo(
  330. EventHandler,
  331. EVENTS.SET_CHART_BUILDER_VIEW,
  332. this.setCharBuilderView
  333. );
  334. this.listenTo(
  335. EventHandler,
  336. EVENTS.SET_FINAL_CHART_VIEW,
  337. this.setFinalChartView
  338. );
  339. },
  340. setCharBuilderView: function() {
  341. var
  342. view = new ChartBuilderView();
  343. this.setView(view)
  344. .render();
  345. },
  346. setFinalChartView: function() {
  347. var view;
  348. view = new FinalChartView();
  349. this.setView(view)
  350. .render();
  351. },
  352. setView: function(view) {
  353. this.activeView = view;
  354. return this;
  355. },
  356. render: function() {
  357. if (this.activeView) {
  358. this.$el.empty();
  359. this.activeView
  360. .render()
  361. .$el
  362. .appendTo(this.$el);
  363. }
  364. },
  365. });
  366. $(document).ready(function() {
  367. var appView;
  368. appView = new AppView();
  369. Backbone.history.start();
  370. appRoutes.navigate("builder", {
  371. trigger: true
  372. });
  373. });
  374. })(jQuery);