PageRenderTime 34ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

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