algolia wordpress autocomplete - wordpress

I'm trying to tweak the WordPress plugin https://github.com/algolia/algoliasearch-wordpress to suit our needs. What we want to be able to do is have a search result that will load the data up in the same page.
I have my WordPress posts and its data being indexed successfully. I have added a search box to the page, and autocomplete is being fired.
Out of the box, the WordPress plugin template wraps the result in an anchor tag and assigns it the URL of the found result. When clicked, this navigates you to that post. However, what I want to do is intercept that click and load the result in the same page without navigating away.
I have removed the href from the anchor. I have also edited the supplied autocomplete.php template where the call to autocomplete:selected occurs. In there I have removed the call to navigate away by removing window.location.href.
Now I have two main issues.
1 - When the user clicks the search result I would like the input to be populate with the title of the item they clicked on. I added this in the autocomplete:selected callback by adding $searchInput[0].value = suggestion.post_title. Which seems to change the value of the input correctly, but as soon as I click away from the input, it is re-set back to the original typed value. So if I type 'may' and click the result 'mayonnaise', the result data can be accessed but the input returns back to 'may'. My function looks this:
/* Instantiate autocomplete.js */
var autocomplete = algoliaAutocomplete($searchInput[0], config, sources)
.on('autocomplete:selected', function (e, suggestion) {
console.log(suggestion);
autocomplete.autocomplete.close();
});
2 - It seems that the autocomplete dropdown does not hide when the user clicks away. To resolve this i've had to use what I think is a bit of a nasty hack with jQuery. I was wondering if this is really required? My code just below the autocomplete:selected call looks like this:
jQuery('body').on("click", function(event){
if (!jQuery(event.target).closest($searchInput[0]).length) {
autocomplete.autocomplete.close();
}
});

Found some answers to my questions.
1 - In order to populate the input with the title of the selected search result I added a call to the setVal method of the autocomplete object. I'[m still not sure why this is required.
/* Instantiate autocomplete.js */
var autocomplete = algoliaAutocomplete($searchInput[0], config, sources)
.on('autocomplete:selected', function (e, suggestion) {
autocomplete.autocomplete.setVal(suggestion.post_title);
});
2 - It looks like the config of the autocomplete object uses the value of WP_DEBUG in order to set the debug value. The options available for the autocomplete component can be found here https://github.com/algolia/autocomplete.js#options. This lead me to find that when debug is set to true, the autocomplete box does not hide on selection. This is to allow for easier debugging and styling of the component.

Related

FlowRouter without page reload

I am following this example https://kadira.io/academy/meteor-routing-guide/content/rendering-blaze-templates
When I click on my links the whole page is being reloaded. Is there any way to load only the template part that is needed and not the whole page?
Edit: Also I noted another problem. Everything that is outside {{> Template.dynamic}} is being rendered twice.
Here is my project sample. https://github.com/hayk94/UbMvp/tree/routing
EDIT: Putting the contents in the mainLayout template and starting the rendering from there fixed the double render problems. However the reload problems happen because of this code
Template.mainLayout.events({
"click *": function(event, template){
event.stopPropagation();
console.log('body all click log');
// console.log(c0nnIp);
var clickedOne = $(event.target).html().toString();
console.log('This click ' + clickedOne);
//getting the connID
var clientIp = null // headers.getClientIP(); // no need for this anymore
var clientConnId = Meteor.connection._lastSessionId;
console.log(clientIp);
console.log(clientConnId);
Meteor.call("updateDB", {clientIp,clientConnId,clickedOne}, function(error, result){
if(error){
console.log("error", error);
}
if(result){
}
});
}, // click *
});//events
Without this event attached to the template the routing works without any reloads, however as soon as I attach it the problem persists.
Do you have any ideas why this code causes such problems?
EDIT 2 following question Rev 3:
event.stopPropagation() on "click *" event probably prevents the router from intercepting the click on link.
Then your browser performs the default behaviour, i.e. navigates to that link, reloading the whole page.
EDIT following question Rev 2:
Not sure you can directly use your body as BlazeLayout target layout.
Notice in the first code sample of BlazeLayout Usage that they use an actual template as layout (<template name="layout1">), targeted in JS as BlazeLayout.render('layout1', {});.
In the tutorial you mention, they similarly use <template name="mainLayout">.
That layout template is then appended to your page's body and filled accordingly. You can also change the placeholder for that layout with BlazeLayout.setRoot() by the way.
But strange things may happen if you try to directly target the body? In particular, that may explain why you have content rendered twice.
Original answer:
If your page is actually reloaded, then your router might not be configured properly, as your link is not being intercepted and your browser makes you actually navigate to that page. In that case, we would need to see your actual code if you need further help.
In case your page does not actually reload, but only your whole content is changed (whereas you wanted to change just a part of it), then you should make sure you properly point your dynamic templates.
You can refer to kadira:blaze-layout package doc to see how you set up different dynamic template targets in your layout, and how you can change each of them separately (or several of them simultaneously).
You should have something similar in case you use kadira:react-layout package.

