Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16345 anikendra 1
!function($, wysi) {
2
	"use strict"
3
 
4
	var templates = {
5
		"font-styles": "<li class='dropdown'>" +
6
							"<a class='btn dropdown-toggle' data-toggle='dropdown' href='#'>" +
7
								"<i class='icon-font'></i>&nbsp;<span class='current-font'>Normal text</span>&nbsp;<b class='caret'></b>" +
8
							"</a>" +
9
						    "<ul class='dropdown-menu'>" +
10
						      	"<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='div'>Normal text</a></li>" +
11
					            "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h1'>Heading 1</a></li>" +
12
					            "<li><a data-wysihtml5-command='formatBlock' data-wysihtml5-command-value='h2'>Heading 2</a></li>" +
13
						    "</ul>" +
14
						"</li>",
15
		"emphasis":     "<li>" +
16
							"<div class='btn-group'>" 
17
							    + "<a class='btn' data-wysihtml5-command='bold' title='CTRL+B'>Bold</a>" 
18
							    + "<a class='btn' data-wysihtml5-command='italic' title='CTRL+I'>Italic</a>" 
19
							    //,+ "<a class='btn' data-wysihtml5-command='underline' title='CTRL+U'>Underline</a>" 
20
							+ "</div>" 
21
						+ "</li>",
22
		"lists": 	"<li>" 
23
						+ "<div class='btn-group'>" 
24
					    	+ "<a class='btn' data-wysihtml5-command='insertUnorderedList' title='Unordered List'><i class='glyphicon glyphicon-list'></i></a>" 
25
						    + "<a class='btn' data-wysihtml5-command='insertOrderedList' title='Ordered List'><i class='glyphicon glyphicon-list-alt'></i></a>" 
26
						    + "<a class='btn' data-wysihtml5-command='Outdent' title='Outdent'><i class='glyphicon glyphicon-indent-right'></i></a>"  							    
27
						    + "<a class='btn' data-wysihtml5-command='Indent' title='Indent'><i class='glyphicon glyphicon-indent-left'></i></a>" 
28
						+ "</div>" 
29
					+ "</li>",
30
 
31
		"link": 	"<li>" 
32
 
33
						+ "<div class='bootstrap-wysihtml5-insert-link-modal modal hide fade'>"
34
							+ "<div class='modal-header'>"
35
							+ "<a class='close' data-dismiss='modal'>×</a>"
36
							  + "<h3>Insert Link</h3>"
37
							+ "</div>"
38
							+ "<div class='modal-body'>"
39
							  + "<input value='http://' class='bootstrap-wysihtml5-insert-link-url input-xlarge'>"
40
							+ "</div>"
41
							+ "<div class='modal-footer'>"
42
							  + "<a href='#' class='btn' data-dismiss='modal'>Cancel</a>"
43
							  + "<a href='#' class='btn btn-primary' data-dismiss='modal'>Insert link</a>"
44
							+ "</div>"
45
						+ "</div>"
46
 
47
				    	+ "<a class='btn' data-wysihtml5-command='createLink' title='Link'><i class='glyphicon glyphicon-link'></i></a>" 
48
 
49
					+ "</li>",
50
 
51
			"image": "<li>" 
52
 
53
						+ "<div class='bootstrap-wysihtml5-insert-image-modal modal hide fade'>"
54
							+ "<div class='modal-header'>"
55
							+ "<a class='close' data-dismiss='modal'>×</a>"
56
							  + "<h3>Insert Image</h3>"
57
							+ "</div>"
58
							+ "<div class='modal-body'>"
59
							  + "<input value='http://' class='bootstrap-wysihtml5-insert-image-url input-xlarge'>"
60
							+ "</div>"
61
							+ "<div class='modal-footer'>"
62
							  + "<a href='#' class='btn' data-dismiss='modal'>Cancel</a>"
63
							  + "<a href='#' class='btn btn-primary' data-dismiss='modal'>Insert image</a>"
64
							+ "</div>"
65
						+ "</div>"
66
 
67
						+ "<a class='btn' data-wysihtml5-command='insertImage' title='Insert image'><i class='glyphicon glyphicon-picture'></i></a>" 
68
 
69
					+ "</li>",
70
 
71
		"html": 
72
						"<li>"
73
							+ "<div class='btn-group'>"
74
								+ "<a class='btn' data-wysihtml5-action='change_view' title='Edit HTML'><i class='glyphicon glyphicon-pencil'></i></a>" 
75
							+ "</div>"
76
						+ "</li>"
77
	};
78
 
79
	var defaultOptions = {
80
		"font-styles": true,
81
		"emphasis": true,
82
		"lists": true,
83
		"html": false,
84
		"link": true,
85
		"image": true,
86
		events: {},
87
		parserRules: {
88
			tags: {
89
				"b":  {},
90
				"i":  {},
91
				"br": {},
92
				"ol": {},
93
				"ul": {},
94
				"li": {},
95
				"h1": {},
96
				"h2": {},
97
				"u": 1,
98
				"img": {
99
					"check_attributes": {
100
			            "width": "numbers",
101
			            "alt": "alt",
102
			            "src": "url",
103
			            "height": "numbers"
104
			        }
105
				},
106
				"a":  {
107
					set_attributes: {
108
						target: "_blank",
109
						rel:    "nofollow"
110
					},
111
					check_attributes: {
112
						href:   "url" // important to avoid XSS
113
					}
114
				}
115
			}
116
		}
117
	};
118
 
119
	var Wysihtml5 = function(el, options) {
120
		this.el = el;
121
		this.toolbar = this.createToolbar(el, options || defaultOptions);
122
		this.editor =  this.createEditor(options);
123
 
124
		window.editor = this.editor;
125
 
126
  		$('iframe.wysihtml5-sandbox').each(function(i, el){
127
			$(el.contentWindow).off('focus.wysihtml5').on({
128
			  'focus.wysihtml5' : function(){
129
			     $('li.dropdown').removeClass('open');
130
			   }
131
			});
132
		});
133
	};
134
 
135
	Wysihtml5.prototype = {
136
		constructor: Wysihtml5,
137
 
138
		createEditor: function(options) {
139
			var parserRules = defaultOptions.parserRules; 
140
 
141
			if(options && options.parserRules) {
142
				parserRules = options.parserRules;
143
			}
144
 
145
			var editor = new wysi.Editor(this.el.attr('id'), {
146
	    		toolbar: this.toolbar.attr('id'),
147
				parserRules: parserRules
148
	  		});
149
 
150
	  		if(options && options.events) {
151
				for(var eventName in options.events) {
152
					editor.on(eventName, options.events[eventName]);
153
				}
154
			}	
155
 
156
	  		return editor;
157
		},
158
 
159
		createToolbar: function(el, options) {
160
			var self = this;
161
			var toolbar = $("<ul/>", {
162
				'id' : el.attr('id') + "-wysihtml5-toolbar",
163
				'class' : "wysihtml5-toolbar",
164
				'style': "display:none"
165
			});
166
 
167
			for(var key in defaultOptions) {
168
				var value = false;
169
 
170
				if(options[key] != undefined) {
171
					if(options[key] == true) {
172
						value = true;
173
					}
174
				} else {
175
					value = defaultOptions[key];
176
				}
177
 
178
				if(value == true) {
179
					toolbar.append(templates[key]);
180
 
181
					if(key == "html") {
182
						this.initHtml(toolbar);
183
					}
184
 
185
					if(key == "link") {
186
						this.initInsertLink(toolbar);
187
					}
188
 
189
					if(key == "image") {
190
						this.initInsertImage(toolbar);
191
					}
192
				}
193
			}
194
 
195
			var self = this;
196
 
197
			toolbar.find("a[data-wysihtml5-command='formatBlock']").click(function(e) {
198
				var el = $(e.srcElement);
199
				self.toolbar.find('.current-font').text(el.html())
200
			});
201
 
202
			this.el.before(toolbar);
203
 
204
			return toolbar;
205
		},
206
 
207
		initHtml: function(toolbar) {
208
			var changeViewSelector = "a[data-wysihtml5-action='change_view']";
209
			toolbar.find(changeViewSelector).click(function(e) {
210
				toolbar.find('a.btn').not(changeViewSelector).toggleClass('disabled');
211
			});
212
		},
213
 
214
		initInsertImage: function(toolbar) {
215
			var self = this;
216
			var insertImageModal = toolbar.find('.bootstrap-wysihtml5-insert-image-modal');
217
			var urlInput = insertImageModal.find('.bootstrap-wysihtml5-insert-image-url');
218
			var insertButton = insertImageModal.find('a.btn-primary');
219
			var initialValue = urlInput.val();
220
 
221
			var insertImage = function() { 
222
				var url = urlInput.val();
223
				urlInput.val(initialValue);
224
				self.editor.composer.commands.exec("insertImage", url);
225
			};
226
 
227
			urlInput.keypress(function(e) {
228
				if(e.which == 13) {
229
					insertImage();
230
					insertImageModal.modal('hide');
231
				}
232
			});
233
 
234
			insertButton.click(insertImage);
235
 
236
			insertImageModal.on('shown', function() {
237
				urlInput.focus();
238
			});
239
 
240
			insertImageModal.on('hide', function() { 
241
				self.editor.currentView.element.focus();
242
			});
243
 
244
			toolbar.find('a[data-wysihtml5-command=insertImage]').click(function() {
245
				insertImageModal.modal('show');
246
			});
247
		},
248
 
249
		initInsertLink: function(toolbar) {
250
			var self = this;
251
			var insertLinkModal = toolbar.find('.bootstrap-wysihtml5-insert-link-modal');
252
			var urlInput = insertLinkModal.find('.bootstrap-wysihtml5-insert-link-url');
253
			var insertButton = insertLinkModal.find('a.btn-primary');
254
			var initialValue = urlInput.val();
255
 
256
			var insertLink = function() { 
257
				var url = urlInput.val();
258
				urlInput.val(initialValue);
259
				self.editor.composer.commands.exec("createLink", { 
260
					href: url, 
261
					target: "_blank", 
262
					rel: "nofollow" 
263
				});
264
			};
265
			var pressedEnter = false;
266
 
267
			urlInput.keypress(function(e) {
268
				if(e.which == 13) {
269
					insertLink();
270
					insertLinkModal.modal('hide');
271
				}
272
			});
273
 
274
			insertButton.click(insertLink);
275
 
276
			insertLinkModal.on('shown', function() {
277
				urlInput.focus();
278
			});
279
 
280
			insertLinkModal.on('hide', function() { 
281
				self.editor.currentView.element.focus();
282
			});
283
 
284
			toolbar.find('a[data-wysihtml5-command=createLink]').click(function() {
285
				insertLinkModal.modal('show');
286
			});
287
		}
288
	};
289
 
290
	$.fn.wysihtml5 = function (options) {
291
		return this.each(function () {
292
			var $this = $(this);
293
	      	$this.data('wysihtml5', new Wysihtml5($this, options));
294
	    })
295
  	};
296
 
297
  	$.fn.wysihtml5.Constructor = Wysihtml5;
298
 
299
}(window.jQuery, window.wysihtml5);