Calculating width after css height resize - php

I'm building a page of horizontal images pulled from XML
When the images load they take on the screen height minus a small value to give some room.
I'm grabbing the widths of the images so I can add them up and set the width of the div container.
At the moment the total width is the size of the full unresized images leaving a blank space at the end of the last image. How can I grab the width of all the resized images?
Here is my code
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: "xml.xml",
dataType: "xml",
success: xmlParser
});
});
function xmlParser(xml) {
var $heightb = $(window).height() - 45;
var $widthb = 0;
$(xml).find("image").each(function () {
$url = $(this).attr("url");
$(".main").append('<div class="image" style="height='+$heightb+'px"><div class="title"><img height="'+$heightb+'px" src="' + $url + '" title="' + $(this).attr("desc") + '" /></div><div class="description">' + $(this).attr("desc") + '</div></div>');
$(".image").fadeIn(1000);
$widthb += $(".image").width();
});
$(".main").css('width', $widthb);
}
The page I'm working on is http://www.spitznagel.ch/mobile.php

You can probably just create the element for each image and get its size before appending it. Something like this (untested code; hopefully you get the idea)
function xmlParser(xml) {
var $heightb = $(window).height() - 45;
var $widthb = 0;
var imgDiv;
var width;
$(xml).find("image").each(function () {
$url = $(this).attr("url");
imgDiv = $('<div class="image" style="height='+$heightb+'px"><div class="title"><img height="'+$heightb+'px" src="' + $url + '" title="' + $(this).attr("desc") + '" /></div><div class="description">' + $(this).attr("desc") + '</div></div>');
$(".main").append(imgDiv);
width = imgDiv.width();
imgDiv.fadeIn(1000);
$widthb += width;
});
$(".main").css('width', $widthb);
}

Related

Load multiple data from database with ajax and slickAdd them to existing Slick on success, but only once

so I'm using Slick Carousel and I'm showing in it a list of products from my database limited to 24 for performance reasons. But I need to show all of them, so I made a function that ajax loads another 24 products everytime the user is 2 slides before the end and add them with slickAdd function to the existing Slick. Everything is working great but it takes a few seconds till it gets added because the slickAdd function runs for each new product. So I wanted to ask if there is a way to select all the 24 new products, and add them only once with the slickAdd function.
This is my current code
$('.demo').on('beforeChange', function(event, slick, currentSlide, nextSlide){
var komponent = $(".komponent-container.active").attr("id");
var slideCount = slick.slideCount-6;
console.log(slick.slideCount);
if(nextSlide == slideCount){
console.log("loadmore");
$.ajax({
type: "POST",
url: "/project/public/konfigurator",
data: {id: komponent, from_column: slick.slideCount, requestid: "load_more"},
dataType: "json",
success: function (data) {
var data_parser = JSON.parse(data)[0];
if (data_parser.length > 0) {
for (i = 0; i < data_parser.length; i++) {
var produkt_nazov = 0;
if (data_parser[i].produkt.length > 45) {
produkt_nazov = data_parser[i].produkt.substring(0, 45) + "...";
} else {
produkt_nazov = data_parser[i].produkt;
}
$('.demo').slick('slickAdd', '<div><div><div class="item-container"><div class="container-wrapper"><div class="produkt-container"><div class="item-left"><div class="item-image-wrapper"><img draggable="false" id="produkt-img" src="img/konfigurator/'+komponent+'/' + data_parser[i].produkt + '/1.jpg" alt="" /></div><div class="cena">' + data_parser[i].cena + ' €</div></div><div class="item-right"><div class="item-info"><span class="item-title">' + produkt_nazov + '</span><span class="item-description"><span>Výrobca čipu - ' + data_parser[i].vyrobca_cipu + '</span><span>Veľkosť pamäte - ' + data_parser[i].vram_size + '</span><span>Typ pamäte - ' + data_parser[i].vram_type + '</span><span>Frekvencia jadra - ' + data_parser[i].gpu_memory_clockrate + '</span></span></div><div class="spodna-cast"><div class="action-buttons"><a class="detail-button">Detail</a><a class="add-button">Vybrať</a></div></div></div></div></div></div></div></div>');
console.log("add");
}
}
},
error: function (result) {
alert('error');
}
});
}
});
And sorry if there is any gramatical mistakes, english is not my first language.
I tried to move the SlickAdd function outside the for(), but it added only 1 product to the Slick.
I figured it out, leaving it here for others.
var html = '';
for (i = 0; i < data_parser.length; i++) {
var produkt_nazov = 0;
if (data_parser[i].produkt.length > 45) {
produkt_nazov = data_parser[i].produkt.substring(0, 45) + "...";
} else {
produkt_nazov = data_parser[i].produkt;
}
html += '<div><div><div class="item-container"><div class="container-wrapper"><div class="produkt-container"><div class="item-left"><div class="item-image-wrapper"><img draggable="false" id="produkt-img" src="img/konfigurator/' + komponent + '/' + data_parser[i].produkt + '/1.jpg" alt="" /></div><div class="cena">' + data_parser[i].cena + ' €</div></div><div class="item-right"><div class="item-info"><span class="item-title">' + produkt_nazov + '</span><span class="item-description"><span>Výrobca čipu - ' + data_parser[i].vyrobca_cipu + '</span><span>Veľkosť pamäte - ' + data_parser[i].vram_size + '</span><span>Typ pamäte - ' + data_parser[i].vram_type + '</span><span>Frekvencia jadra - ' + data_parser[i].gpu_memory_clockrate + '</span></span></div><div class="spodna-cast"><div class="action-buttons"><a class="detail-button">Detail</a><a class="add-button">Vybrať</a></div></div></div></div></div></div></div></div>';
}
$(".demo").slick('slickAdd', html);
console.log("add");