CRM Ribbon Workbench - Hide + Button on Sub-Grid

I have a sub grid on a new entity called Issues, the sub grid is for another new entity called Cost Detail.
I have them both loaded into my solution called Issue, and have opened issue in the ribbon workbench.
What I want to do is when you are on the Issue form and can see the sub-grid I want to be able to hide the + button which is displayed. However when I have hidden this button in the ribbon workbench it also hides the add new button in the 'Associated View' therefore no records can be added.
How do I do it so that only the + button on the sub grid is hidden and not the other view?
EDIT:
In order to hide the add button, you either need to take away create privileges to the role that the user is in or you could do a hack(unsupported) like this:
function hideAddButton(){
var addButton = $('#NameOfGrid_addImageButton');
if(addButton.size())
addButton.hide();
else
setTimeout(hideAddButton, 1000);//checks every second to see if the button exists yet
}
and call the hideAddButton function on form load
There is one answer that I found. If you have a field that unique for that entity, then you can do it with a simple javascript code.
Here is steps that you must follow in ribbon workbench:
Right click the button and customise button.
Add an enable rule, in Steps section add an Custom Javascript Rule, that contains your library and function name, please make sure that default is true.
This must be in your javascirpt library :
function hideAddNew(){
if(Xrm.Page.getAttribute("yourField")){
return false;
}
else {
return true;
}
}
Add a command that contains the enable rule we created.
Add the command to button.
That's it. I test it, it is working.

How can I simulate a click event on an anchor tag without actually calling click or refreshing?

I am using meteor with iron router and meteors standard blaze tempting.
I have an ul set up so that when one of my li items is clicked its containing anchor tag is clicked on using the function click. The only problem is that when that a is clicked with the function it counts as another click on my li and my menu isn't toggling right.
I am wondering if there is a way to call the pathFor without actually calling a click function.
Thanks for any help!
Update, here is the code its working along with a pretty standard drop down nav. I am not sure if I can catch that event the same as a jquery event since I think it is the standard browser click event working on the [0], but I think I should be able to make a function that matches href to route and call Router.go
Template.nav.rendered = () ->
# set initial page view and take care of refeshes
currentPageHtlm = getCurrentPage(window.location.pathname)
$('#currentPage').find('span').html(currentPageHtlm)
$("li").on "click", () ->
#simulate anchor click and set currentPage session
$(this).find("a")[0].click()
Session.set 'pageName', getCurrentPage($(this).find("a").attr("href"))
currentPageHtlm = Session.get 'pageName'
$('#currentPage').find('span').html(currentPageHtlm)
# toggle nav and arrow if mobile view
if $('.smOnly').css('display') != 'none'
$('#nav').slideToggle('slow')
$('#dropArrow').toggleClass('fa-caret-square-o-down fa-caret-square-o-up')
It sounds like you're looking for a way to programatically redirect from JS rather than via UI interaction, in which case you need to use Router.go('ROUTE_NAME'), as per the docs (you can use the route name as in pathFor as an alternative to supplying the actual path).

