-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.Codepress.js
225 lines (202 loc) · 7.75 KB
/
jquery.Codepress.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
### jQuery Codepress Plugin v1.2 - 2008-04-17
### * http://www.fyneworks.com/ - [email protected]
Licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License
###
Official Project: http://jquery.com/plugins/project/Codepress/
Official Website: http://www.fyneworks.com/jquery/Codepress/
*/
// 25-Jul-2007 - v1.0 - it begins...
// 12-Jan-2008 - v1.1 - plugin now follows the jquery philosophy of $(selector).plugin();
// 12-Jan-2008 - the new structure allows multiple configurations on one page
// 20-Feb-2008 - fixed bug (option variable isolation): http://plugins.jquery.com/node/900
/*
USAGE:
$.codepress.start({ path:'/path/to/codepress/editor/' }); // initialize Codepress editor
$.codepress.update(); // update value in textareas of each Codepress editor instance
*/
/*# AVOID COLLISIONS #*/
;if(window.jQuery) (function($){
/*# AVOID COLLISIONS #*/
$.extend($, {
codepress:{
config: { }, // default configuration
path: '/codepress/', // default path to Codepress directory
list: [], // holds a list of instances
loaded: false, // flag indicating whether Codepress script is loaded
intercepted: null, // variable to store intercepted method(s)
// utility method to read contents of Codepress editor
content: function(i){
try{
return i.codepress.editor.getCode();
}catch(e){ return ''; };
}, // codepress.content function
// utility method to update textarea contents before ajax submission
update: function(){
// Update contents of all instances
var e = $.codepress.list;
for(var i=0;i<e.length;i++){
var ta = e[i].textarea;
var T = $(e[i]);
//if(T.size()>0){
var ht = $.codepress.content(e[i]);
T.text(ht).val(ht).attr('disabled',false);
if(ht!=ta.val())
alert('Critical error in Codepress plugin:'+'\n'+'Unable to update form data');
//}
};
}, // codepress.update
// utility method to create instances of Codepress editor (if any)
create: function(option){
// Create a new options object
var o = $.extend({}/* new object */, $.codepress.config || {}, option || {});
// Plugin options
$.extend(o,{
selector: (o.selector || 'textarea.code, textarea.codepress'),
path: (o.path || $.codepress.path || '')
});
// Find codepress.editor-instance 'wannabes'
var e = $(o.e);
if(!e.length>0) e = $(o.selector);
if(!e.length>0) return;
// Accept settings from metadata plugin
o = $.extend({}, o,
($.meta ? e.data()/*NEW metadata plugin*/ :
($.metadata ? e.metadata()/*OLD metadata plugin*/ :
null/*metadata plugin not available*/)) || {}
);
// Load script and create instances
if(!$.codepress.loading && !$.codepress.loaded){
$.codepress.loading = true;
$.getScript(
o.path+'codepress.js',
function(){ $.codepress.loaded = true; }
);
};
// Start editor
var start = function(e){
if($.codepress.loaded){
//if(window.console) console.log(['codepress.create','start',e,o]);
$.codepress.editor(e,o);
}
else{
//if(window.console) console.log(['codepress.create','waiting for script...',e,o]);
if($.codepress.waited<=0){
alert('jQuery.codepress plugin error: The Codepress script did not load.');
}
else{
$.codepress.waitFor--;
window.setTimeout(start,1000,e);
};
}
};
start(e);//window.setTimeout(startIfReady,0,e);
// Return matched elements...
return e;
},
// utility method to integrate this plugin with others...
intercept: function(){
if($.codepress.intercepted) return;
// This method intercepts other known methods which
// require up-to-date code from FCKEditor
$.codepress.intercepted = {
ajaxSubmit: $.fn.ajaxSubmit || function(){}
};
$.fn.ajaxSubmit = function(){
$.codepress.update(); // update html
return $.codepress.intercepted.ajaxSubmit.apply( this, arguments );
};
// Also attach to conventional form submission
$('form').submit(function(){
$.codepress.update(); // update html
});
},
// utility method to create an instance of Codepress editor
editor: function(e /* elements */, o /* options */){
o = $.extend($.codepress.config || {}, o || {});
// Default configuration
$.extend(o,{
width: (o.width || o.Width || '100%'),
height: (o.height || o.Height|| '500px'),
path: (o.path || $.codepress.path)
});
// Make sure we have a jQuery object
e = $(e);
if(e.size()>0){
// Local array to store instances
var a = ($.codepress.list || []);
// Go through objects and initialize codepress.editor
e.each(
function(i,t){
var T = $(t);// t = element, T = jQuery
t.id = (t.id || 'codepress'+($.codepress.list.length+1));
if(t.id/* has id */ && !t.codepress/* not already installed */){
var n = a.length;
//BUG: Sometimes, codepress creates an editor with height/width 0.
// The next line will manually set the dimensions of the editor
T.show().height(o.height).width(o.width);
// OPTIONS: linenumbers-off, autocomplete-off and readonly-on
var c0 = String(t.className), c2 = '';
c2 += ' codepress';
c2 += (' linenumbers-' + (!c0.match(/linenumbers/g)?'on':'off'));
c2 += (' autocomplete-' + (c0.match(/autocomplete/g)?'on':'off'));
c2 += (' readonly-' + (c0.match(/readonly/g)?'on':'off'));
c2 += ' '+(c0.match(/html|css|php|java|javascript|perl|ruby|sql|text/gi) || ['html'])[0];
t.className = c2.replace(/(^\s+)|(\s+$)/gi,'');
/*
I use the class property to store non-html data and other javascript settings.
The current codepress implementation does not allow me to have any data in the
class property other than the class names used by codepress.
On line 40 of codepress.js:
self.language = language ? language : self.options.replace(/ ?codepress ?| ?readonly-on ?| ?autocomplete-off ?| ?linenumbers-off ?/g,'');
I'd like to suggest this flexible version, which allows other classes to be used:
self.language = language ? language : (self.options.match(/html|css|php|java|javascript|perl|ruby|sql|text/gi) || ['generic'])[0];
The modification looks like this:
// Original:
//self.language = language ? language : self.options.replace(/ ?codepress ?| ?readonly-on ?| ?autocomplete-off ?| ?linenumbers-off ?/g,'');
// Allow other class names: (by [email protected])
self.language = language ? language : (self.options.match(/html|css|php|java|javascript|perl|ruby|sql|text/gi) || ['generic'])[0];
*/
// Initialize Codepress
CodePress.engine = CodePress.getEngine();
CodePress.path = o.path;
// Create Codepress instance
t.codepress = new CodePress(t);
// 1. Insert Editor
// 2. Re-enable textarea (we still want to submit the data)
// - why does codepress disable the textarea?
T.attr('disabled',false).parent().prepend(t.codepress);
// Store element reference to be used later...
a[n] = t;
a[n].textarea = T;
};
}
);
// Store instances in global array
$.codepress.list = a;
};
// return jQuery array of elements
return e;
}, // codepress.editor function
// start-up method
start: function(o/* options */){
// Attach itself to known plugins...
$.codepress.intercept();
// Create Codepress editors
return $.codepress.create(o);
} // codepress.start
} // codepress object
//##############################
});
// extend $
//##############################
$.extend($.fn, {
codepress: function(o){
$.codepress.start($.extend(o || {}, {e: this}));
}
});
// extend $.fn
//##############################
/*# AVOID COLLISIONS #*/
})(jQuery);
/*# AVOID COLLISIONS #*/