Tags AJAX integration on OPAC details page, so it behaves like results.
[koha.git] / koha-tmpl / opac-tmpl / prog / en / js / tags.js
1 if (typeof KOHA == "undefined" || !KOHA) {
2     var KOHA = {};
3 }
4
5 /**
6 * A namespace for Tags related functions.
7 * readCookie is expected to already be declared.  That's why the assignment below is unscoped.
8 * readCookie should be from basket.js or undefined.
9
10 $.ajaxSetup({
11         url: "/cgi-bin/koha/opac-tags.pl",
12         type: "POST",
13         dataType: "script"
14 });
15 */
16 if (typeof(readCookie) == "undefined") {
17         readCookie = function (name) { // from http://www.quirksmode.org/js/cookies.html
18                 var nameEQ = name + "=";
19                 var ca = document.cookie.split(';');
20                 for(var i=0;i < ca.length;i++) {
21                         var c = ca[i];
22                         while (c.charAt(0)==' '){ c = c.substring(1,c.length); }
23                         if (c.indexOf(nameEQ) == 0){ return c.substring(nameEQ.length,c.length); }
24                 }
25                 return null;
26         }
27 }
28 KOHA.Tags = {
29         add_tag_button: function(){
30                 var mybibnum = $(this).attr("title");
31                 var mynewtag = "newtag" + mybibnum;
32                 var mytagid = "#" + mynewtag;
33                 var mydata = {CGISESSID: readCookie('CGISESSID')};      // Someday this should be OPACSESSID
34                 mydata[mynewtag] = $(mytagid).val();    // need [bracket] for variable property id
35                 var response;   // AJAX from server will assign value to response.
36                 $.post(
37                         "/cgi-bin/koha/opac-tags.pl",
38                         mydata,
39                         function(data){
40                                 // alert("AJAX Response: " + data);
41                                 eval(data);
42                                 // alert("counts: " + response["added"] + response["deleted"] + response["errors"]);
43                                 KOHA.Tags.set_tag_status(
44                                         mytagid + "_status",
45                                         KOHA.Tags.common_status(response["added"], response["deleted"], response["errors"])
46                                 );
47                                 if (response.alerts) {
48                                         alert(response.alerts.join("\n\n"));
49                                 }
50                         },
51                         'script'
52                 );
53                 return false;
54         },
55         common_status : function(addcount, delcount, errcount) {
56             var cstat = "";
57             if (addcount && addcount > 0) {cstat += "Added "   + addcount + (addcount==1 ? " tag" : " tags") + ".  " ;}
58             if (delcount && delcount > 0) {cstat += "Deleted " + delcount + (delcount==1 ? " tag" : " tags") + ".  " ;}
59             if (errcount && errcount > 0) {cstat += (errcount==1 ? "ERROR" : errcount + " ERRORS") + " during operation.";}
60             return cstat;
61         },
62         set_tag_status : function(tagid, newstatus) {
63                 $(tagid).html(newstatus);
64                 $(tagid).css({display:"inline"});
65         },
66
67         tag_message: {
68         tagsdisabled : function(arg) {return ("Sorry, tags are not enabled on this system.");},
69         scrubbed_all_bad : function(arg) {return ("Error! Your tag was entirely markup code.  It was NOT added.  Please try again with plain text.");},
70         badparam : function(arg) {return ("Error! Illegal parameter '" +arg+ "'.");},
71         scrubbed : function(arg) {return ("Note: your tag contained markup code that was removed. The tag was added as '" +arg+ "'.");},
72     failed_add_tag : function(arg) {return ("Error! The add_tag operation failed on '" +arg+ "'.  Note: you can only tag an item with a given term once.  Check 'My Tags' to see your current tags.");},
73     failed_delete  : function(arg) {return ("Error! You cannot delete the tag '" +arg+ "'.  Note: you can only delete your own tags.");},
74         },
75 };
76