From 578c6aa2981781cfb99964dc36950e2c54ce461c Mon Sep 17 00:00:00 2001 From: Florian BLOUET Date: Wed, 17 Feb 2016 11:22:28 +0100 Subject: [PATCH] PHRAS-974 - PHRAS-974 - fix publication module --- resources/gulp/components/prod.js | 3 +- .../www/prod/js/components/publication.js | 360 ++++++++++++++++++ resources/www/prod/js/jquery.main-prod.js | 331 +--------------- templates/web/prod/results/feeds.html.twig | 2 +- .../web/prod/results/feeds_entry.html.twig | 4 +- 5 files changed, 367 insertions(+), 333 deletions(-) create mode 100644 resources/www/prod/js/components/publication.js diff --git a/resources/gulp/components/prod.js b/resources/gulp/components/prod.js index ce7bd11c61..e0bbcf7ee0 100644 --- a/resources/gulp/components/prod.js +++ b/resources/gulp/components/prod.js @@ -73,6 +73,7 @@ gulp.task('build-prod-js', function(){ config.paths.vendors + 'jquery-file-upload/js/jquery.iframe-transport.js', config.paths.vendors + 'jquery-file-upload/js/jquery.fileupload.js', config.paths.vendors + 'geonames-server-jquery-plugin/jquery.geonames.js', + config.paths.src + 'prod/js/components/publication.js', config.paths.src + 'prod/js/jquery.form.2.49.js', config.paths.src + 'prod/js/jquery.Selection.js', config.paths.src + 'prod/js/jquery.Edit.js', @@ -116,4 +117,4 @@ gulp.task('watch-prod-css', function() { gulp.task('build-prod', ['copy-prod-images', 'build-prod-css'], function(){ debugMode = false; return gulp.start('build-prod-js'); -}); \ No newline at end of file +}); diff --git a/resources/www/prod/js/components/publication.js b/resources/www/prod/js/components/publication.js new file mode 100644 index 0000000000..6498a78fd9 --- /dev/null +++ b/resources/www/prod/js/components/publication.js @@ -0,0 +1,360 @@ +var publicationModule = (function () { + + var ajaxState = { + query: null, + isRunning: false + }; + + var curPage; + var $answers = $('#answers'); + + // refresh current view + $answers.on('click', '.feed_reload', function (event) { + event.preventDefault(); + fetchPublications(curPage) + }); + + // navigate to a specific feed + $answers.on('click', '.ajax_answers', function (event) { + event.preventDefault(); + var $this = $(this); + var append = $this.hasClass('append'); + var noScroll = $this.hasClass('no_scroll'); + + _fetchRemote($(event.currentTarget).attr('href'), {}) + .then(function (data) { + if (!append) { + $answers.empty(); + if (!noScroll) { + $answers.scrollTop(0); + } + $answers.append(data); + + $answers.find("img.lazyload").lazyload({ + container: $answers + }); + } + else { + $('.see_more.loading', $answers).remove(); + $answers.append(data); + + $answers.find("img.lazyload").lazyload({ + container: $answers + }); + + if (!noScroll) { + $answers.animate({ + 'scrollTop': ($answers.scrollTop() + $answers.innerHeight() - 80) + }); + } + } + afterSearch(); + }); + + }); + + // subscribe_rss + $answers.on('click', '.subscribe_rss', function (event) { + event.preventDefault(); + var $this = $(this); + + if (typeof(renew) === 'undefined') + renew = 'false'; + else + renew = renew ? 'true' : 'false'; + + var buttons = {}; + buttons[language.renewRss] = function () { + $this.trigger({ + type: 'click', + renew: true + }); + }; + buttons[language.fermer] = function () { + $('#DIALOG').empty().dialog('destroy'); + }; + + event.stopPropagation(); + + $.ajax({ + type: "GET", + url: $this.attr('href') + (event.renew === true ? '?renew=true' : ''), + dataType: 'json', + success: function (data) { + if (data.texte !== false && data.titre !== false) { + if ($("#DIALOG").data("ui-dialog")) { + $("#DIALOG").dialog('destroy'); + } + $("#DIALOG").attr('title', data.titre) + .empty() + .append(data.texte) + .dialog({ + autoOpen: false, + closeOnEscape: true, + resizable: false, + draggable: false, + modal: true, + buttons: buttons, + width: 650, + height: 250, + overlay: { + backgroundColor: '#000', + opacity: 0.7 + } + }).dialog('open'); + + } + } + }); + }); + + // edit a feed + $answers.on('click', '.feed .entry a.feed_edit', function () { + var $this = $(this); + $.ajax({ + type: "GET", + url: $this.attr('href'), + dataType: 'html', + success: function (data) { + return _createPublicationModal(data); + } + }); + return false; + }); + + // remove a feed + $answers.on('click', '.feed .entry a.feed_delete', function () { + if (!confirm('etes vous sur de vouloir supprimer cette entree ?')) + return false; + var $this = $(this); + $.ajax({ + type: "POST", + url: $this.attr('href'), + dataType: 'json', + success: function (data) { + if (data.error === false) { + var $entry = $this.closest('.entry'); + $entry.animate({ + height: 0, + opacity: 0 + }, function () { + $entry.remove(); + }); + } + else + alert(data.message); + } + }); + return false; + }); + + + $answers.on('mouseover', '.feed .entry', function () { + $(this).addClass('hover'); + }); + + $answers.on('mouseout', '.feed .entry', function () { + $(this).removeClass('hover'); + }); + + $answers.on('click', '.see_more a', function (event) { + $see_more = $(this).closest('.see_more'); + $see_more.addClass('loading'); + }); + + + var _fetchRemote = function (url, data) { + var page = 0; + if (data.page === undefined) { + page = data.page; + } + + return ajaxState.query = $.ajax({ + type: "GET", + url: url, + dataType: 'html', + data: data, + beforeSend: function () { + if (ajaxState.isRunning && ajaxState.query.abort) + answAjax.abort(); + if (page === 0) + clearAnswers(); + ajaxState.isRunning = true; + $answers.addClass('loading'); + }, + error: function () { + ajaxState.isRunning = false; + $answers.removeClass('loading'); + }, + timeout: function () { + ajaxState.isRunning = false; + $answers.removeClass('loading'); + }, + success: function (data) { + ajaxState.isRunning = false; + } + }); + }; + + var _createPublicationModal = function (data) { + + var buttons = {}; + buttons[language.valider] = function () { + var dialog = p4.Dialog.get(1); + var error = false; + var $form = $('form.main_form', dialog.getDomElement()); + + $('.required_text', $form).each(function (i, el) { + if ($.trim($(el).val()) === '') { + $(el).addClass('error'); + error = true; + } + }); + + if (error) { + alert(language.feed_require_fields); + } + + if ($('input[name="feed_id"]', $form).val() === '') { + alert(language.feed_require_feed); + error = true; + } + + if (error) { + return false; + } + + $.ajax({ + type: 'POST', + url: $form.attr('action'), + data: $form.serializeArray(), + dataType: 'json', + beforeSend: function () { + $('button', dialog.getDomElement()).prop('disabled', true); + }, + error: function () { + $('button', dialog.getDomElement()).prop('disabled', false); + }, + timeout: function () { + $('button', dialog.getDomElement()).prop('disabled', false); + }, + success: function (data) { + $('button', dialog.getDomElement()).prop('disabled', false); + if (data.error === true) { + alert(data.message); + return; + } + + if ($('form.main_form', dialog.getDomElement()).hasClass('entry_update')) { + var id = $('form input[name="entry_id"]', dialog.getDomElement()).val(); + var container = $('#entry_' + id); + + container.replaceWith(data.datas); + + container.hide().fadeIn(); + + $answers.find("img.lazyload").lazyload({ + container: $answers + }); + } + + p4.Dialog.Close(1); + } + }); + p4.Dialog.Close(1); + }; + + var dialog = p4.Dialog.Create({ + size: 'Full', + closeOnEscape: true, + closeButton: true, + buttons: buttons + }); + + dialog.setContent(data); + + var $feeds_item = $('.feeds .feed', dialog.getDomElement()); + var $form = $('form.main_form', dialog.getDomElement()); + + $feeds_item.bind('click', function () { + $feeds_item.removeClass('selected'); + $(this).addClass('selected'); + $('input[name="feed_id"]', $form).val($('input', this).val()); + }).hover(function () { + $(this).addClass('hover'); + }, function () { + $(this).removeClass('hover'); + }); + + $form.bind('submit', function () { + return false; + }); + + return; + }; + + var fetchPublications = function (page) { + curPage = page; + return _fetchRemote('../prod/feeds/', { + page: page + }) + .then(function (data) { + $('.next_publi_link', $answers).remove(); + + $answers.append(data); + + $answers.find("img.lazyload").lazyload({ + container: $answers + }); + + afterSearch(); + if (page > 0) { + $answers.stop().animate({ + scrollTop: $answers.scrollTop() + $answers.height() + }, 700); + } + return; + }); + }; + + + var publishRecords = function (type, value) { + var options = { + lst: '', + ssel: '', + act: '' + }; + + switch (type) { + case "IMGT": + case "CHIM": + options.lst = value; + break; + + case "STORY": + options.story = value; + break; + case "SSTT": + options.ssel = value; + break; + } + + $.post("../prod/feeds/requestavailable/" + , options + , function (data) { + + return _createPublicationModal(data); + }); + + return; + }; + + return { + fetchPublications: function (page) { + return fetchPublications(page); + }, + publishRecords: function (type, value) { + return publishRecords(type, value) + } + }; +})(); diff --git a/resources/www/prod/js/jquery.main-prod.js b/resources/www/prod/js/jquery.main-prod.js index 1dd7cf0edb..6cc493ae01 100644 --- a/resources/www/prod/js/jquery.main-prod.js +++ b/resources/www/prod/js/jquery.main-prod.js @@ -32,51 +32,7 @@ function getHome(cas, page) { newSearch($("#EDIT_query").val()); break; case 'PUBLI': - answAjax = $.ajax({ - type: "GET", - url: "../prod/feeds/", - dataType: 'html', - data: { - page: page - }, - beforeSend: function () { - if (answAjaxrunning && answAjax.abort) - answAjax.abort(); - if (page === 0) - clearAnswers(); - answAjaxrunning = true; - $('#answers').addClass('loading'); - }, - error: function () { - answAjaxrunning = false; - $('#answers').removeClass('loading'); - }, - timeout: function () { - answAjaxrunning = false; - $('#answers').removeClass('loading'); - }, - success: function (data) { - answAjaxrunning = false; - var answers = $('#answers'); - - $('.next_publi_link', answers).remove(); - - answers.append(data); - - answers.find("img.lazyload").lazyload({ - container: answers - }); - - afterSearch(); - if (page > 0) { - answers.stop().animate({ - scrollTop: answers.scrollTop() + answers.height() - }, 700); - } - return; - } - - }); + publicationModule.fetchPublications(page, answAjax, answAjaxrunning); break; case 'HELP': $.ajax({ @@ -1146,122 +1102,6 @@ $(document).ready(function () { $(this).highlight('#CCCCCC'); }); - $('#answers .see_more a').on('click', function (event) { - $see_more = $(this).closest('.see_more'); - $see_more.addClass('loading'); - }); - - $('#answers .feed .entry').on('mouseover', function () { - $(this).addClass('hover'); - }); - $('#answers .feed .entry').on('mouseout', function () { - $(this).removeClass('hover'); - }); - - $('a.ajax_answers').on('click', function (event) { - event.stopPropagation(); - var $this = $(this); - - var append = $this.hasClass('append'); - var no_scroll = $this.hasClass('no_scroll'); - - $.ajax({ - type: "GET", - url: $this.attr('href'), - dataType: 'html', - success: function (data) { - var $answers = $('#answers'); - - if (!append) { - $answers.empty(); - if (!no_scroll) { - $answers.scrollTop(0); - } - $answers.append(data); - - $answers.find("img.lazyload").lazyload({ - container: $answers - }); - } - else { - $('.see_more.loading', $answers).remove(); - $answers.append(data); - - $answers.find("img.lazyload").lazyload({ - container: $answers - }); - - if (!no_scroll) { - $answers.animate({ - 'scrollTop': ($answers.scrollTop() + $answers.innerHeight() - 80) - }); - } - } - afterSearch(); - } - }); - - return false; - }); - - - $('a.subscribe_rss').on('click', function (event) { - - var $this = $(this); - - if (typeof(renew) === 'undefined') - renew = 'false'; - else - renew = renew ? 'true' : 'false'; - - var buttons = {}; - buttons[language.renewRss] = function () { - $this.trigger({ - type: 'click', - renew: true - }); - }; - buttons[language.fermer] = function () { - $('#DIALOG').empty().dialog('destroy'); - }; - - event.stopPropagation(); - var $this = $(this); - - $.ajax({ - type: "GET", - url: $this.attr('href') + (event.renew === true ? '?renew=true' : ''), - dataType: 'json', - success: function (data) { - if (data.texte !== false && data.titre !== false) { - if ($("#DIALOG").data("ui-dialog")) { - $("#DIALOG").dialog('destroy'); - } - $("#DIALOG").attr('title', data.titre) - .empty() - .append(data.texte) - .dialog({ - autoOpen: false, - closeOnEscape: true, - resizable: false, - draggable: false, - modal: true, - buttons: buttons, - width: 650, - height: 250, - overlay: { - backgroundColor: '#000', - opacity: 0.7 - } - }).dialog('open'); - - } - } - }); - - return false; - }); - $('#search_submit').on('mousedown', function (event) { return false; }); @@ -1684,44 +1524,6 @@ $(document).ready(function () { $(this).select(); }); - $('#answers .feed .entry a.options').on('click', function () { - var $this = $(this); - $.ajax({ - type: "GET", - url: $this.attr('href'), - dataType: 'html', - success: function (data) { - return set_up_feed_box(data); - } - }); - return false; - }); - $('#answers .feed .entry a.feed_delete').on('click', function () { - if (!confirm('etes vous sur de vouloir supprimer cette entree ?')) - return false; - var $this = $(this); - $.ajax({ - type: "POST", - url: $this.attr('href'), - dataType: 'json', - success: function (data) { - if (data.error === false) { - var $entry = $this.closest('.entry'); - $entry.animate({ - height: 0, - opacity: 0 - }, function () { - $entry.remove(); - }); - } - else - alert(data.message); - } - }); - return false; - }); - - $('#loader_bar').stop().animate({ width: '100%' }, 450, function () { @@ -1730,7 +1532,6 @@ $(document).ready(function () { }); }); - }); @@ -2158,44 +1959,13 @@ function activeIcons() { } if (value !== '') { - feedThis(type, value); + publicationModule.publishRecords(type, value); } else { alert(language.nodocselected); } }); - function feedThis(type, value) { - var options = { - lst: '', - ssel: '', - act: '' - }; - - switch (type) { - case "IMGT": - case "CHIM": - options.lst = value; - break; - - case "STORY": - options.story = value; - break; - case "SSTT": - options.ssel = value; - break; - } - - $.post("../prod/feeds/requestavailable/" - , options - , function (data) { - - return set_up_feed_box(data); - }); - - return; - } - $container.on('click', '.TOOL_chgcoll_btn', function () { var value = {}; @@ -3027,104 +2797,7 @@ function autoorder() { } -function set_up_feed_box(data) { - var buttons = {}; - buttons[language.valider] = function () { - var dialog = p4.Dialog.get(1); - var error = false; - var $form = $('form.main_form', dialog.getDomElement()); - - $('.required_text', $form).each(function (i, el) { - if ($.trim($(el).val()) === '') { - $(el).addClass('error'); - error = true; - } - }); - - if (error) { - alert(language.feed_require_fields); - } - - if ($('input[name="feed_id"]', $form).val() === '') { - alert(language.feed_require_feed); - error = true; - } - - if (error) { - return false; - } - - $.ajax({ - type: 'POST', - url: $form.attr('action'), - data: $form.serializeArray(), - dataType: 'json', - beforeSend: function () { - $('button', dialog.getDomElement()).prop('disabled', true); - }, - error: function () { - $('button', dialog.getDomElement()).prop('disabled', false); - }, - timeout: function () { - $('button', dialog.getDomElement()).prop('disabled', false); - }, - success: function (data) { - $('button', dialog.getDomElement()).prop('disabled', false); - if (data.error === true) { - alert(data.message); - return; - } - - if ($('form.main_form', dialog.getDomElement()).hasClass('entry_update')) { - var id = $('form input[name="entry_id"]', dialog.getDomElement()).val(); - var container = $('#entry_' + id); - - container.replaceWith(data.datas); - - container.hide().fadeIn(); - - var answers = $('#answers'); - - answers.find("img.lazyload").lazyload({ - container: answers - }); - } - - p4.Dialog.Close(1); - } - }); - p4.Dialog.Close(1); - }; - - var dialog = p4.Dialog.Create({ - size: 'Full', - closeOnEscape: true, - closeButton: true, - buttons: buttons - }); - - dialog.setContent(data); - - var $feeds_item = $('.feeds .feed', dialog.getDomElement()); - var $form = $('form.main_form', dialog.getDomElement()); - - $feeds_item.bind('click',function () { - $feeds_item.removeClass('selected'); - $(this).addClass('selected'); - $('input[name="feed_id"]', $form).val($('input', this).val()); - }).hover(function () { - $(this).addClass('hover'); - }, function () { - $(this).removeClass('hover'); - }); - - $form.bind('submit', function () { - return false; - }); - - return; -} //clear search diff --git a/templates/web/prod/results/feeds.html.twig b/templates/web/prod/results/feeds.html.twig index 79213a9ab3..bb98e01100 100644 --- a/templates/web/prod/results/feeds.html.twig +++ b/templates/web/prod/results/feeds.html.twig @@ -7,7 +7,7 @@

- + {% if feed.isAggregated %} {{ 'Toutes les publications' | trans }} - + - + {% endif %}