Ok, here's my issue. The following code works as intended, but I have this nagging feeling that I should be able to solve the problem more succinctly. I'm writing a script that will allow a person to pan/tilt/zoom an IP camera using custom controls on an html page. I've layed out the direction icons in a numberpad-style arrangement representing up, down, left-up etc... like so:
1 2 3
4 _ 6
7 8 9
When the user holds the mousedown on an icon, the img is swapped out for an active version and the command to begin the action is sent to a php cURL script, along with the respective direction (icon id). When the mouse is released the image is again swapped for the inactive version and the command is sent to the cURL script to stop moving in that same direction.
This works as long as the mouse is kept hovering over the same icon that was initially selected. If the person let the mouse leave the icon and then releases it, the second function checks to see if any of the directions are currently activated, inactivating them and sending a respective stop command to the cURL script.
Is there a way to actually accomplish this using one function?
// PTZ MOVEMENT / IMAGE SWAP
$('.nav-control').on('mousedown mouseup', '.ptz-cmd', function(e){
var thisCmd = $(this).attr('id'); // 1 - 9, designating numberpad style of movement
var thisAction = $(this).attr('action') // pantilt or zoom
if (e.type == 'mousedown') {
$(this).attr('src','img/' + thisCmd + 'h.png'); // example: 1h.png = active icon, 1b.png = inactive icon
$('#ptz').load("ptz.php?action=" + thisAction + "&cmd=" + thisCmd); // movement is handled by php cURL script and 'loaded' into a hidden div
} else {
$(this).attr('src','img/' + thisCmd + 'b.png');
$('#ptz').load("ptz.php?action=" + thisAction + "&cmd=stop"); // stop the movement or zoom for this direction...
}
});
// CANCEL MOVEMENT AND REPLACE IMAGE IF MOUSE LEAVES ICON AND IS RELEASED
$('.nav-control').on('mouseleave', '.ptz-cmd', function(e){
$('#ptz').load("ptz.php?action=pantilt&cmd=stop");
$('.ptz-cmd:not([action=preset])').each(function(){
if($(this).attr('src').substring(5) == "h.png"){
var whichDirection = $(this).attr('src').substring(0,5);
$(this).attr('src',whichDirection + 'b.png')
}
});
});
Absolutely:
var $ptz = $('#ptz');
$('.nav-control').on({
mousedown:function(){
var self = this,
thisCmd = self.id,
thisAction = self.action;
self.src = 'img/' + thisCmd + 'h.png';
$ptz.load("ptz.php?action=" + thisAction + "&cmd=" + thisCmd);
},
mouseup:function(){
var self = this,
thisCmd = self.id,
thisAction = self.action;
self.src = 'img/' + thisCmd + 'b.png';
$ptz.load("ptz.php?action=" + thisAction + "&cmd=stop");
},
mouseleave:function(){
$ptz.load("ptz.php?action=pantilt&cmd=stop");
$('.ptz-cmd').filter(':not([action=preset])').each(function(){
var self = this,
src = self.src;
if(src.substring(5) === 'h.png'){
self.src = src.substring(0,5) + 'b.png';
}
});
}
},'.ptz-cmd');
Changes:
By using the object form of .on(), you can consolidate these three events into a single binding
By splitting mousedown and mouseup, there is less runtime parsing (no checking events, less code per run of each, etc)
Using vanilla JS vs jQuery is faster, used for items like src,id, and action.
Use of === instead of == is more strict, and therefore more standards-compliant
Caching of $('#ptz') at top will save extra DOM scrapes
This isn't tested, but the theory behind it is sound. Even if you only take away the first point, you will have consolidated all your event bindings into a single call, and delegated appropriately.
Alternative:
var $ptz = $('#ptz');
$('.nav-control').on({
'mousedown mouseup':function(e){
var self = this,
cmdImg = self.id,
thisAction = self.action,
img = 'h',
thisCmd = cmdImg;
if(e.type === 'mouseup'){
img = 'b';
thisCmd = 'stop';
}
self.src = 'img/' + cmdImg + img + '.png';
$ptz.load("ptz.php?action=" + thisAction + "&cmd=" + thisCmd);
},
mouseleave:function(){
$ptz.load("ptz.php?action=pantilt&cmd=stop");
$('.ptz-cmd').filter(':not([action=preset])').each(function(){
var self = this,
src = self.src;
if(src.substring(5) === 'h.png'){
self.src = src.substring(0,5) + 'b.png';
}
});
}
},'.ptz-cmd');
This just maintains the mouseup and mousedown combination, checking the event type to see which it is. Its every-so-slightly slower, but consolidates the codebase into a single function, making maintenance a bit easier. Notice in this option when you have two different events you need to make it a string, e.g. 'mouseup mousedown' vs mouseup mousedown ... you can only use the object names if there is a single object.
Either one of these options should put you on the right track.
Related
I'm trying to implement SEO friendly infinite scrolling in accordance with google's recommendations as seen here (http://scrollsample.appspot.com/items?page=7). I have a jquery function that sends a request to a php file, (which requests the data from the db) anytime someone scrolls to the bottom of the page, now everything is working fine except that when the user scrolls to the bottom of the page, the request function gets fired more than once. So duplicate entries of the data gets loaded into the page, now i know this isn't from my php file because i opened the page directly in my browser and everything was fine. Checkout the bug here http://devx.dx.am/haze/categor.php?artemis=foo&&page=1
I have already tried the solutions here (jQuery .load() callback function fires multiple times) and here ($(window).load() is executing 2 times?) and a few others as well.
$(window).bind('scroll', function() { //#cagorwrap is the div that should contain the data retrieved
if($(window).scrollTop() >= $('#cagorwrap').offset().top + $('#cagorwrap').outerHeight() - window.innerHeight) { //344.6
var queryParameters = {}, queryString = location.search.substring(1),
re = /([^&=]+)=([^&]*)/g, m;
while (m = re.exec(queryString)) {
queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
var url2 = "modules/paginate.php?numpages=set";
// #rc is a hidden div too
$("#rc").load(url2, function() {
var rc = $(this).html();
if (queryParameters['page'] < rc) {
queryParameters['page']++;
console.log(rc);
var stateObj = queryParameters['page'];
let cagh = $('#cagorwrap').height() + 344.6 - 75;
$("#cagorwrap").height(cagh);
history.pushState(null, null, "categor.php?artemis=cat&&page="+stateObj);
var url = "modules/paginate.php?artemis=cats&&page="+stateObj;
$("#obtainer").load(url, function () {
$("#cagorwrap").append($(this).html());
}); //#obtainer is a hidden div that receives the data at first before it is appended to #cagorwrap
} else{
//unbind scroll here
}
});
}
});
well if all else fails and you absolutely need a solution, you can add a
counter=1; on the start
and only fire the request function in the case below
counter++;
if (counter%2==0){//fire request}
It's not clean, but if you're loosing too much time with this and want to return to the problem later on...
I want to implement a counter which keeps track of the number of times a file is downloaded. The function retrieves files from the google drive and it is printed on to the screen using for loop. Please help with a logic where to implement the counter to keep track of the count.
The function is as follows:
function makeRequest() {
var request = gapi.client.drive.files.list();
request.execute(function(resp) {
for (i=0; i<resp.items.length; i++) {
var titulo = resp.items[i].title;
var fechaUpd = resp.items[i].modifiedDate;
var userUpd = resp.items[i].lastModifyingUserName;
var userEmbed = resp.items[i].embedLink;
var userAltLink = resp.items[i].alternateLink;
var download = resp.items[i].webContentLink;
var hold="Download";
var flag=0;
<!-- var fileInfo = document.createElement('li');
<!-- fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd +' url: ' + hold.link(download)));
<!-- document.getElementById('content').appendChild(fileInfo);
document.write(titulo + " ");
document.write(hold.link(download) + "<br>");
<!--flag=1;
}
<!--if(flag!=1){
<!--document.write("not found!");
<!--}
}); ;
}
I am not sure I understand what you want to keep track off.
If you want to a single user to be able to see how many times that user has downloaded a file, it could be implemented in javascript and store the information in a cookie.
If you on the other hand want to see how many times a file has been downloaded by any user, it has to be implemented on the server side using PHP or whatever language you have available.
The key question here is if you can/want to implement the functions on the server or the client side. To do it on the client side you use javascript, to do it on the server side you can use PHP.
See this form - http://schnell.dreamhosters.com/form.php
This form has a portion of it where you enter data and can choose to add more of the same data by clicking a button called 'Add A Site' and it will make another of that section to enter another site. This is the jQuery that performs the duplication...
$(function () {
var sites = 1;
var siteform = $("#site1").html();
$(".addsites").live("click", function(e) {
e.preventDefault();
sites++;
$("#events").append("<div id='site" + sites + "'>"
+ "<br /><hr><br />"
+ siteform
+ "<center><button class='removesites' title='site"
+ sites + "'>Remove This Site</button><br />"
+ "<button class='addsites'>Add Another Site</button>"
+ "</center></div>");
});
$(".removesites").live("click", function(e) {
e.preventDefault();
var id = $(this).attr("title");
$("#" + id).remove();
});
});
The duplication works perfectly, but one thing that's bugging me is that when I have to enter data for someone claiming a LOT of sites, it gets very annoying having to repeat same or similar parts of this section of the form (like every site is in the same city, on the same day, by the same person, etc.) So I had the idea that with each duplication, the values of the form elements would also carry over and I just edit what's not the same. The current implementation only duplicates the elements, not the data. I'm not sure how to easily copy the data into new sections, and I can't find any jQuery tools to do that.
PS - This part isn't as important, but I've also considered using this same form to load the data back in for viewing/editing, etc. The only problem with this is that the reprinting of the form means that there will be a form section with the id "Site7" or something, but jQuery starts its numbering at 1, always. I've thought about using selectors to find the highest number site and start off the variable 'sites' at that number, but I'm not sure how. Any advice how to do this, or a better system overall, would be much appreciated.
You want to itterate over the input fields in siteform and store them in an object using their name attribute as a key.
Then after the duplication of the object you made and look for the equivelant fields in the new duplicated form ans set their values.
Somthing like this (not tested, just the idea)
var obj = new Object();
$("#site1 input").each(function(){
obj[this.id] = this.value;
);
// Dupicate form
$.each(obj, function(key, value){
$('#newform input[name="'+key+'"]').value = value;
});
Mind you these two each() functions differ from each other.
http://api.jquery.com/jQuery.each/
http://api.jquery.com/each/
You could consider using cloneNode to truely clone the previous site-div and (by passing true to cloneNode) all of its descendants and their attributes. Just know that the clone will have the same id as the original, so you'll have to manually set its id afterwards
Try this in your click-function
var clone = $("#site" + sites).clone(true, true); // clone the last div
sites++; // increment the number of divs
clone.attr('id', "site" + sites); // give the clone a unique id
$("#events").append(clone); // append it to the container
As Scuzzy points out in a comment jQuery does have its own clone() method (I don't use jQuery much, so I didn't know, and I didn't bother to check before answering). Probably better to use jQuery's method than the built-in cloneNode DOM method, since you're already using jQuery for event listeners. I've updated the code
The query to transfer values is quite simple (please, check the selector for all the right types on the form):
$("#site1").find("input[checked], input:text, input:hidden, input:password, input:submit, option:selected, textarea")
//.filter(":disabled")
.each(function()
{
$('#site2 [name="'+this.name+'"]').val(this.value);
}
Ok I finally figured this out. It's, more or less, an expansion on Alex Pakka's answer.
sites++;
$("#events").append("<div id='site" + sites + "'>"
+ "<hr><br />"
+ siteform
+ "<center><button class='removesites' title='site"
+ sites + "'>Remove This Site</button><br />");
$("#site1").find("input:checked, input:text, textarea, select").each(function() {
var name = $(this).attr("name");
var val = $(this).val();
var checked = $(this).attr("checked");
var selected = $(this).attr("selectedIndex");
$('#site' + sites + ' [name="'+name+'"]').val(val);
$('#site' + sites + ' [name="'+name+'"]').attr("checked", checked);
$('#site' + sites + ' [name="'+name+'"]').attr("selectedIndex", selected);
});
I used extra vars for readability sake, but it should do just as fine if you didn't and used the methods directly.
Dont forget to create a function for registering the event! Its very important because when the DOM is loaded, all new attributes need to be registrated to the DOM.
Small example:
<script>
$(document).ready(function(){
$('#click-me').click(function(){
registerClickEvent();
})
function registerClickEvent(){
$('<input type="text" name="input_field_example[]">').appendTo('#the-div-you-want')
}
registerClickEvent();
})
</script>
To follow on from a previous question here is a function used to determine what elements selected by Radio Buttons and Checkboxes are loaded on the right hand sign and what elements are loaded out via 'blank2.html'. The problem is it is loading the wrong elements in at the wrong points in time. Here is the code.
function product_analysis_global() {
$(':checked').each(function() {
var alt = $(this).attr('alt');
var title = $(this).attr('title');
if ($('#product_quantity_PRI_' + alt).attr('title') != title) {
$('#product_' + alt).load(title);
$('#product_quantity_PRI_' + alt).attr('title', title);
$('#product_quantity_PRI_' + alt).val($(this).val());
} else if ($('#product_quantity_PRI_' + alt).attr('title') != 'http://www.divethegap.com/update/blank2.html') {
$('#product_' + alt).load('http://www.divethegap.com/update/blank2.html');
$('#product_quantity_PRI_' + alt).attr('title', 'http://www.divethegap.com/update/blank2.html');
} else return false ;
});
}
Take a look at its implementation here and you will see precisely what I mean.
http://www.divethegap.com/update/diving-trips/adventure-training and the click on BEGINNERS to load in the form.
I believe the problem lies in the $(this).attr('alt') and $(this).attr('title') collecting only one attribute for the box checked not all of them.
An additional feature that I would like this code to have is to treat a disabled checkbox or radio button at an unchecked box and load it out via 'blank2.html'
$.load fires off an asynchronous request, therefore subsequent statements will be evaluated before the request finishes (at least in the vast majority of cases).
You will need to do what you want within its success callback, to ensure that it is guaranteed to happen once $.load has completed:
// make sure you make the current context available within the callback
var that = this;
$('#product_' + alt).load(title, function() {
$('#product_quantity_PRI_' + alt).attr('title', title);
$('#product_quantity_PRI_' + alt).val($(that).val());
});
I am working to an application that uses jqGrid. The problem is that the edit dialog that should appear at row edit must have a specific layout. So I would prefer to load it via ajax and then send the data back to jqGrid manually. I searched a lot on the forums but I could not find an example of how to do it.
So, I just need jqGrid to fill the edit dialog pop-up with custom content from a PHP script.
UPDATE: THe idea is that I have a form generator, where the user sets the position/width/heigh/visibility of the edit fields... and this must be used in the edit dialog.
You can use editfunc or addfunc option of the navGrid. If for example editfunc are defined then instead of editGridRow jqGrid will be called editfunc with the id of selected row as the parameter.
Alternative you can use custom button (see this answer as an example).
To modify data in the table after ther custom edit dialog you can use setRowData function.
UPDATED: If you need just make some modification of layout of the edit dialog you can use beforeShowForm for th modifications.
You can check this Tutorial, which is the official demo website of jqGrid Plugin. I am sure that there are examples of some "Row Editing" in that category. You can view lots of other examples of jqGrid also, in this demo website.
You can also check the Home page.
If you have any more problems, you can ask it here. I did use some of those examples in one of my client's (confidential) website, so it will be easy to manipulate according to your needs.
Hope it helps.
My edit dialog had too many fields and so became too high, so I had to put the fields side by side in 2 columns. I did it as follows:
I tried various ways, using wrap(), etc, but found that the values are not posted to the server if you modify the original table structure. So I just cloned the tr elements, put them in new tables, and hid the old ones. I did not hide the whole table, so that the validation will still be visible. I put an onchange on the cloned elements to update the old ones. This works great. Parameter tableName is your jqgrid element id.
var splitFormatted = false;
function SplitFormatForm(tableName, add) {
if (!splitFormatted) {
splitFormatted = true;
$("#FrmGrid_" + tableName).append('<table><tr><td><table id="TblGrid_' + tableName + '_A" class="EditTable" border="0" cellSpacing="0" cellPadding="0" /></td><td><table id="TblGrid_' + tableName + '_B" class="EditTable" border="0" cellSpacing="0" cellPadding="0" /></td></tr></table>');
var cc = $("#TblGrid_" + tableName + "> tbody").children("tr").length;
var s = (cc / 2) - 1;
var x = $("#TblGrid_" + tableName + "> tbody").children("tr");
var i = 0;
x.each(function (index) {
var e = $(this).clone();
var oldID = e.attr("id") + "";
var newID = oldID;
if (oldID.substring(0, 3) === "tr_") {
newID = "clone_" + oldID;
$(this).css("display", "none");
e.change(function () { $("#" + oldID + " > .DataTD > .FormElement").val($("#" + newID + " > .DataTD > .FormElement").val()); });
e.attr("id", newID);
if (i++ < s) {
$("#TblGrid_" + tableName + "_A").append(e);
}
else {
$("#TblGrid_" + tableName + "_B").append(e);
}
}
});
//This hack makes the popup work the first time too
$(".ui-icon-closethick").trigger('click');
var sel_id = "'new'";
if (!add) {
sel_id = jQuery('#'+tableName).jqGrid('getGridParam', 'selrow');
}
jQuery('#'+tableName).jqGrid('editGridRow', sel_id, { closeAfterAdd: true, width: 800, afterSubmit: function (response, postdata) { return [response.responseText == 'OK', response.responseText]; } });
}}
Call this code in your editOptions as follows:
afterShowForm: function () { SplitFormatForm("SiteAccountsGrid", false); }