1 | ///////////////////////////////////////////////////////////////////// |
---|
2 | ///////////////////////////////////////////////////////////////////// |
---|
3 | var bitsSubstitutionTree = { |
---|
4 | _xmlfile : null, |
---|
5 | _xmldoc : null, |
---|
6 | |
---|
7 | get idMENU() { return "bitsSubstitutionTreeMenu"; }, |
---|
8 | get idMENU_ALL() { return "bitsSubstitutionTreeAllMenuitem"; }, |
---|
9 | get idMENU_SELECT(){ return "bitsSubstitutionTreeSelectMenuitem"; }, |
---|
10 | |
---|
11 | get idCMENU() { return "bitsSubstitutionTreeContextmenu"; }, |
---|
12 | |
---|
13 | get MENU() { return document.getElementById(this.idMENU); }, |
---|
14 | get MENU_ALL() { return document.getElementById(this.idMENU_ALL); }, |
---|
15 | |
---|
16 | get CMENU() { return document.getElementById(this.idCMENU); }, |
---|
17 | |
---|
18 | get TITLE() { return window.top.document.getElementById("sidebar-title"); }, |
---|
19 | |
---|
20 | get STRING() { return window.top.document.getElementById("MarkingCollectionOverlayString"); }, |
---|
21 | get DataSource(){ return window.top.bitsObjectMng.DataSource; }, |
---|
22 | get Common() { return window.top.bitsObjectMng.Common; }, |
---|
23 | get XPath() { return window.top.bitsObjectMng.XPath; }, |
---|
24 | get Database() { return window.top.bitsObjectMng.Database; }, |
---|
25 | get gBrowser() { return window.top.bitsObjectMng.getBrowser();}, |
---|
26 | |
---|
27 | get xmldoc() { return this._xmldoc; }, |
---|
28 | get xmlurl() { return (this._xmlfile?this.Common.convertFilePathToURL(this._xmlfile.path):""); }, |
---|
29 | |
---|
30 | get confirmDelete(){ return true; }, |
---|
31 | |
---|
32 | baseURL : "", |
---|
33 | shouldRebuild : false, |
---|
34 | |
---|
35 | init : function(){ |
---|
36 | var extensionDir = this.Common.getExtensionDir().clone(); |
---|
37 | if(extensionDir){ |
---|
38 | this._xmlfile = extensionDir.clone(); |
---|
39 | this._xmlfile.append("substitutiontree.xml"); |
---|
40 | if(!this._xmlfile.exists()){ |
---|
41 | var aContent = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n'; |
---|
42 | aContent += '<!DOCTYPE items>\n<items/>\n'; |
---|
43 | this.Common.writeFile(this._xmlfile,aContent,"UTF-8"); |
---|
44 | } |
---|
45 | function _loadXMLDocument(pUri){ |
---|
46 | if(pUri == undefined) return undefined; |
---|
47 | var xmlDocument = window.top.bitsMarkingCollection.loadXMLDocument(pUri); |
---|
48 | if(xmlDocument){ |
---|
49 | return xmlDocument; |
---|
50 | }else{ |
---|
51 | return undefined; |
---|
52 | } |
---|
53 | } |
---|
54 | function _createXMLDocument(aXMLFile){ |
---|
55 | if(!aXMLFile) return undefined; |
---|
56 | try{ |
---|
57 | return _loadXMLDocument(bitsSubstitutionTree.Common.IO.newFileURI(aXMLFile).spec); |
---|
58 | }catch(ex){ |
---|
59 | bitsSubstitutionTree._dump("bitsSubstitutionTree._createXMLDocument():"+ ex); |
---|
60 | return undefined; |
---|
61 | } |
---|
62 | } |
---|
63 | this._xmldoc = _createXMLDocument(this._xmlfile); |
---|
64 | } |
---|
65 | }, |
---|
66 | |
---|
67 | done : function(){ |
---|
68 | this.xmlflash(); |
---|
69 | }, |
---|
70 | |
---|
71 | rebuild : function(){ |
---|
72 | }, |
---|
73 | |
---|
74 | xmlflash : function(){ |
---|
75 | var s = new XMLSerializer(); |
---|
76 | var xml = s.serializeToString(bitsSubstitutionTree.xmldoc); |
---|
77 | bitsSubstitutionTree.Common.writeFile(bitsSubstitutionTree._xmlfile, xml+"\n","UTF-8"); |
---|
78 | }, |
---|
79 | |
---|
80 | addSubstitution : function(aItem){ |
---|
81 | var results = this.XPath.evaluateArray('//item[@src="'+aItem.src+'"]',this.xmldoc); |
---|
82 | if(results.length){ |
---|
83 | this.Common.alert(this.STRING.getString("ALERT_REGISTERED")); |
---|
84 | return false; |
---|
85 | } |
---|
86 | if(this.xmldoc){ |
---|
87 | var node = this.xmldoc.createElement("item"); |
---|
88 | node.setAttribute("src",aItem.src); |
---|
89 | node.setAttribute("alt",aItem.alt); |
---|
90 | this.xmldoc.documentElement.appendChild(node); |
---|
91 | this.xmlflash(); |
---|
92 | } |
---|
93 | }, |
---|
94 | |
---|
95 | _confirmRemovingFor : function(){ |
---|
96 | try{ |
---|
97 | if(bitsSubstitutionTree.confirmDelete){ |
---|
98 | return this.Common.confirm( bitsSubstitutionTree.STRING.getString("CONFIRM_DELETE") ); |
---|
99 | } |
---|
100 | return true; |
---|
101 | }catch(e){ |
---|
102 | return false; |
---|
103 | } |
---|
104 | }, |
---|
105 | |
---|
106 | removeSubstitution : function(aEvent){ |
---|
107 | if(!this.xmldoc || !this._explicitOriginalTarget) return; |
---|
108 | if(!this._confirmRemovingFor()) return; |
---|
109 | var src = this._explicitOriginalTarget.getAttribute("src"); |
---|
110 | var results = this.XPath.evaluateArray('//item[@src="'+src+'"]',this.xmldoc); |
---|
111 | if(results.length){ |
---|
112 | var i; |
---|
113 | for(i=results.length-1;i>=0;i--){ |
---|
114 | results[i].parentNode.removeChild(results[i]); |
---|
115 | } |
---|
116 | this.xmlflash(); |
---|
117 | } |
---|
118 | this._explicitOriginalTarget = undefined; |
---|
119 | }, |
---|
120 | |
---|
121 | getSubstitutionText : function(aNode){ |
---|
122 | var selectimportimage = nsPreferences.copyUnicharPref("wiredmarker.selectimportimage","none"); |
---|
123 | var src = (aNode.src?aNode.src:""); |
---|
124 | var alt = (aNode.alt?aNode.alt:""); |
---|
125 | if(selectimportimage == "alt") return alt; |
---|
126 | if(selectimportimage == "alt_substitution" && alt != "") return alt; |
---|
127 | if(selectimportimage == "substitution") alt = ""; |
---|
128 | src = src.replace(/^\s*/mg,"").replace(/\s*$/mg,""); |
---|
129 | if(src == "") return ""; |
---|
130 | try{ |
---|
131 | var aURI = this.Common.convertURLToObject(src); |
---|
132 | var aURL = aURI.QueryInterface(Components.interfaces.nsIURL); |
---|
133 | }catch(e){} |
---|
134 | if(!aURL) return ""; |
---|
135 | var results = this.XPath.evaluateArray('//item[@src="'+aURL.fileName+'"]',this.xmldoc); |
---|
136 | if(results.length){ |
---|
137 | alt = results[0].getAttribute("alt"); |
---|
138 | }else{ |
---|
139 | var confirm = nsPreferences.getBoolPref("wiredmarker.substitution.confirm", true); |
---|
140 | if(confirm){ |
---|
141 | if(this.Common.confirm( this.STRING.getString("CONFIRM_SUBSTITUTION_TABLE_NONE") )){ |
---|
142 | var src = {value: aURL.fileName}; |
---|
143 | var alt = {value: (aNode.alt?aNode.alt:"")}; |
---|
144 | var check = {value: false}; |
---|
145 | var result = false; |
---|
146 | do{ |
---|
147 | result = this.Common.PROMPT.prompt(window, this.STRING.getString("MSG_SUBSTITUTION_ADD"), this.STRING.getString("MSG_SUBSTITUTION_INPUT_FILENAME"), src, null, check); |
---|
148 | if(!result) return alt; |
---|
149 | src.value = src.value.replace(/^\s*/mg,"").replace(/\s*$/mg,""); |
---|
150 | }while(src.value == ""); |
---|
151 | var src_results = this.XPath.evaluateArray('//item[@src="'+src.value+'"]',this.xmldoc); |
---|
152 | if(src_results.length){ |
---|
153 | this.Common.alert(this.STRING.getString("ALERT_REGISTERED")); |
---|
154 | return alt; |
---|
155 | } |
---|
156 | result = this.Common.PROMPT.prompt(window, this.STRING.getString("MSG_SUBSTITUTION_ADD"), this.STRING.getString("MSG_SUBSTITUTION_FILENAME") + ":["+src.value+"]\n" + this.STRING.getString("MSG_SUBSTITUTION_INPUT_SUBSTITUTE_TEXT"), alt, null, check); |
---|
157 | if(!result) return alt; |
---|
158 | var node = this.xmldoc.createElement("item"); |
---|
159 | node.setAttribute("src",src.value); |
---|
160 | node.setAttribute("alt",alt.value); |
---|
161 | this.xmldoc.documentElement.appendChild(node); |
---|
162 | alt = alt.value; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | return (alt?alt:""); |
---|
167 | }, |
---|
168 | |
---|
169 | ///////////////////////////////////////////////////////////////////// |
---|
170 | _dump : function(aString){ |
---|
171 | window.top.bitsMarkingCollection._dump(aString); |
---|
172 | }, |
---|
173 | }; |
---|
174 | |
---|