How to do "If div class is 0 then start from 1 to append"

I'm trying to make multiple upload images via ajax with XMLHttpRequest. Everything is okay. Images post successfully and data returns as expected. So returned images are appending to related div class. But every time i upload images one by one, div class start from zero.
With this code,before to send file(s) i'm creating progressbar div, after each image loaded successfully, image replace to loder.
for (var i = 0; i < input.files.length; i++) {
var fileId = i;
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
With this function, no matter how many files, im uploading them:
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId = i
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
function uploadSingleFile(file, i) {
var fileId = i;
var ajax = new XMLHttpRequest();
//Progress Listener
ajax.upload.onprogress = function (e) {
var percent = (e.loaded / e.total) * 100;
$('#progressbar_' + fileId).animate({"width": percent + "%"},800);
};
//Load Listener
ajax.onreadystatechange = function (e) {
if (this.readyState == 4 && this.status == 200) {
var data = ajax.responseText;
var rep = JSON.parse(data);
$('#progressbar_' + fileId).css("width", "100%");
setTimeout(function(){
$('#progressbar_' + fileId).remove();
$('.uploaded_photo_grid.'+ fileId).html('');
$.each(file, function(){
if(rep.error !== ''){
$(".uploaded_photo_grid."+ fileId ).html(rep.name + ' yüklenemedi');
} else {
$(".uploaded_photo_grid."+ fileId ).html(
'<img class="previewItem" src="'+rep.fcontent+'" id="img_'+rep.id+'">');
}
});
},1500);
}
};
Rest of code...
}
Also i tried editing these lines of code. It increases the value of div for each file but just first image appears.
var zero = $('.uploaded_photo_grid.0').length;
for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
var fileId;
if(zero == 0){
uploadSingleFile(this.files[i], i);
console.log(this.files[i]);
fileId = i;
} else {
uploadSingleFile(this.files[i+1], i+1);
fileId = i+1;
console.log(this.files[i]);
}
$('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' +
'<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
'</div>');
}
What should i do? Is there a way to do this better? I mean upload images without form via XML with progressbar for each file. I don't choose to use plugin, that's not for me.
The following is the problematic line.
uploadSingleFile(this.files[i+1], i+1);
You want the value for i passed in uploadSingleFile to be the total number of images uploaded incremented by one.
One approach to do this is to sum $('.up_preview').children().length, i, and 1. 1 is added in the sum because the in the loop i starts at 0.
// var zero = $('.uploaded_photo_grid.0').length;
var numImages = $('.up_preview').children().length;
if (numImages === 0) {
//...
} else {
uploadSingleFile(this.files[i+1], numImages + i + 1);
}

How to replace the whole item inside the id div using Pusher php and javascript

