]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/XULTermLoader.js
webstaff: get Load Catalog Record IDs working
[Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / XULTermLoader.js
1 if (!dojo._hasResource["openils.widget.XULTermLoader"]) {
2     dojo._hasResource["openils.widget.XULTermLoader"] = true;
3
4     dojo.provide("openils.widget.XULTermLoader");
5     dojo.require("openils.XUL");
6     dojo.requireLocalization("openils.widget", "XULTermLoader");
7
8     dojo.declare(
9         "openils.widget.XULTermLoader", [dijit.layout.ContentPane], {
10             "constructor": function(args) {
11                 this.args = args;
12                 this.terms = [];
13                 this._ = openils.widget.XULTermLoader.localeStrings;
14
15                 /* XXX Totally arbitrary defaults. Keeping them low for now
16                  * since all search terms have to be turned into a URL.
17                  * There will need to be a better long term solution.
18                  */
19                 if (!this.args.fileSizeLimit)
20                     this.args.fileSizeLimit = 2048;
21                 if (!this.args.termLimit)
22                     this.args.termLimit = 100;
23             },
24             "build": function(callback) {
25                 var self = this;
26
27                 this.domNode = dojo.create("span");
28                 this.labelNode = dojo.create(
29                     "span", {
30                         "innerHTML": this._.LABEL_TEXT,
31                         "style": "padding-right: 8px;"
32                     }, this.domNode, "last"
33                 );
34                 this.countNode = dojo.create(
35                     "span", {"innerHTML": this.terms.length},
36                     this.labelNode, "first"
37                 );
38                 if (window.parent.IEMBEDXUL) {
39                     this.buttonNode = dojo.create(
40                         "input", {
41                             "type" : "file",
42                             "innerHTML": this._.BUTTON_TEXT,
43                             "onchange": function(evt) { self.loadTerms(evt); }
44                         },
45                         this.domNode, "last"
46                     );
47                 } else {
48                     this.buttonNode = dojo.create(
49                         "button", {
50                             "innerHTML": this._.BUTTON_TEXT,
51                             "onclick": function() { self.loadTerms(); }
52                         },
53                         this.domNode, "last"
54                     );
55                 }
56
57                 if (this.args.parentNode)
58                     dojo.place(this.domNode, this.args.parentNode, "last");
59
60                 callback(this);
61             },
62             "updateCount": function() {
63                 var value = this.attr("value");
64                 if (dojo.isArray(value))
65                     this.terms = this.attr("value");
66                 this.countNode.innerHTML = this.terms.length;
67             },
68             "focus": function() {
69                 this.buttonNode.focus();
70             },
71             "loadTerms": function(evt) {
72                 try {
73                     if (this.terms.length >= this.args.termLimit) {
74                         alert(this._.TERM_LIMIT);
75                         return;
76                     }
77                     var data;
78                     var self = this;
79
80                     function updateTermList() {
81                         if (data.length + self.terms.length >=
82                             self.args.termLimit) {
83                             alert(self._.TERM_LIMIT_SOME);
84                             var can = self.args.termLimit - self.terms.length;
85                             if (can > 0)
86                                 self.terms = self.terms.concat(data.slice(0, can));
87                         } else {
88                             self.terms = self.terms.concat(data);
89                         }
90                         self.attr("value", self.terms);
91                         self.updateCount();
92                     }
93
94                     if (evt && window.IAMBROWSER) {
95                         var reader = new FileReader();
96                         reader.onloadend = function(evt) {
97                             data = self[
98                                 this.parseCSV ? "parseAsCSV" : "parseUnimaginatively"
99                             ](evt.target.result);
100                             updateTermList();
101                         };
102                         reader.readAsText(evt.target.files[0]);
103                     } else {
104                         data = this[
105                             this.parseCSV ? "parseAsCSV" : "parseUnimaginatively"
106                         ](
107                             openils.XUL.contentFromFileOpenDialog(
108                                 this._.CHOOSE_FILE, this.args.fileSizeLimit
109                             )
110                         );
111                         updateTermList();
112                     }
113
114                 } catch(E) {
115                     alert(E);
116                 }
117             },
118             "parseAsCSV": function(data) {
119                 return this.parseUnimaginatively(data).
120                     map(
121                         function(o) {
122                             return o.match(/^".+"$/) ? o.slice(1,-1) : o;
123                         }
124                     ).
125                     filter(
126                         function(o) { return Boolean(o.match(/^\d+$/)); }
127                     );
128             },
129             "parseUnimaginatively": function(data) {
130                 if (!data) return [];
131                 else return data.split("\n").
132                     filter(function(o) { return o.length > 0; }).
133                     map(function(o) {return o.replace("\r","").split(",")[0];}).
134                     filter(function(o) { return o.length > 0; });
135             }
136         }
137     );
138
139     openils.widget.XULTermLoader.localeStrings =
140         dojo.i18n.getLocalization("openils.widget", "XULTermLoader");
141 }