summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts/source/prado/controls/htmlarea.js
blob: 4cb37c6f810a8ea65256888ee1373428b81721b4 (plain)
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

/*
 * 
 * HtmlArea (tinyMCE) wrapper
 *
 * @author Gabor Berczi <gabor.berczi@devworx.hu>
 *
*/


Prado.WebUI.THtmlArea = Class.create(Prado.WebUI.Control,
{
	initialize: function($super, options)
	{
		options.ID = options.EditorOptions.elements;
		$super(options);
	},

    onInit : function(options)
	{
		this.options = options;

		var obj = this;
		this.ajaxresponder = {
			onComplete : function(request) 
			{
				if(request && (request instanceof Prado.AjaxRequest))
					obj.checkInstance();
			}
		};
		this.registerAjaxHook();

		this.registerInstance();
	},
	
	registerInstance: function()
	{
		if (typeof tinyMCE_GZ == 'undefined')
			{
				if (typeof tinyMCE == 'undefined')
					{
						if (typeof Prado.CallbackRequest != 'undefined')
							if (typeof Prado.CallbackRequest.transport != 'undefined')
							{
								// we're in a callback
								// try it again in some time, as tinyMCE is most likely still loading
								this.setTimeout(this.registerInstance.bind(this), 50); 
								return;
							}
						throw "TinyMCE libraries must be loaded first";
					}
				Prado.WebUI.THtmlArea.tinyMCELoadState = 255;
				this.initInstance();
			}
		else
			if (Prado.WebUI.THtmlArea.tinyMCELoadState==255)
				this.initInstance();
			else
				{
					Prado.WebUI.THtmlArea.pendingRegistrations.push(this.options.ID);
					if (Prado.WebUI.THtmlArea.tinyMCELoadState==0)
					{
						Prado.WebUI.THtmlArea.tinyMCELoadState = 1;
						tinyMCE_GZ.init(
							this.options.CompressionOptions,
							this.compressedScriptsLoaded.bind(this)
						);
					}
				}
	},
	
	compressedScriptsLoaded: function()
	{
		Prado.WebUI.THtmlArea.tinyMCELoadState = 255;
		tinymce.dom.Event._pageInit();
		var wrapper;
		while(Prado.WebUI.THtmlArea.pendingRegistrations.length>0)
			if (wrapper = Prado.Registry.get(Prado.WebUI.THtmlArea.pendingRegistrations.pop()))
				wrapper.initInstance();
	},

	initInstance: function()
	{
		tinyMCE.init(this.options.EditorOptions);
	},

	checkInstance: function()
	{
		if (!document.getElementById(this.ID))
			this.deinitialize();
	},

	removePreviousInstance: function()
	{
		for(var i=0;i<tinyMCE.editors.length;i++)
			if (tinyMCE.editors[i].id==this.ID)
			{
				tinyMCE.editors.splice(i,1); // ugly hack, but works
				this.deRegisterAjaxHook();
				this.deregister();
				i--;
			}
	},

	registerAjaxHook: function()
	{
		if (typeof(Ajax)!="undefined")
			if (typeof(Ajax.Responders)!="undefined")
				Ajax.Responders.register(this.ajaxresponder);
	},


	deRegisterAjaxHook: function()
	{
		if (typeof(Ajax)!="undefined")
			if (typeof(Ajax.Responders)!="undefined")
				Ajax.Responders.unregister(this.ajaxresponder);
	},

	onDone: function()
	{
		// check for previous tinyMCE registration, and try to remove it gracefully first
		var prev = tinyMCE.get(this.ID);
		if (prev)
		try
		{
			tinyMCE.execCommand('mceFocus', false, this.ID); 
			tinyMCE.execCommand('mceRemoveControl', false, this.ID);
		}
		catch (e) 
		{
			// suppress error here in case editor can't be properly removed
			// (happens when <textarea> has been removed from DOM tree without deinitialzing the tinyMCE editor first)
		}

		// doublecheck editor instance here and remove manually from tinyMCE-registry if neccessary
		this.removePreviousInstance();

		this.deRegisterAjaxHook();
	}
});

Object.extend(Prado.WebUI.THtmlArea, 
{
	pendingRegistrations : [],
	tinyMCELoadState : 0
});