I want to replace the whole item inside the id div instead of adding it.I am not familliar with javascript/ajax/jquery and i dont know which file i will edit.
This is the sample code given by pusher.com in activity-stream tutorial
$(function() {
var pusher = new Pusher('pusher key');
var channel = pusher.subscribe('site-activity');
var streamer = new PusherActivityStreamer(channel, '#replace_item');
});
html id that will replace all item inside the div
<div id="replace_item">ITEMS</div>
This is the javascript function
function PusherActivityStreamer(activityChannel, ulSelector, options) {
var self = this;
this._email = null;
options = options || {};
this.settings = $.extend({
maxItems: 10
}, options);
this._activityChannel = activityChannel;
this._activityList = $(ulSelector);
this._activityChannel.bind('activity', function(activity) {
self._handleActivity.call(self, activity, activity.type);
});
this._activityChannel.bind('page-load', function(activity) {
self._handleActivity.call(self, activity, 'page-load');
});
this._activityChannel.bind('test-event', function(activity) {
self._handleActivity.call(self, activity, 'test-event');
});
this._activityChannel.bind('scroll', function(activity) {
self._handleActivity.call(self, activity, 'scroll');
});
this._activityChannel.bind('like', function(activity) {
self._handleActivity.call(self, activity, 'like');
});
this._itemCount = 0;
};
PusherActivityStreamer.prototype._handleActivity = function(activity, eventType) {
var self = this;
++this._itemCount;
var activityItem = PusherActivityStreamer._buildListItem(activity);
activityItem.addClass(eventType);
activityItem.hide();
this._activityList.prepend(activityItem);
activityItem.slideDown('slow');
if(this._itemCount > this.settings.maxItems) {
this._activityList.find('li:last-child').fadeOut(function(){
$(this).remove();
--self._itemCount;
});
}
};
PusherActivityStreamer.prototype.sendActivity = function(activityType, activityData) {
var data = {
activity_type: activityType,
activity_data: activityData
};
if(this._email) {
data.email = this._email;
}
$.ajax({
url: 'php/trigger_activity.php', // PHP
// url: '/trigger_activity', // Node.js
data: data
})
};
PusherActivityStreamer._buildListItem = function(activity) {
var li = $('<li class="activity"></li>');
li.attr('data-activity-id', activity.id);
var item = $('<div class="stream-item-content"></div>');
li.append(item);
var imageInfo = activity.actor.image;
var image = $('<div class="image">' +
'<img src="' + imageInfo.url + '" width="' + imageInfo.width + '" height="' + imageInfo.height + '" />' +
'</div>');
item.append(image);
var content = $('<div class="content"></div>');
item.append(content);
var user = $('<div class="activity-row">' +
'<span class="user-name">' +
'<a class="screen-name" title="' + activity.actor.displayName + '">' + activity.actor.displayName + '</a>' +
//'<span class="full-name">' + activity.actor.displayName + '</span>' +
'</span>' +
'</div>');
content.append(user);
var message = $('<div class="activity-row">' +
'<div class="text">' + activity.body + '</div>' +
'</div>');
content.append(message);
var time = $('<div class="activity-row">' +
'<a href="' + activity.link + '" class="timestamp">' +
'<span title="' + activity.published + '">' + PusherActivityStreamer._timeToDescription(activity.published) + '</span>' +
'</a>' +
'<span class="activity-actions">' +
'<span class="tweet-action action-favorite">' +
'<span><i></i><b>Like</b></span>' +
'</span>' +
'</span>' +
'</div>');
content.append(time);
return li;
};
If you want to replace the entire div instead of adding things to it, I think this will work.
Change
this._activityList.prepend(activityItem);
for
this._activityList.empty();
this._activityList.prepend(activityItem);
".prepend" adds the activityItem to the top of the div as you know, but ".empty()" will clear everything previously in the div before you do this.

facebook Sharer popup window

