OSDN Git Service

ADD: プラグイン「NP_Medium」
[nucleus-jp/nucleus-next.git] / nucleus / plugins / medium / scripts / medium.js
1 /* init */
2 if ( !medium )
3 {
4         var medium = new Object();
5         medium.type = 'inline';
6         medium.url = '';
7
8 }
9
10 /* for main window */
11 medium.addMedia = function()
12 {
13         window.open(this.url,
14          'Nucleus CMS Medium Plugin',
15          'status=yes,toolbar=no,scrollbars=yes,resizable=yes,width=500,height=450,top=0,left=0');
16         return;
17 };
18
19 medium.isCaretEmpty = function()
20 {
21         /* lastSelected object is on main window */
22         if ( lastSelected && lastSelected.createTextRange && lastSelected.caretPos )
23         {
24                 return ( lastSelected.caretPos.text == '' );
25         }
26         else if ( !document.all && document.getElementById )
27         {
28                 return ( mozSelectedText() == '' );
29         }
30         return true;
31 };
32
33 medium.getCaretText = function()
34 {
35         if ( !document.all && document.getElementById )
36         {
37                 return mozSelectedText();
38         }
39         return lastSelected.caretPos.text;
40 }
41
42 // inserts text at caret (overwriting selection)
43 medium.insertAtCaret = function(text)
44 {
45         /* lastSelected is on main window */
46         var textEl = lastSelected;
47         if ( textEl && textEl.createTextRange && textEl.caretPos )
48         {
49                 var caretPos = textEl.caretPos;
50                 
51                 if ( caretPos.text.charAt(caretPos.text.length - 1) != ' ' )
52                 {
53                         caretPos.text = text;
54                 }
55                 else
56                 {
57                         caretPos.text = text + ' ';
58                 }
59         }
60         else if ( !document.all && document.getElementById )
61         {
62                 mozReplace(document.getElementById('input' + nonie_FormType), text);
63                 if ( scrollTop > -1 )
64                 {
65                         document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
66                 }
67         }
68         else if ( textEl )
69         {
70                 textEl.value  += text;
71         }
72         else
73         {
74                 document.getElementById('input' + nonie_FormType).value += text;
75                 if ( scrollTop > -1 )
76                 {
77                         document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
78                 }
79         }
80         /* updAllPreviews() is on main window */
81         updAllPreviews();
82         return;
83 }
84
85 medium.includeImage = function(collection, filename, type, width, height)
86 {
87         var fullName;
88         var replaceBy;
89         
90         if ( this.isCaretEmpty() )
91         {
92                 text = prompt("Text to display ?", filename);
93         }
94         else
95         {
96                 text = this.getCaretText();
97         }
98         
99         /*
100          * add collection name when not private collection
101          * (or editing a message that's not your)
102          */
103         if ( isNaN(collection) || (nucleusAuthorId != collection) )
104         {
105                 fullName = collection + '/' + filename;
106         }
107         else
108         {
109                 fullName = filename;
110         }
111         
112         switch ( type )
113         {
114                 case 'popup':
115                         replaceBy = '<%popup(' +  fullName + '|'+width+'|'+height+'|' + text +')%>';
116                         break;
117                 case 'inline':
118                 default:
119                         replaceBy = '<%image(' +  fullName + '|'+width+'|'+height+'|' + text +')%>';
120                         break;
121         }
122         
123         this.insertAtCaret(replaceBy);
124         return;
125 }
126
127 medium.includeOtherMedia = function(collection, filename)
128 {
129         var fullName;
130         var replaceBy;
131         
132         if ( this.isCaretEmpty() )
133         {
134                 text = prompt("Text to display ?",filename);
135         }
136         else
137         {
138                 text = getCaretText();
139         }
140         
141         // add collection name when not private collection (or editing a message that's not your)
142         if ( isNaN(collection) || (nucleusAuthorId != collection) )
143         {
144                 fullName = collection + '/' + filename;
145         }
146         else
147         {
148                 fullName = filename;
149         }
150         
151         replaceBy = '<%media(' +  fullName + '|' + text +')%>';
152         
153         this.insertAtCaret(replaceBy);
154         return;
155 }
156
157 /* for sub window */
158 medium.setType = function(value)
159 {
160         this.type = value;
161 };
162
163 medium.chooseImage = function(collection, filename, width, height)
164 {
165         if ( this.type != 'inline' )
166         {
167                 this.type = 'popup';
168         }
169         
170         window.close();
171         window.opener.focus();
172         window.opener.medium.includeImage(collection, filename, this.type, width, height);
173         return;
174 }
175
176 medium.chooseOther = function(collection, filename)
177 {
178         
179         window.close();
180         window.opener.focus();
181         window.opener.medium.type = this.type;
182         window.opener.medium.includeOtherMedia(collection, filename);
183         return;
184 }