User:Docmoates/Social: Difference between revisions

From XMethod Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 95: Line 95:


<script>
<script>
$(function(){
mw.loader.using(['jquery','mediawiki.api']).then(function(){
mw.loader.using(['mediawiki.api']).then(function(){
var user=mw.config.get('wgUserName')||'Guest';
var user=mw.config.get('wgUserName')||'Guest';
var photo=localStorage.getItem('sf_photo_'+user)||'';
var photo=localStorage.getItem('sf_photo_'+user)||'';
Line 105: Line 104:
function esc(t){var d=document.createElement('div');d.textContent=t;return d.innerHTML;}
function esc(t){var d=document.createElement('div');d.textContent=t;return d.innerHTML;}
function ago(d){var s=Math.floor((Date.now()-d)/1000);if(s<60)return'Just now';var m=Math.floor(s/60);if(m<60)return m+'m';var h=Math.floor(m/60);if(h<24)return h+'h';return Math.floor(h/24)+'d';}
function ago(d){var s=Math.floor((Date.now()-d)/1000);if(s<60)return'Just now';var m=Math.floor(s/60);if(m<60)return m+'m';var h=Math.floor(m/60);if(h<24)return h+'h';return Math.floor(h/24)+'d';}
function gel(id){return document.getElementById(id);}


function setAv(){
function setAv(){
var el=document.getElementById('composer-avatar');
var el=gel('composer-avatar');
if(!el)return;
if(!el)return;
el.innerHTML=photo?'<img src="'+photo+'">':init(user);
el.innerHTML=photo?'<img src="'+photo+'">':init(user);
Line 113: Line 113:
}
}
setAv();
setAv();
var postContent=document.getElementById('post-content');
var postContent=gel('post-content');
if(postContent)postContent.placeholder='What is on your mind, '+user+'?';
if(postContent)postContent.placeholder='What is on your mind, '+user+'?';


$('#composer-avatar').on('click',function(){
gel('composer-avatar').addEventListener('click',function(){
$('#profile-url-input').val(photo);
gel('profile-url-input').value=photo;
$('#profile-modal').addClass('active');
gel('profile-modal').classList.add('active');
});
});
$('#profile-cancel-btn').on('click',function(){$('#profile-modal').removeClass('active');});
gel('profile-cancel-btn').addEventListener('click',function(){gel('profile-modal').classList.remove('active');});
$('#profile-save-btn').on('click',function(){
gel('profile-save-btn').addEventListener('click',function(){
photo=$('#profile-url-input').val().trim();
photo=gel('profile-url-input').value.trim();
localStorage.setItem('sf_photo_'+user,photo);
localStorage.setItem('sf_photo_'+user,photo);
setAv();
setAv();
$('#profile-modal').removeClass('active');
gel('profile-modal').classList.remove('active');
});
});


$('#add-image-btn').on('click',function(){
gel('add-image-btn').addEventListener('click',function(){
var u=prompt('Enter image URL:');
var u=prompt('Enter image URL:');
if(u){pendingImg=u;$('#preview-img').attr('src',u);$('#image-preview').show();}
if(u){pendingImg=u;gel('preview-img').src=u;gel('image-preview').style.display='block';}
});
});
$('#remove-image-btn').on('click',function(){pendingImg='';$('#image-preview').hide();});
gel('remove-image-btn').addEventListener('click',function(){pendingImg='';gel('image-preview').style.display='none';});


$('#post-btn').on('click',function(){
gel('post-btn').addEventListener('click',function(){
var c=$('#post-content').val().trim();
var c=gel('post-content').value.trim();
if(!(c||pendingImg)){alert('Write something or add image');return;}
if(!(c||pendingImg)){alert('Write something or add image');return;}
var btn=$(this);btn.prop('disabled',true).text('Posting...');
var btn=this;btn.disabled=true;btn.textContent='Posting...';
var p={action:'socialfeed',sfaction:'createpost',content:c||'(photo)'};
var p={action:'socialfeed',sfaction:'createpost',content:c||'(photo)'};
if(pendingImg)p.image_url=pendingImg;
if(pendingImg)p.image_url=pendingImg;
api.postWithToken('csrf',p).done(function(){
api.postWithToken('csrf',p).done(function(){
$('#post-content').val('');
gel('post-content').value='';
pendingImg='';$('#image-preview').hide();
pendingImg='';gel('image-preview').style.display='none';
btn.prop('disabled',false).text('Post');
btn.disabled=false;btn.textContent='Post';
load();
load();
}).fail(function(code,data){var msg=code;if(data){if(data.error){msg=data.error.info;}}alert('Error: '+msg);btn.prop('disabled',false).text('Post');});
}).fail(function(code,data){var msg=code;if(data){if(data.error){msg=data.error.info;}}alert('Error: '+msg);btn.disabled=false;btn.textContent='Post';});
});
});


function load(){
function load(){
api.get({action:'socialfeed',sfaction:'getposts',limit:20}).done(function(d){render(d.socialfeed.posts||[]);}).fail(function(){
api.get({action:'socialfeed',sfaction:'getposts',limit:20}).done(function(d){render(d.socialfeed.posts||[]);}).fail(function(){
$('#timeline-container').html('<div class="sf-empty"><div class="sf-empty-icon">😕</div><div class="sf-empty-text">Error loading</div></div>');
gel('timeline-container').innerHTML='<div class="sf-empty"><div class="sf-empty-icon">😕</div><div class="sf-empty-text">Error loading</div></div>';
});
});
}
}


function render(posts){
function render(posts){
var c=$('#timeline-container');
var c=gel('timeline-container');
if(!posts.length){c.html('<div class="sf-empty"><div class="sf-empty-icon">📝</div><div class="sf-empty-text">No posts yet</div><div class="sf-empty-sub">Be the first to share!</div></div>');return;}
if(!posts.length){c.innerHTML='<div class="sf-empty"><div class="sf-empty-icon">📝</div><div class="sf-empty-text">No posts yet</div><div class="sf-empty-sub">Be the first to share!</div></div>';return;}
var h='';
var h='';
posts.forEach(function(p){
posts.forEach(function(p){
Line 173: Line 173:
h+='<div class="sf-comments" id="cmt-'+p.id+'" style="display:none"></div></div>';
h+='<div class="sf-comments" id="cmt-'+p.id+'" style="display:none"></div></div>';
});
});
c.html(h);
c.innerHTML=h;
c.find('[data-del]').on('click',function(){var id=$(this).data('del');if(confirm('Delete?'))api.postWithToken('csrf',{action:'socialfeed',sfaction:'deletepost',post_id:id}).done(load);});
c.querySelectorAll('[data-del]').forEach(function(b){b.addEventListener('click',function(){if(confirm('Delete?'))api.postWithToken('csrf',{action:'socialfeed',sfaction:'deletepost',post_id:b.dataset.del}).done(load);});});
c.find('[data-like]').on('click',function(){var id=$(this).data('like');api.postWithToken('csrf',{action:'socialfeed',sfaction:'react',post_id:id,reaction_type:'like'}).done(load);});
c.querySelectorAll('[data-like]').forEach(function(b){b.addEventListener('click',function(){api.postWithToken('csrf',{action:'socialfeed',sfaction:'react',post_id:b.dataset.like,reaction_type:'like'}).done(load);});});
c.find('[data-cmt]').on('click',function(){var id=$(this).data('cmt');var box=$('#cmt-'+id);if(box.css('display')==='none'){box.show();loadCmt(id);}else{box.hide();}});
c.querySelectorAll('[data-cmt]').forEach(function(b){b.addEventListener('click',function(){var box=gel('cmt-'+b.dataset.cmt);if(box.style.display==='none'){box.style.display='block';loadCmt(b.dataset.cmt);}else{box.style.display='none';}});});
}
}


function loadCmt(id){
function loadCmt(id){
var box=$('#cmt-'+id);
var box=gel('cmt-'+id);
api.get({action:'socialfeed',sfaction:'getcomments',post_id:id}).done(function(d){
api.get({action:'socialfeed',sfaction:'getcomments',post_id:id}).done(function(d){
var cmts=d.socialfeed.comments||[];
var cmts=d.socialfeed.comments||[];
Line 187: Line 187:
var myPic=photo?'<img src="'+photo+'">':init(user);
var myPic=photo?'<img src="'+photo+'">':init(user);
h+='<div class="sf-add-comment"><div class="sf-avatar sf-avatar-small">'+myPic+'</div><input class="sf-comment-input" id="cinput-'+id+'" placeholder="Write a comment..."></div>';
h+='<div class="sf-add-comment"><div class="sf-avatar sf-avatar-small">'+myPic+'</div><input class="sf-comment-input" id="cinput-'+id+'" placeholder="Write a comment..."></div>';
box.html(h);
box.innerHTML=h;
$('#cinput-'+id).on('keypress',function(e){if(e.key==='Enter'){var v=$(this).val().trim();if(v){$(this).val('');api.postWithToken('csrf',{action:'socialfeed',sfaction:'comment',post_id:id,content:v}).done(function(){loadCmt(id);load();});}}});
gel('cinput-'+id).addEventListener('keypress',function(e){if(e.key==='Enter'){var v=this.value.trim();if(v){this.value='';api.postWithToken('csrf',{action:'socialfeed',sfaction:'comment',post_id:id,content:v}).done(function(){loadCmt(id);load();});}}});
});
});
}
}


load();
load();
});
});
});
</script>
</script>
</div>
</div>
</html>
</html>

Revision as of 02:47, 3 February 2026

Add Story
?
Edit
📝
No posts yet
Be the first to share something!
Set Profile Photo

Upload to wiki first, then paste URL