The FB sharer popup window displays the data of the page, not the metadata I have inserted in the php link.
<a href="http://www.facebook.com/sharer/sharer.php?s=100&p%5Btitle%5D=Google&p%5Burl%5D=http%3A%2F%2Fwww.google.com&p%5Bsummary%5D=Google search engine&p%5Bimages%5D%5B0%5D=https://www.google.com/images/srpr/logo3w.png
"onClick="return fbs_click(626, 305)" target="_blank" title="Share">FACEBOOK</a>
It seems the to be caused by the javascript
<script type="text/javascript">
function fbs_click(width, height) {
var leftPosition, topPosition;
leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
topPosition = (window.screen.height / 2) - ((height / 2) + 50);
var windowFeatures = "status=no,height=" + height + ",width=" + width + ",resizable=no,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no";
u=location.href;
t=document.title;
window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer', windowFeatures);
return false;
}
</script>
How can I display the correct data in the popup window?
If you want to add custom metadata, use following code:
<a id="fb-share" style='text-decoration:none;' type="icon_link" onClick="window.open('http://www.facebook.com/sharer.php?s=100&p[title]=YOUR_TITLE&p[summary]=YOUR_DESCRIPTION&p[url]=YOUR_URL&p[images][0]=YOUR_IMAGE','sharer','toolbar=0,status=0,width=580,height=325');" href="javascript: void(0)">Share</a>
UPDATE:
I wrote a little JS code for Facebook Share.
JS Code: https://gist.github.com/Stichoza/4942775
Live Demo: http://jsfiddle.net/stichoza/EYxTJ/
Javascript:
function fbShare(url, title, descr, image, winWidth, winHeight) {
var winTop = (screen.height / 2) - (winHeight / 2);
var winLeft = (screen.width / 2) - (winWidth / 2);
window.open('http://www.facebook.com/sharer.php?s=100&p[title]=' + title + '&p[summary]=' + descr + '&p[url]=' + url + '&p[images][0]=' + image, 'sharer', 'top=' + winTop + ',left=' + winLeft + ',toolbar=0,status=0,width='+winWidth+',height='+winHeight);
}
HTML:
Share
the sharing link not working any more after resarch I found this you should update your function on this new share url
href="https://www.facebook.com/sharer/sharer.php?u=&t=" title="Share on Facebook" target="_blank" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(document.URL) + '&t=' + encodeURIComponent(document.URL)); return false;"> </a>

Instagram API: how to make in popular feed see user and likes

I have this code and I want to write before every photo a text and I don't know where to write. By the way, I want to know every photo which is uploaded likes and users.
function loadFeed() {
var param = {access_token:access_token};
cmd(param, onPhotoLoaded);
}
function cmd(param, callback) {
//popular
var cmdURL = 'https://api.instagram.com/v1/media/popular?callback=?';
$.getJSON(cmdURL, param, callback);
}
function onPhotoLoaded(data) {
if(data.meta.code == 200) {
var photos = data.data;
if(photos.length > 0) {
for (var key in photos ){
var photo = photos[key];
$('<div id=p' + photo.id + '></div>').addClass('photoWrapper').appendTo('#panel');
var str = '<img id="' + photo.id + '" src="' + photo.images.standard_resolution.url + '" width="100%">';
$('<div></div>').addClass('photo').html(str).appendTo('#p' + photo.id);
$('#' + photo.id).load(function() {
$('#p' + $(this).attr('id')).fadeTo('slow', 1.0);
});
}
}else{
alert('empty');
}
}else{
alert(data.meta.error_message);
}
}
The data you are looking for is in the photo object.
http://jsfiddle.net/AbU7a/
function loadFeed() {
cmd({access_token:'11007611.f59def8.4f942a67c9174159a1601a037490d3ea'}, onPhotoLoaded);
}
function cmd(param, callback) {
//popular
var cmdURL = 'https://api.instagram.com/v1/media/popular?callback=?';
$.getJSON(cmdURL, param, callback);
}
function onPhotoLoaded(data) {
if(data.meta.code == 200) {
var photos = data.data;
console.log(photos);
if(photos.length > 0) {
for (var key in photos ){
var photo = photos[key];
$('<div id=p' + photo.id + '></div>').addClass('photoWrapper').appendTo('#panel');
var str = '<p>likes: '+ photo.likes.count +'</p><img id="' + photo.id + '" src="' + photo.images.standard_resolution.url + '" width="50">';
$('<div></div>').addClass('photo').html(str).appendTo('#p' + photo.id);
$('#' + photo.id).load(function() {
$('#p' + $(this).attr('id')).fadeTo('slow', 1.0);
});
}
}else{
alert('empty');
}
}else{
alert(data.meta.error_message);
}
}
loadFeed();
View your console (differs depending on your browser) it will show you the data you can get from the photo object.
I started you off by adding the likes count.

Categories