ASP.NET MVC cancel, not delete

I have two questions, both related to the same view: so there is view called ProductDetails which shows the details of a product.
Each product can have the status:
Available - in this case, two button are available "edit" and "remove"(which will change the status of the product to "Not available" but will not remove it from DB)
Not available - in this case, the page displays the product but no options to edit or remove are
visible.
The controller ProductsController has an action Details that shows that view.
The problem is that I don't know how to implement the two buttons (Edit and Remove) because:
Edit sends to another action method (Edit which display another view) <- this works
Remove should do (IMO) a post on the current page. In the post action, the status of the product is changed and the view is shown again.
I want both button to look like links. If I put a form for remove, then it will be displayed as a button. I would like to avoid making the button look like a link with css. Or... at least I want to use the same HTML element for both 'buttons'.
This is more an issue of displaying the elements so I have added the CSS tag to your question as some alternative answers may rely on this.
Personally I think trying to make a button look like a text link would be quite awkward, even once you turn off the border and background you have issues with lining up the text etc.
I'd say you have 2 "simple" options.
Firstly you could make the delete not post a delete request but link to a delete confirmation page (or bring up a JS modal window with your delete form and button).
Secondly you could make them both look like buttons, while you requested that it looks like a link I figured that the main point was consistency in UI than the link look specifically. You could use JQueryUI and invoke .button() on both elements, invoking JQueryUI for 1 feature is a bit overkill but it's a quick change, of course you could replicate the same idea of styling the link like the buttons but would have to spend time dealing with browser CSS issues.
the Remove link should post to the Remove action, which should in turn (after validation and DB update) redirect to the details action.
public ActionResult Details(int productId)
{
// Your current action method
return View(model);
}
public ActionResult Remove(int productId)
{
// Validate productId
// Update DB
return RedirectToAction("Details", new { productId = productId } );
}
You easily can solve your link vs button problem by using a GET instead of a POST. Don't be blinded by best practices.
Or you can use a Remove link that executes a one-liner Javascript function that posts the form:
Remove

JQuery Simplemodal and Tabs Help Needed

I've got an asp.net page containing a Textbox with an Autocomplete extender on it.
It's setup so the user can type a short reference code into the textbox and then choose from the list of matching codes returned by the autocomplete.
On the "select", I then call the server using JQuery. I'm currently using $.get here....
The callback function from $.get checks for "success" and then displays a simple-modal dialog containing info about the item they've just selected.
if (sStatus == "success") {
$.modal(sText, {
overlayClose: true,
appendTo:'form',
onShow: function(dialog) {
$("#ccTargets_tabContainer").tabs();
},
onClose: function(dialog) {
$("#<%=TextBox1.ClientID%>").val("");
$.modal.close();
}
});
$.ready();
}
One of the bits of info being loaded here is a JQuery TABS setup, so the onShow function of the simplemodal is used to initiate the tabs which are within the simplemodal.
Now to the crux of my problem.
If I do multiple consecutive "autocompletes" on the same page it all works fine Unless I have selected a different tab on the tabs in the simplemodal ....If I select a different tab, close the simplemodal and then do another autocomplete I get a JQuery error which seems to relate to a selector doing something with the "old" selected tab that was on the "closed" modal.
I'm clearly missing some sort of cleardown / initialisation somewhere, but can't find what it is. Help?
I've tried "tabs.destroy" before the modal call in the code above and I've tried a $.ready() call as indicated too....
UPDATE: Is it something to do with JQuery Tabs appending my addressbar URL with the selected tab's ID?
I've found the problem.
It's with the "history" script that the tabs plugin normally uses. Obviously as I am continually creating and destroying popups there is no history to speak of - it's all done outside of the normal app navigation.
I've removed the jquery.history_remote script and now it works just great!
Dave

Resources