fix syntax error in tags JavaScript
[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         login : function(arg) {return (_("You must be logged in to add tags."));}
75         },
76
77     // Used to tag multiple items at once.  The main difference
78     // is that status is displayed on a per item basis.
79     add_multitags_button : function(bibarray, tag){
80                 var mydata = {CGISESSID: readCookie('CGISESSID')};      // Someday this should be OPACSESSID
81         for (var i = 0; i < bibarray.length; i++) {
82             var mynewtag = "newtag" + bibarray[i];
83             mydata[mynewtag] = tag;
84         }
85                 var response;   // AJAX from server will assign value to response.
86                 $.post(
87                         "/cgi-bin/koha/opac-tags.pl",
88                         mydata,
89                         function(data){
90                                 eval(data);
91                 $(".tagstatus").empty();
92                 var bibErrors = false;
93
94                 // Display the status for each tagged bib
95                 for (var i = 0; i < bibarray.length; i++) {
96                     var bib = bibarray[i];
97                     var mytagid = "#newtag" + bib;
98                     var status = "";
99
100                     // Number of tags added.
101                     if (response[bib]) {
102                         var added = response[bib]["added"];
103                         if (added > 0) {
104                             status = "Added " + added + (added == 1 ? " tag" : " tags") + ".  ";
105                                         KOHA.Tags.set_tag_status(mytagid + "_status", status);
106                         }
107
108                         // Show a link that opens an error dialog, if necessary.
109                         var errors = response[bib]["errors"];
110                         if (errors.length > 0) {
111                             bibErrors = true;    
112                             var errid = "tagerr_" + bib;
113                             var errstat = "<a id=\"" + errid + "\" class=\"tagerror\" href=\"#\">";
114                             errstat += "Error" + (errors.length > 1 ? "s" : "") + " adding tag.";
115                             errstat += "</a>";
116                                             KOHA.Tags.append_tag_status(mytagid + "_status", errstat);
117                             var errmsg = "";
118                             for (var e = 0; e < errors.length; e++){
119                                 if (e) {
120                                     errmsg += "\n\n";
121                                 }
122                                 errmsg += errors[e];
123                             }
124                             $("#" + errid).click(function(){
125                                 alert(errmsg);
126                             });
127                         }
128                     }
129                 }
130
131                 if (bibErrors || response["global_errors"]) {
132                     var msg = "";
133                     if (bibErrors) {
134                         msg = "Unable to add one or more tags.";
135                     }
136
137                     // Show global errors in a dialog.
138                     if (response["global_errors"]) {
139                         var global_errors = response["global_errors"];
140                         var msg;
141                         for (var e = 0; e < global_errors.length; e++) {
142                             msg += "\n\n";
143                             msg += response.alerts[global_errors[e]];
144                         }
145                     }
146                     alert(msg);
147                 }
148                         },
149                         'script'
150                 );
151                 return false;
152     }
153 };
154