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(['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';
Line 100: Line 101:
var pendingImg='';
var pendingImg='';
var api=new mw.Api();
var api=new mw.Api();
var AND=function(a,b){return a?b:false;};


function init(n){return n.split(/[\s_]+/).map(function(w){return w[0]||'';}).join('').substring(0,2).toUpperCase()||'?';}
function init(n){return n.split(/[\s_]+/).map(function(w){return w[0]||'';}).join('').substring(0,2).toUpperCase()||'?';}
Line 108: Line 108:
function setAv(){
function setAv(){
var el=document.getElementById('composer-avatar');
var el=document.getElementById('composer-avatar');
if(!el)return;
el.innerHTML=photo?'<img src="'+photo+'">':init(user);
el.innerHTML=photo?'<img src="'+photo+'">':init(user);
el.innerHTML+='<div class="sf-avatar-edit">Edit</div>';
el.innerHTML+='<div class="sf-avatar-edit">Edit</div>';
}
}
setAv();
setAv();
document.getElementById('post-content').placeholder='What is on your mind, '+user+'?';
var postContent=document.getElementById('post-content');
if(postContent)postContent.placeholder='What is on your mind, '+user+'?';


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


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


document.getElementById('post-btn').onclick=function(){
$('#post-btn').on('click',function(){
var c=document.getElementById('post-content').value.trim();
var c=$('#post-content').val().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.disabled=true;btn.textContent='Posting...';
var btn=$(this);btn.prop('disabled',true).text('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(){
document.getElementById('post-content').value='';
$('#post-content').val('');
pendingImg='';document.getElementById('image-preview').style.display='none';
pendingImg='';$('#image-preview').hide();
btn.disabled=false;btn.textContent='Post';
btn.prop('disabled',false).text('Post');
load();
load();
}).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';});
}).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');});
};
});


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(){
document.getElementById('timeline-container').innerHTML='<div class="sf-empty"><div class="sf-empty-icon">😕</div><div class="sf-empty-text">Error loading</div></div>';
$('#timeline-container').html('<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=document.getElementById('timeline-container');
var c=$('#timeline-container');
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;}
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;}
var h='';
var h='';
posts.forEach(function(p){
posts.forEach(function(p){
Line 171: 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.innerHTML=h;
c.html(h);
c.querySelectorAll('[data-del]').forEach(function(b){b.onclick=function(){if(confirm('Delete?'))api.postWithToken('csrf',{action:'socialfeed',sfaction:'deletepost',post_id:b.dataset.del}).done(load);};});
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-like]').forEach(function(b){b.onclick=function(){api.postWithToken('csrf',{action:'socialfeed',sfaction:'react',post_id:b.dataset.like,reaction_type:'like'}).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-cmt]').forEach(function(b){b.onclick=function(){var box=document.getElementById('cmt-'+b.dataset.cmt);if(box.style.display==='none'){box.style.display='block';loadCmt(b.dataset.cmt);}else{box.style.display='none';}};});
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();}});
}
}


function loadCmt(id){
function loadCmt(id){
var box=document.getElementById('cmt-'+id);
var box=$('#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 185: 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.innerHTML=h;
box.html(h);
document.getElementById('cinput-'+id).onkeypress=function(e){if(e.key==='Enter'){if(this.value.trim()){var v=this.value.trim();this.value='';api.postWithToken('csrf',{action:'socialfeed',sfaction:'comment',post_id:id,content:v}).done(function(){loadCmt(id);load();});}}};
$('#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();});}}});
});
});
}
}


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

Revision as of 02:05, 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