WIP: Note editing, markdown to html
[oweals/karmaworld.git] / karmaworld / apps / wysihtml5 / static / wysihtml5 / wysihtml-0.4.17 / src / dom / simulate_placeholder.js
1 /**
2  * Simulate HTML5 placeholder attribute
3  *
4  * Needed since
5  *    - div[contentEditable] elements don't support it
6  *    - older browsers (such as IE8 and Firefox 3.6) don't support it at all
7  *
8  * @param {Object} parent Instance of main wysihtml5.Editor class
9  * @param {Element} view Instance of wysihtml5.views.* class
10  * @param {String} placeholderText
11  *
12  * @example
13  *    wysihtml.dom.simulatePlaceholder(this, composer, "Foobar");
14  */
15 (function(dom) {
16   dom.simulatePlaceholder = function(editor, view, placeholderText) {
17     var CLASS_NAME = "placeholder",
18         unset = function() {
19           var composerIsVisible   = view.element.offsetWidth > 0 && view.element.offsetHeight > 0;
20           if (view.hasPlaceholderSet()) {
21             view.clear();
22             view.element.focus();
23             if (composerIsVisible ) {
24               setTimeout(function() {
25                 var sel = view.selection.getSelection();
26                 if (!sel.focusNode || !sel.anchorNode) {
27                   view.selection.selectNode(view.element.firstChild || view.element);
28                 }
29               }, 0);
30             }
31           }
32           view.placeholderSet = false;
33           dom.removeClass(view.element, CLASS_NAME);
34         },
35         set = function() {
36           if (view.isEmpty()) {
37             view.placeholderSet = true;
38             view.setValue(placeholderText);
39             dom.addClass(view.element, CLASS_NAME);
40           }
41         };
42
43     editor
44       .on("set_placeholder", set)
45       .on("unset_placeholder", unset)
46       .on("focus:composer", unset)
47       .on("paste:composer", unset)
48       .on("blur:composer", set);
49
50     set();
51   };
52 })(wysihtml5.dom);