]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/XULTermLoader.js
db49e089bc5b08e3fdf416c39e903caec4c31e67
[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                 this.buttonNode = dojo.create(
39                     "button", {
40                         "innerHTML": this._.BUTTON_TEXT,
41                         "onclick": function() { self.loadTerms(); }
42                     },
43                     this.domNode, "last"
44                 );
45
46                 if (this.args.parentNode)
47                     dojo.place(this.domNode, this.args.parentNode, "last");
48
49                 callback(this);
50             },
51             "updateCount": function() {
52                 var value = this.attr("value");
53                 if (dojo.isArray(value))
54                     this.terms = this.attr("value");
55                 this.countNode.innerHTML = this.terms.length;
56             },
57             "focus": function() {
58                 this.buttonNode.focus();
59             },
60             "loadTerms": function() {
61                 try {
62                     if (this.terms.length >= this.args.termLimit) {
63                         alert(this._.TERM_LIMIT);
64                         return;
65                     }
66                     var data = this[
67                         this.parseCSV ? "parseAsCSV" : "parseUnimaginatively"
68                     ](
69                         openils.XUL.contentFromFileOpenDialog(
70                             this._.CHOOSE_FILE, this.args.fileSizeLimit
71                         )
72                     );
73
74                     if (data.length + this.terms.length >=
75                         this.args.termLimit) {
76                         alert(this._.TERM_LIMIT_SOME);
77                         var can = this.args.termLimit - this.terms.length;
78                         if (can > 0)
79                             this.terms = this.terms.concat(data.slice(0, can));
80                     } else {
81                         this.terms = this.terms.concat(data);
82                     }
83                     this.attr("value", this.terms);
84                     this.updateCount();
85                 } catch(E) {
86                     alert(E);
87                 }
88             },
89             "parseAsCSV": function(data) {
90                 return this.parseUnimaginatively(data).
91                     map(
92                         function(o) {
93                             return o.match(/^".+"$/) ? o.slice(1,-1) : o;
94                         }
95                     ).
96                     filter(
97                         function(o) { return Boolean(o.match(/^\d+$/)); }
98                     );
99             },
100             "parseUnimaginatively": function(data) {
101                 if (!data) return [];
102                 else return data.split("\n").
103                     filter(function(o) { return o.length > 0; }).
104                     map(function(o) {return o.replace("\r","").split(",")[0];}).
105                     filter(function(o) { return o.length > 0; });
106             }
107         }
108     );
109
110     openils.widget.XULTermLoader.localeStrings =
111         dojo.i18n.getLocalization("openils.widget", "XULTermLoader");
112 }