PageRenderTime 35ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Resources/assets/js/mobile-engine-debug.js

https://bitbucket.org/wizzmedia/nexity-generateur
JavaScript | 821 lines | 744 code | 67 blank | 10 comment | 135 complexity | 24d84b83267edd80cb821aea4ea72e55 MD5 | raw file
Possible License(s): Apache-2.0
  1. var infoBubbleWidth = 250;
  2. var infoBubbles = [];
  3. _.templateSettings = {
  4. evaluate: /\{%(.+?)%\}/g,
  5. interpolate: /\{\{(.+?)\}\}/g,
  6. escape: /\{\{-(.+?)\}\}/g
  7. };
  8. function formatNumber(num) {
  9. return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1 ");
  10. }
  11. function formatKNumber(num) {
  12. if (num < 10000) {
  13. return formatNumber(num);
  14. }
  15. return (num / 1000).toString() + 'k';
  16. }
  17. function formatDate(chaine) {
  18. var toDate = chaine.split('-');
  19. return toDate[2] + '/' + toDate[1] + '/' + toDate[0];
  20. }
  21. function modal_msg(msg, autohide) {
  22. $('#modalMsg .modal-body p').html(msg);
  23. if (autohide) {
  24. $('#modalMsg').addClass('autohide');
  25. } else {
  26. $('#modalMsg').removeClass('autohide');
  27. }
  28. $('#modalMsg').modal('show');
  29. }
  30. function getIBItemHtml(result, divData) {
  31. var etiquette = '';
  32. var libelle = result.get('bien').get('libelle');
  33. if (result.get('bien').get('source') == 2) {
  34. etiquette = 'neuf';
  35. if (result.get('bien').get('programme')) {
  36. libelle = result.get('bien').get('programme').libelle;
  37. }
  38. }
  39. if (result.get('bien').get('residence')) {
  40. etiquette = 'résidence gérée';
  41. }
  42. if (result.get('bien').get('source') == 4) {
  43. etiquette = 'Terrain à bâtir';
  44. }
  45. if (result.get('bien').get('type_bien') != 'Parking/Box' && result.get('bien').get('type_bien') != 'Terrain') {
  46. libelle = result.get('bien').get('type_bien');
  47. if (result.get('bien').get('nb_piece') > 0) {
  48. libelle = libelle.concat(result.get('bien').get('nb_piece') + ' pièce');
  49. }
  50. if (result.get('bien').get('nb_piece') > 1) {
  51. libelle = libelle.concat('s');
  52. }
  53. if (result.get('bien').get('surface') > 0) {
  54. libelle = libelle.concat('de ' + result.get('bien').get('surface') + ' m²');
  55. }
  56. } else {
  57. libelle = result.get('bien').get('type_bien');
  58. }
  59. return _.template($('#infobubble_content_item').html())({ result: result, etiquette: etiquette, libelle: libelle, divData: divData });
  60. }
  61. function getIBMultipleItemHtml(quartier) {
  62. content = quartier.map(function (point, index) {
  63. return getIBItemHtml(point, 'data-index="' + index + '" data-annonce-id="' + quartier[index].get('bien').get('reference') + '"');
  64. }).join('');
  65. return _.template($('#infobubble_content_quartier').html())({ quartier: quartier, items: content });
  66. }
  67. $.fn.nodoubletapzoom = function () {
  68. if (/iphone|ipad/i.test(navigator.userAgent)) {
  69. $(this).bind('touchstart', function preventZoom(e) {
  70. var t2 = e.timeStamp,
  71. t1 = $(this).data('lastTouch') || t2,
  72. dt = t2 - t1,
  73. fingers = e.originalEvent.touches.length;
  74. $(this).data('lastTouch', t2);
  75. if (!dt || dt > 500 || fingers > 1) return;
  76. e.preventDefault();
  77. $(this).trigger('click');
  78. });
  79. }
  80. };
  81. var HtmlInput = Backbone.Model.extend({
  82. defaults: {
  83. id: '',
  84. libelle: '',
  85. value: null,
  86. isActive: true,
  87. isChecked: false
  88. },
  89. reinitialize: function reinitialize() {
  90. this.set('isChecked', false);
  91. }
  92. });
  93. var HtmlInputCollection = Backbone.Collection.extend({
  94. model: HtmlInput
  95. });
  96. var CriteriaSelector = Backbone.Model.extend({
  97. defaults: {
  98. id: '',
  99. value: null,
  100. inputType: '',
  101. isActive: true,
  102. inputChoices: []
  103. },
  104. initialize: function initialize(data) {
  105. this.set('inputChoices', new HtmlInputCollection(data.inputChoices));
  106. }
  107. });
  108. var Criteria = Backbone.Model.extend({
  109. defaults: {
  110. name: '',
  111. libelle: '',
  112. value: null,
  113. units: '',
  114. selector: '',
  115. isDisplayed: true,
  116. isChecked: false,
  117. isActive: true,
  118. inputType: '',
  119. step: null,
  120. inputChoices: []
  121. },
  122. initialize: function initialize(data) {
  123. this.set('inputChoices', new HtmlInputCollection(data.inputChoices));
  124. this.on('change:isActive', function (model) {
  125. if (!model.get('isActive') && model.get('inputType') == 'checkbox') {
  126. this.reinitialize();
  127. }
  128. }, this);
  129. this.get('inputChoices').on('change', function (model) {
  130. this.updateValue();
  131. }, this);
  132. },
  133. add: function add(attribute, value) {
  134. var aAttribute = _.clone(this.get(attribute));
  135. aAttribute.push(value);
  136. this.set(attribute, aAttribute);
  137. },
  138. remove: function remove(attribute, value) {
  139. var arrAttr = this.get(attribute);
  140. this.set(attribute, _.without(arrAttr, value));
  141. },
  142. reinitialize: function reinitialize() {
  143. switch (this.get('inputType')) {
  144. case 'checkbox':
  145. this.set('isChecked', false);
  146. break;
  147. case 'changer':
  148. this.set('isActive', false);
  149. break;
  150. case 'hidden':
  151. if (typeof this.get('value') == 'number') {
  152. this.set('value', 0);
  153. } else {
  154. this.set('value', '');
  155. }
  156. this.set('isActive', false);
  157. break;
  158. case 'radio':
  159. if (this.get('name') == 'anciennete') {
  160. this.set('value', '0');
  161. }
  162. break;
  163. case 'multiple-checkbox':
  164. this.get('inputChoices').each(function (choice) {
  165. choice.reinitialize();
  166. });
  167. break;
  168. }
  169. },
  170. serialize: function serialize(mode) {
  171. var _this2 = this;
  172. if (this.get('isDisplayed') && !_.contains(['', null], this.get('value'))) {
  173. var retour;
  174. switch (this.get('inputType')) {
  175. case 'checkbox':
  176. if (this.get('isActive') && this.get('isChecked')) {
  177. retour = this.get('name') + '=' + this.get('value');
  178. }
  179. break;
  180. case 'multiple-checkbox':
  181. if (this.get('isActive')) {
  182. switch (mode) {
  183. case SERIALIZE_FOR_URL:
  184. case SERIALIZE_FOR_ALERT:
  185. retour = this.get('inputChoices').where({ isChecked: true }).map(function (inputChoice) {
  186. return _this2.get('name') + "[]=" + inputChoice.get('value');
  187. }).join('&');
  188. break;
  189. default:
  190. retour = this.get('name') + '=' + this.get('value');
  191. break;
  192. }
  193. }
  194. break;
  195. case 'location':
  196. if (this.get('isActive')) {
  197. switch (mode) {
  198. case SERIALIZE_FOR_URL:
  199. if (this.get('value').length == 1 && this.get('value')[0] == masterFilters.get('initialCriterias').get('location_id').get('value')) {
  200. return null;
  201. }
  202. case SERIALIZE_FOR_ALERT:
  203. retour = this.get('value').map(function (locId) {
  204. return "locationsId[]=" + locId;
  205. }).join('&');
  206. break;
  207. case SERIALIZE_FOR_MAP_ZONE:
  208. retour = '';
  209. break;
  210. default:
  211. retour = this.get('name') + '=' + this.get('value');
  212. break;
  213. }
  214. }
  215. break;
  216. case 'hidden':
  217. if (this.get('isActive')) {
  218. switch (mode) {
  219. case SERIALIZE_FOR_URL:
  220. switch (this.get('name')) {
  221. case 'nb_p':
  222. retour = this.get('value').map(function (piece) {
  223. return "nb_p[]=" + piece;
  224. }).join('&');
  225. break;
  226. default:
  227. retour = this.get('name') + '=' + this.get('value');
  228. break;
  229. }
  230. break;
  231. case SERIALIZE_FOR_MAP_ZONE:
  232. if (_.indexOf(['latitude', 'longitude', 'distance'], this.get('name')) == -1) {
  233. retour = this.get('name') + '=' + this.get('value');
  234. }
  235. break;
  236. default:
  237. retour = this.get('name') + '=' + this.get('value');
  238. break;
  239. }
  240. }
  241. break;
  242. default:
  243. if (this.get('isActive') && _.indexOf(['min_piece', 'max_piece'], this.get('name')) == -1) {
  244. switch (mode) {
  245. case SERIALIZE_FOR_URL:
  246. if (_.indexOf(masterFilters.get('initialCriterias').pluck('name'), this.get('name')) > -1) {
  247. if (this.get('value') == masterFilters.get('initialCriterias').get(this.get('name')).get('value')) {
  248. return null;
  249. }
  250. }
  251. break;
  252. }
  253. retour = this.get('name') + '=' + this.get('value');
  254. }
  255. break;
  256. }
  257. if (retour != "anciennete=0") {
  258. return retour;
  259. }
  260. }
  261. return null;
  262. },
  263. updateValue: function updateValue() {
  264. var criteriaValue = this.get('inputChoices').where({ isChecked: true }).map(function (inputChoice) {
  265. return inputChoice.get('value');
  266. }).join(',');
  267. this.set('value', criteriaValue);
  268. }
  269. });
  270. var CriteriaCollection = Backbone.Collection.extend({
  271. model: Criteria,
  272. modelId: function modelId(attrs) {
  273. return attrs.name;
  274. }
  275. });
  276. var Filter = Backbone.Model.extend({
  277. defaults: {
  278. id: '',
  279. libelle: '',
  280. picto: '',
  281. value: '',
  282. desc: '',
  283. isActive: true,
  284. criteriaSelector: null,
  285. criterias: []
  286. },
  287. initialize: function initialize(data) {
  288. var _this3 = this;
  289. if (data.criteriaSelector) {
  290. this.set('criteriaSelector', new CriteriaSelector(data.criteriaSelector));
  291. }
  292. this.set('criterias', new CriteriaCollection(data.criterias));
  293. this.updateValue();
  294. this.on('change:isActive', function (model) {
  295. if (!model.get('isActive')) {
  296. _this3.reinitialize();
  297. }
  298. });
  299. this.get('criterias').on('change', this.updateValue.bind(this));
  300. if (data.criteriaSelector) {
  301. this.get('criteriaSelector').on('change:value', function (model, value) {
  302. _this3.get('criterias').forEach(function (criteria) {
  303. if (model.get('inputType') === 'exploded-radio') {
  304. criteria.set('isActive', criteria.get('selector') == value);
  305. } else {
  306. criteria.set('isActive', false);
  307. if (model.get('inputType') === 'radio') {
  308. criteria.set('isDisplayed', criteria.get('selector') == value);
  309. }
  310. }
  311. });
  312. });
  313. }
  314. },
  315. reinitialize: function reinitialize() {
  316. this.get('criterias').forEach(function (criteria) {
  317. criteria.reinitialize();
  318. });
  319. },
  320. updateValue: function updateValue() {
  321. var _this4 = this;
  322. var filterValues = [],
  323. c_name = null,
  324. c_val_min = null,
  325. c_val_lib = null;
  326. this.get('criterias').forEach(function (criteria) {
  327. switch (criteria.get('inputType')) {
  328. case 'radio':
  329. filterValues.push(criteria.get('inputChoices').findWhere({ value: criteria.get('value') }).get('libelle'));
  330. break;
  331. case 'checkbox':
  332. if (criteria.get('isActive') && criteria.get('isChecked')) {
  333. filterValues.push(criteria.get('libelle'));
  334. }
  335. break;
  336. case 'multiple-checkbox':
  337. {
  338. var filterValue = criteria.get('inputChoices').where({ isChecked: true }).map(function (inputChoice) {
  339. return inputChoice.get('libelle');
  340. }).join(' - ');
  341. filterValues.push(filterValue != '' ? filterValue : _this4.get('emptyLib'));
  342. break;
  343. }
  344. case 'changer':
  345. if (criteria.get('isActive')) {
  346. if (criteria.get('name').indexOf('min') === 0) {
  347. c_name = criteria.get('name').replace('min_', '');
  348. c_val_min = criteria.get('value');
  349. c_val_lib = formatKNumber(criteria.get('value')) + ' ' + criteria.get('units');
  350. filterValues.push(c_val_lib + ' min.');
  351. } else if (criteria.get('name').indexOf('max') === 0) {
  352. if (c_name == criteria.get('name').replace('max_', '')) {
  353. if (c_val_min == criteria.get('value')) {
  354. filterValues[filterValues.length - 1] = formatKNumber(criteria.get('value')) + ' ' + criteria.get('units');
  355. } else {
  356. filterValues[filterValues.length - 1] = c_val_lib + ' &gt; ' + formatKNumber(criteria.get('value')) + ' ' + criteria.get('units');
  357. }
  358. c_name = c_val_min = c_val_lib = null;
  359. } else {
  360. filterValues.push(formatKNumber(criteria.get('value')) + ' ' + criteria.get('units') + ' max.');
  361. }
  362. } else {
  363. filterValues.push(formatKNumber(criteria.get('value')) + ' ' + criteria.get('units'));
  364. }
  365. }
  366. break;
  367. case 'location':
  368. {
  369. var _filterValue = criteria.get('valuelib').join(' - ');
  370. if (_filterValue == '') {
  371. _filterValue = 'France';
  372. }
  373. filterValues.push(_filterValue);
  374. break;
  375. }
  376. }
  377. });
  378. this.set('value', filterValues.length > 0 ? filterValues.join(' - ') : this.get('emptyLib'));
  379. },
  380. serialize: function serialize(mode) {
  381. var param = [];
  382. this.get('criterias').each(function (criteria) {
  383. var paramString = criteria.serialize(mode);
  384. if (paramString) {
  385. param.push(paramString);
  386. }
  387. });
  388. return param.join('&');
  389. }
  390. });
  391. var FilterCollection = Backbone.Collection.extend({
  392. model: Filter
  393. });
  394. var MasterFilters = Backbone.Model.extend({
  395. defaults: {
  396. id: '',
  397. libelle: '',
  398. initialCriterias: {},
  399. mainFilters: {},
  400. otherFilters: {},
  401. pageNumber: 1,
  402. pageSize: 12,
  403. mapPageSize: 300,
  404. sortField: 'prix',
  405. sortOrder: 'asc'
  406. },
  407. initialize: function initialize(data) {
  408. this.set('initialCriterias', new CriteriaCollection(data.initialCriterias));
  409. this.set('mainFilters', new FilterCollection(data.mainFilters));
  410. this.set('otherFilters', new FilterCollection(data.otherFilters));
  411. this.setSort($('#resultats-tri').val());
  412. this.updateNbFilters();
  413. this.get('mainFilters').on('change', function (model) {
  414. this.updateFiltersState();
  415. if (model.get('id') == 'projet') {
  416. this.updateBudgetValues();
  417. }
  418. this.resetPage();
  419. }, this);
  420. this.get('otherFilters').on('change', function (model) {
  421. this.updateFiltersState();
  422. this.resetPage();
  423. this.updateNbFilters();
  424. }, this);
  425. $('.row').on('click', '.container-offer', function (e) {
  426. e.stopPropagation();
  427. $(this).find('.offer-link').get(0).click();
  428. });
  429. },
  430. serialize: function serialize(mode, extra) {
  431. var param = [];
  432. this.get('mainFilters').each(function (filter) {
  433. var paramString = filter.serialize(mode);
  434. if (paramString) {
  435. param.push(paramString);
  436. }
  437. });
  438. this.get('otherFilters').each(function (filter) {
  439. var paramString = filter.serialize(mode);
  440. if (paramString) {
  441. param.push(paramString);
  442. }
  443. });
  444. if (extra) {
  445. _.each(extra, function (value, key) {
  446. param.push(key + '=' + value);
  447. });
  448. }
  449. switch (mode) {
  450. case SERIALIZE_FOR_MAP:
  451. case SERIALIZE_FOR_MAP_ZONE:
  452. param.push('withPartners=1');
  453. param.push('pageNumber=1');
  454. param.push('pageSize=' + this.get('mapPageSize'));
  455. break;
  456. case SERIALIZE_FOR_URL:
  457. if (this.get('pageNumber') > 1) {
  458. param.push('page=' + this.get('pageNumber'));
  459. }
  460. if (this.get('sortField') != 'prix' || this.get('sortOrder') != 'asc') {
  461. param.push('order=' + this.get('sortField') + ' ' + this.get('sortOrder'));
  462. }
  463. break;
  464. case SERIALIZE_FOR_ALERT:
  465. break;
  466. case SERIALIZE_DEFAULT:
  467. default:
  468. param.push('withPartners=1');
  469. param.push('pageNumber=' + this.get('pageNumber'));
  470. param.push('pageSize=' + this.get('pageSize'));
  471. param.push('sortField=' + this.get('sortField'));
  472. param.push('sortOrder=' + this.get('sortOrder'));
  473. break;
  474. }
  475. return param.join('&');
  476. },
  477. setSort: function setSort(inputVal) {
  478. if (!inputVal) {
  479. return;
  480. }
  481. var data = inputVal.split('|');
  482. this.set('sortField', data[0]);
  483. this.set('sortOrder', data[1]);
  484. },
  485. resetPage: function resetPage() {
  486. this.set('pageNumber', 1);
  487. },
  488. updateFiltersState: function updateFiltersState() {
  489. var checkedTypes = masterFilters.get('mainFilters').get('types_bien').get('criterias').get('types_bien').get('value').split(',');
  490. if (_.intersection(['', 'Appartement', 'Maison/Villa', 'Loft/Atelier/Surface', 'Immeuble'], checkedTypes).length == 0) {
  491. // Terrain, Parking/Box, Local ou Bureaux
  492. if (_.indexOf(['Terrain', 'Parking/Box', 'Terrain,Parking/Box'], this.get('mainFilters').get('types_bien').get('criterias').get('types_bien').get('value')) != -1) {
  493. this.get('otherFilters').get('pieces').set('isActive', false);
  494. this.get('otherFilters').get('equipement').set('isActive', false);
  495. this.get('otherFilters').get('autre').get('criterias').get('mobilite_reduite').set('isActive', false);
  496. } else {
  497. this.get('otherFilters').get('pieces').set('isActive', true);
  498. this.get('otherFilters').get('equipement').set('isActive', true);
  499. this.get('otherFilters').get('autre').get('criterias').get('mobilite_reduite').set('isActive', true);
  500. }
  501. this.get('otherFilters').get('defiscalisation').set('isActive', false);
  502. } else {
  503. this.get('otherFilters').get('pieces').set('isActive', true);
  504. this.get('otherFilters').get('equipement').set('isActive', true);
  505. this.get('otherFilters').get('defiscalisation').set('isActive', true);
  506. this.get('otherFilters').get('autre').get('criterias').get('mobilite_reduite').set('isActive', true);
  507. }
  508. if (this.get('mainFilters').get('projet').get('criterias').get('type_commercialisation').get('value') == 'Vente') {
  509. this.get('mainFilters').get('types_bien').get('criterias').get('types_bien').get('inputChoices').get('terrain').set('isActive', true);
  510. this.get('otherFilters').get('budget').get('criteriaSelector').set('isActive', true);
  511. if (this.get('mainFilters').get('categories').get('criterias').get('anciennete').get('value') == '1') {
  512. this.get('otherFilters').get('autre').get('criterias').get('bbc').set('isActive', false);
  513. var fiscalCrit = this.get('otherFilters').get('defiscalisation').get('criterias');
  514. fiscalCrit.get('scellier').set('isActive', false);
  515. fiscalCrit.get('lmnp').set('isActive', false);
  516. fiscalCrit.get('censi').set('isActive', false);
  517. fiscalCrit.get('tva_reduite').set('isActive', false);
  518. this.get('otherFilters').get('disponibilite').set('isActive', false);
  519. } else {
  520. if (_.intersection(['', 'Appartement', 'Maison/Villa', 'Loft/Atelier/Surface'], checkedTypes).length > 0) {
  521. this.get('otherFilters').get('autre').get('criterias').get('bbc').set('isActive', true);
  522. var fiscalCrit = this.get('otherFilters').get('defiscalisation').get('criterias');
  523. fiscalCrit.get('scellier').set('isActive', true);
  524. fiscalCrit.get('lmnp').set('isActive', true);
  525. fiscalCrit.get('censi').set('isActive', true);
  526. fiscalCrit.get('tva_reduite').set('isActive', true);
  527. this.get('otherFilters').get('disponibilite').set('isActive', true);
  528. } else {
  529. this.get('otherFilters').get('autre').get('criterias').get('bbc').set('isActive', false);
  530. var fiscalCrit = this.get('otherFilters').get('defiscalisation').get('criterias');
  531. fiscalCrit.get('scellier').set('isActive', false);
  532. fiscalCrit.get('lmnp').set('isActive', false);
  533. fiscalCrit.get('censi').set('isActive', false);
  534. fiscalCrit.get('tva_reduite').set('isActive', false);
  535. if (_.indexOf(['Terrain'], this.get('mainFilters').get('types_bien').get('criterias').get('types_bien').get('value')) != -1) {
  536. this.get('otherFilters').get('disponibilite').set('isActive', true);
  537. } else {
  538. this.get('otherFilters').get('disponibilite').set('isActive', false);
  539. }
  540. }
  541. }
  542. } else {
  543. this.get('mainFilters').get('types_bien').get('criterias').get('types_bien').get('inputChoices').get('terrain').set('isChecked', false);
  544. this.get('mainFilters').get('types_bien').get('criterias').get('types_bien').get('inputChoices').get('terrain').set('isActive', false);
  545. this.get('otherFilters').get('budget').get('criteriaSelector').set('value', 'global');
  546. this.get('otherFilters').get('budget').get('criteriaSelector').set('isActive', false);
  547. this.get('otherFilters').get('autre').get('criterias').get('bbc').set('isActive', false);
  548. var fiscalCrit = this.get('otherFilters').get('defiscalisation').get('criterias');
  549. fiscalCrit.get('scellier').set('isActive', false);
  550. fiscalCrit.get('lmnp').set('isActive', false);
  551. fiscalCrit.get('censi').set('isActive', false);
  552. fiscalCrit.get('tva_reduite').set('isActive', false);
  553. if (this.get('mainFilters').get('categories').get('criterias').get('anciennete').get('value') == '1') {
  554. this.get('otherFilters').get('disponibilite').set('isActive', false);
  555. } else {
  556. this.get('otherFilters').get('disponibilite').set('isActive', true);
  557. }
  558. }
  559. },
  560. updateBudgetValues: function updateBudgetValues() {
  561. var typeCom = this.get('mainFilters').get('projet').get('criterias').get('type_commercialisation').get('value');
  562. var minBudget = this.get('otherFilters').get('budget').get('criterias').get('min_budget');
  563. minBudget.set('value', minBudget.get('default' + typeCom));
  564. minBudget.set('step', minBudget.get('step' + typeCom));
  565. minBudget.reinitialize();
  566. var maxBudget = this.get('otherFilters').get('budget').get('criterias').get('max_budget');
  567. maxBudget.set('value', maxBudget.get('default' + typeCom));
  568. maxBudget.set('step', maxBudget.get('step' + typeCom));
  569. maxBudget.reinitialize();
  570. },
  571. updateNbFilters: function updateNbFilters() {
  572. var nb = this.get('otherFilters').length - (this.get('otherFilters').where({ value: '(vide)' }).length + this.get('otherFilters').where({ value: 'Tous' }).length);
  573. if (nb > 0) {
  574. $('.nb-filters').html('<span>' + nb + '</span>');
  575. } else {
  576. $('.nb-filters').html('');
  577. }
  578. }
  579. });
  580. // Recherche
  581. var Pagination = Backbone.Model.extend({});
  582. var Bien = Backbone.Model.extend({});
  583. var Result = Backbone.Model.extend({
  584. defaults: {
  585. bien: null,
  586. center_x: '',
  587. center_y: '',
  588. polygone: '',
  589. c_prix_min: 0,
  590. distance: 0,
  591. p_nb_piece_min: 0,
  592. p_nb_lots: 0
  593. },
  594. initialize: function initialize(data) {
  595. this.set('bien', new Bien(data[0]));
  596. this.unset(0);
  597. }
  598. });
  599. var ResultCollection = Backbone.Collection.extend({
  600. model: Result
  601. });
  602. var SearchResult = Backbone.Model.extend({
  603. defaults: {
  604. searchUrl: '',
  605. isLoaded: false,
  606. lastMode: SERIALIZE_DEFAULT,
  607. historyAction: 'replace',
  608. locationChanged: false,
  609. pagination: {},
  610. count: 0,
  611. results: [],
  612. alentourNeufIds: [],
  613. pushNeufIds: [],
  614. enlargeType: '',
  615. resultMapListPseudoView: false
  616. },
  617. initialize: function initialize(searchUrl) {
  618. this.set('searchUrl', searchUrl);
  619. this.on("sync", function (_this) {
  620. _this.set('isLoaded', true);
  621. window.searchResults = _this;
  622. switch (Backbone.history.getFragment()) {
  623. case 'map':
  624. // if (!_this.resultMapListPseudoView) {
  625. // let resultMapListPseudoView = new ResultMapListPseudoView({model: _this})
  626. // _this.resultMapListPseudoView = resultMapListPseudoView
  627. // _this.resultMapListPseudoView.render();
  628. // }
  629. window.currentMapManager.initMap();
  630. //$.cookie("mapDefault", 1, { path: '/' });
  631. //localStorage.setItem('mapDefault', 1);
  632. break;
  633. case '':
  634. case 'list':
  635. var resultListPseudoView = new ResultListPseudoView({ model: _this });
  636. resultListPseudoView.render();
  637. var paginationPseudoView = new PaginationPseudoView({ model: _this.get('pagination') });
  638. paginationPseudoView.render();
  639. switch (searchResults.get('historyAction')) {
  640. case 'push':
  641. Backbone.history.history.pushState({ type: 'push', serializedFilters: JSON.stringify(masterFilters) }, 'maj_url', "?" + masterFilters.serialize(SERIALIZE_FOR_URL) + '#list');
  642. break;
  643. case 'replace':
  644. Backbone.history.history.replaceState({ type: 'replace', serializedFilters: JSON.stringify(masterFilters) }, 'maj_url', "?" + masterFilters.serialize(SERIALIZE_FOR_URL) + '#list');
  645. break;
  646. }
  647. if (_this.get('locationChanged')) {
  648. $('#search-infos').css('display', 'none');
  649. $('.search-metadata .search-label h1').html(_this.get('title'));
  650. }
  651. $('a.ajax_new_searches').attr('data-name', AlertManager.getAlerteName());
  652. break;
  653. }
  654. _this.switchLoader(true);
  655. });
  656. this.on("error", function (_this) {
  657. modal_msg("Une erreur est survenue lors du chargement des données.<br><br>Veuillez nous excuser pour la gêne occasionnée");
  658. _this.switchLoader(true);
  659. });
  660. this.on("change:searchUrl", function (_this) {
  661. _this.set('isLoaded', false);
  662. _this.fetch();
  663. _this.switchLoader(false);
  664. $('body').animate({ scrollTop: 0 });
  665. var xitiBudgetMax = xitiPiecesMin = xitiSurfaceMin = '';
  666. var xitiLocalisation = '';
  667. var xitiAncienete = 'Tous';
  668. var xitiTypeCommercialisation = 'Louer';
  669. if (masterFilters.get('mainFilters').get('projet').get('criterias').get('type_commercialisation').get('value') == 'Vente') {
  670. xitiTypeCommercialisation = 'Acheter';
  671. }
  672. if (masterFilters.get('mainFilters').get('categories').get('criterias').get('anciennete').get('value') == 1) {
  673. xitiAncienete = 'Ancien';
  674. } else if (masterFilters.get('mainFilters').get('categories').get('criterias').get('anciennete').get('value') == 2) {
  675. xitiAncienete = 'Neuf';
  676. }
  677. if (masterFilters.get('otherFilters').get('budget').get('criteriaSelector').get('isActive')) {
  678. xitiBudgetMax = masterFilters.get('otherFilters').get('budget').get('criterias').get('max_budget').get('value');
  679. }
  680. if (masterFilters.get('otherFilters').get('pieces').get('isActive')) {
  681. xitiPiecesMin = masterFilters.get('otherFilters').get('pieces').get('criterias').get('min_piece').get('value');
  682. }
  683. if (masterFilters.get('otherFilters').get('surface').get('isActive')) {
  684. xitiSurfaceMin = masterFilters.get('otherFilters').get('surface').get('criterias').get('min_surface').get('value');
  685. }
  686. tc_events_5(this, '10', { 'label': tc_vars["xiti_xtpage"], 'xtmed_s2': '12', 'xtmed_type': 'P', 'x1': xitiSurfaceMin, 'x16': xitiLocalisation, 'x17': xitiTypeCommercialisation, 'x18': xitiPiecesMin, 'x19': xitiBudgetMax, 'x20': xitiAncienete });
  687. sessionStorage.setItem('masterFilters', JSON.stringify(masterFilters));
  688. popinMkt();
  689. });
  690. },
  691. url: function url() {
  692. return this.get('searchUrl');
  693. },
  694. parse: function parse(response) {
  695. response.pagination = new Pagination(response.pagination);
  696. response.results = new ResultCollection(response.results);
  697. return response;
  698. },
  699. switchLoader: function switchLoader(display) {
  700. if (display) {
  701. $('.loader').removeClass('active');
  702. $('body').removeClass('loading');
  703. } else {
  704. $('.loader').addClass('active');
  705. $('body').addClass('loading');
  706. }
  707. },
  708. reload: function reload(serialize_mode, extra, historyAction) {
  709. if (!historyAction) {
  710. historyAction = 'replace';
  711. }
  712. this.set('historyAction', historyAction);
  713. this.set('lastMode', serialize_mode);
  714. this.set('searchUrl', urlSearchAPI + "?" + masterFilters.serialize(serialize_mode, extra));
  715. }
  716. });
  717. var Favori = Backbone.Model.extend({
  718. defaults: {
  719. id: 0,
  720. native_id: '',
  721. reference: '',
  722. type: ''
  723. }
  724. });
  725. var FavoriCollection = Backbone.Collection.extend({
  726. model: Favori
  727. });
  728. var Favoris = Backbone.Model.extend({
  729. defaults: {
  730. favIds: [],
  731. favoris: []
  732. },
  733. url: '/mon-compte/mes-favoris.json?type=bien',
  734. initialize: function initialize() {
  735. this.reload();
  736. },
  737. parse: function parse(response) {
  738. this.set('favoris', new FavoriCollection(response));
  739. var favIds = [];
  740. _.each(response, function (fav) {
  741. favIds.push(fav.reference);
  742. });
  743. this.set('favIds', favIds);
  744. },
  745. reload: function reload() {
  746. if ($('header a.loginAction').hasClass('logged')) {
  747. this.fetch();
  748. }
  749. }
  750. });
  751. // Display the popin marketing on page loaded
  752. $(function () {
  753. popinMkt();
  754. });