-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfunctions.min.js
10 lines (9 loc) · 6.5 KB
/
functions.min.js
1
2
3
4
5
6
7
8
9
10
/**
* Ajax functions for moodleoverflow
*
* @module mod_moodleoverflow/functions
* @copyright 2017 Tamara Gunkel
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("mod_moodleoverflow/functions",["jquery","core/ajax","core/templates","core/notification","core/config","core/url","core/str"],(function($,ajax,templates,notification,Cfg,Url,str){var t={recordvote:function(discussionid,ratingid,userid,event){var postid=$(event.target).closest(".moodleoverflowpost").attr("id");postid=postid.substring(1);var vote=ajax.call([{methodname:"mod_moodleoverflow_record_vote",args:{discussionid:discussionid,postid:postid,ratingid:ratingid,sesskey:Cfg.sesskey}}]);return vote[0].done((function(response){var parentdiv=$(event.target).parent().parent();2===ratingid?(parentdiv.children("a:first-of-type").children().attr("src",Url.imageUrl("vote/upvoted","moodleoverflow")),parentdiv.children("a:nth-of-type(2)").children().attr("src",Url.imageUrl("vote/downvote","moodleoverflow"))):1===ratingid?(parentdiv.children("a:first-of-type").children().attr("src",Url.imageUrl("vote/upvote","moodleoverflow")),parentdiv.children("a:nth-of-type(2)").children().attr("src",Url.imageUrl("vote/downvoted","moodleoverflow"))):(parentdiv.children("a:first-of-type").children().attr("src",Url.imageUrl("vote/upvote","moodleoverflow")),parentdiv.children("a:nth-of-type(2)").children().attr("src",Url.imageUrl("vote/downvote","moodleoverflow"))),parentdiv.children("p").text(response.postrating),templates.replaceNode($(".user-details,.author").find('a[href*="id='+userid+'"]').siblings("span"),"<span>"+response.raterreputation+"</span>",""),response.ownerid&&userid!==response.ownerid&&templates.replaceNode($(".user-details,.author").find('a[href*="id='+response.ownerid+'"]').siblings("span"),"<span>"+response.ownerreputation+"</span>","")})).fail(notification.exception),vote},clickevent:function(discussionid,userid,allowmultiplemarks){$(".upvote").on("click",(function(event){$(event.target).is("a")&&(event.target=$(event.target).children()),$(event.target).parent().attr("class").indexOf("active")>=0?t.recordvote(discussionid,20,userid,event):t.recordvote(discussionid,2,userid,event),$(event.target).parent().toggleClass("active"),$(event.target).parent().nextAll("a").removeClass("active")})),$(".downvote").on("click",(function(event){$(event.target).is("a")&&(event.target=$(event.target).children()),$(event.target).parent().attr("class").indexOf("active")>=0?t.recordvote(discussionid,10,userid,event):t.recordvote(discussionid,1,userid,event),$(event.target).parent().toggleClass("active"),$(event.target).parent().prevAll("a").removeClass("active")})),$(".marksolved").on("click",(function(event){var post=$(event.target).parents(".moodleoverflowpost");post.hasClass("statusteacher")||post.hasClass("statusboth")?t.recordvote(discussionid,30,userid,event)[0].then((function(){t.removeSolvedFromPost(post)})):t.recordvote(discussionid,3,userid,event)[0].then((function(){if(0==discussionid){t.removeOtherSolved(post.parent().parent()),post.hasClass("statusstarter")?(post.removeClass("statusstarter"),post.addClass("statusboth")):post.addClass("statusteacher");var promiseStringNotSolved=str.get_string("marknotsolved","mod_moodleoverflow");$.when(promiseStringNotSolved).done((function(string){$(event.target).text(string)})),t.redoStatus(post)}}))})),$(".markhelpful").on("click",(function(event){var post=$(event.target).parents(".moodleoverflowpost");post.hasClass("statusstarter")||post.hasClass("statusboth")?t.recordvote(discussionid,40,userid,event)[0].then((function(){t.removeHelpfulFromPost(post)})):t.recordvote(discussionid,4,userid,event)[0].then((function(){if(0==discussionid){t.removeOtherHelpful(post.parent().parent()),post.hasClass("statusteacher")?(post.removeClass("statusteacher"),post.addClass("statusboth")):post.addClass("statusstarter");var promiseStringNotHelpful=str.get_string("marknothelpful","mod_moodleoverflow");$.when(promiseStringNotHelpful).done((function(string){$(event.target).text(string)})),t.redoStatus(post)}}))}))},removeHelpfulFromPost:function(post){post.hasClass("statusstarter")?post.removeClass("statusstarter"):(post.removeClass("statusboth"),post.addClass("statusteacher")),t.redoStatus(post);var promiseHelpful=str.get_string("markhelpful","mod_moodleoverflow");$.when(promiseHelpful).done((function(string){post.find(".markhelpful").text(string)}))},removeOtherHelpful:function(root){var formerhelpful=root.find(".statusstarter, .statusboth");formerhelpful.length>0&&t.removeHelpfulFromPost(formerhelpful)},removeSolvedFromPost:function(post){post.hasClass("statusteacher")?post.removeClass("statusteacher"):(post.removeClass("statusboth"),post.addClass("statusstarter")),t.redoStatus(post);var promiseHelpful=str.get_string("marksolved","mod_moodleoverflow");$.when(promiseHelpful).done((function(string){post.find(".marksolved").text(string)}))},removeOtherSolved:function(root){var formersolution=root.find(".statusteacher, .statusboth");formersolution.length>0&&t.removeSolvedFromPost(formersolution)},redoStatus:function(post){if($(post).hasClass("statusboth")){str.get_strings([{key:"teacherrating",component:"mod_moodleoverflow"},{key:"starterrating",component:"mod_moodleoverflow"},{key:"bestanswer",component:"mod_moodleoverflow"}]).then((function(results){var circle=templates.renderPix("status/c_circle","mod_moodleoverflow",results[0]),box=templates.renderPix("status/b_box","mod_moodleoverflow",results[1]);return $.when(box,circle).done((function(boxImg,circleImg){screen.width>600?post.find(".status").html(boxImg+circleImg+results[2]):post.find(".status").html(boxImg+circleImg)})),results}))}else if($(post).hasClass("statusteacher")){str.get_strings([{key:"teacherrating",component:"mod_moodleoverflow"},{key:"solvedanswer",component:"mod_moodleoverflow"}]).then((function(results){var circle=templates.renderPix("status/c_outline","mod_moodleoverflow",results[0]);return $.when(circle).done((function(circleImg){screen.width>600?post.find(".status").html(circleImg+results[1]):post.find(".status").html(circleImg)})),results}))}else if($(post).hasClass("statusstarter")){str.get_strings([{key:"starterrating",component:"mod_moodleoverflow"},{key:"helpfulanswer",component:"mod_moodleoverflow"}]).then((function(results){var box=templates.renderPix("status/b_outline","mod_moodleoverflow",results[0]);return $.when(box).done((function(boxImg){screen.width>600?post.find(".status").html(boxImg+results[1]):post.find(".status").html(boxImg)})),results}))}else post.find(".status").html("")}};return t}));
//# sourceMappingURL=functions.min.js.map