|
| 1 | +/** |
| 2 | + * @author thansen <[email protected]> |
| 3 | + * @licence solire.fr |
| 4 | + * |
| 5 | + * @param jQuery $ |
| 6 | + * @param tinymce tinymce |
| 7 | + * |
| 8 | + * @returns jQuery |
| 9 | + */ |
| 10 | +(function($, tinymce) { |
| 11 | + /** |
| 12 | + * |
| 13 | + * @param {string} method |
| 14 | + * |
| 15 | + * @returns jQuery |
| 16 | + */ |
| 17 | + $.fn.tinymce = function(method) { |
| 18 | + var base = this; |
| 19 | + publicMethods = {}; |
| 20 | + |
| 21 | + /** |
| 22 | + * |
| 23 | + * @param {int} length |
| 24 | + * |
| 25 | + * @returns {String} |
| 26 | + */ |
| 27 | + function randomString(length) |
| 28 | + { |
| 29 | + var i, |
| 30 | + text = '', |
| 31 | + possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 32 | + + 'abcdefghijklmnopqrstuvwxyz' |
| 33 | + + '0123456789'; |
| 34 | + |
| 35 | + for(i=0; i < length; i++) { |
| 36 | + text += possible.charAt(Math.floor(Math.random() * possible.length)); |
| 37 | + } |
| 38 | + |
| 39 | + return text; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Génére un attribut id unique (pour element HTML) |
| 44 | + * |
| 45 | + * @returns {String} |
| 46 | + */ |
| 47 | + function randomId() |
| 48 | + { |
| 49 | + do{ |
| 50 | + tmpId = randomString(10); |
| 51 | + } while ($('#tinymce-' + tmpId).length > 0) |
| 52 | + |
| 53 | + return 'tinymce-' + tmpId; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Adds a public method to the base element, to allow methods calls |
| 58 | + * to be made on the returned object. |
| 59 | + * |
| 60 | + * @param {String} name |
| 61 | + * @param {function} func |
| 62 | + * |
| 63 | + * @return {void} |
| 64 | + */ |
| 65 | + function addMethod(name, func) { |
| 66 | + publicMethods[name] = func; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * |
| 71 | + * |
| 72 | + * @returns void |
| 73 | + */ |
| 74 | + function _enable() |
| 75 | + { |
| 76 | + tinyMCE.execCommand('mceAddEditor', false, this.id); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * |
| 81 | + * |
| 82 | + * @returns void |
| 83 | + */ |
| 84 | + function _disable() |
| 85 | + { |
| 86 | + tinyMCE.execCommand('mceFocus', false, this.id); |
| 87 | + tinyMCE.execCommand('mceRemoveEditor', false, this.id); |
| 88 | + tinyMCE.triggerSave(true, true); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * |
| 93 | + * |
| 94 | + * @returns void |
| 95 | + */ |
| 96 | + function change() |
| 97 | + { |
| 98 | + if(typeof tinyMCE.get(this.id) !== 'undefined') { |
| 99 | + _disable.call(this); |
| 100 | + } else { |
| 101 | + _enable.call(this); |
| 102 | + } |
| 103 | + |
| 104 | + // tinyMCE.execCommand('mceToggleEditor',false,this.id); |
| 105 | + }; |
| 106 | + |
| 107 | + /** |
| 108 | + * |
| 109 | + * |
| 110 | + * @returns void |
| 111 | + */ |
| 112 | + function disable() |
| 113 | + { |
| 114 | + if (typeof tinyMCE.get(this.id) !== 'undefined') { |
| 115 | + _disable.call(this); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * |
| 121 | + * |
| 122 | + * @returns void |
| 123 | + */ |
| 124 | + function enable() |
| 125 | + { |
| 126 | + if (typeof tinyMCE.get(this.id) === 'undefined') { |
| 127 | + _enable.call(this); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + function isFunc(func) |
| 132 | + { |
| 133 | + return $.isFunction(func); |
| 134 | + } |
| 135 | + |
| 136 | + function init() |
| 137 | + { |
| 138 | + if (!$(this).data('tynimce')) { |
| 139 | + if (this.id === null || this.id === '' |
| 140 | + || $('[id=' + this.id + ']').length > 1 |
| 141 | + ) { |
| 142 | + this.id = randomId(); |
| 143 | + $(this).attr('tynimce-id', this.id); |
| 144 | + } |
| 145 | + |
| 146 | + $(this).data('tynimce', true); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + addMethod('disable', disable); |
| 151 | + addMethod('enable', enable); |
| 152 | + addMethod('change', change); |
| 153 | + |
| 154 | + return base.each(function(){ |
| 155 | + init.call(this); |
| 156 | + |
| 157 | + if (method in publicMethods && isFunc(publicMethods[method])) { |
| 158 | + publicMethods[method].call(this); |
| 159 | + } |
| 160 | + }); |
| 161 | + }; |
| 162 | +})(jQuery, tinymce); |
| 163 | + |
0 commit comments