4

I am using j-query tinyMce plugin as editor . Everything was going fine . But now i need to add a custom button so that i can insert date from a date picker . What i did is

added a button in the toolbar . and its working to show the dates in a text area which is outside the editor . But i want to show the datepicker below that button . here is my current code .

setup: function(editor) {
  editor.addButton('datepicker', {
    type: 'button',
    class: 'MyCoolBtn',
    text: 'Datepicker',
    tooltip: 'Pick a date',
    onClick: function() {
      $('#datepicker').datepicker('show')
    },
    icon: true,
  });
}

here is a screenshot for better understanding enter image description here

Dhiraj
  • 33,140
  • 10
  • 61
  • 78
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68

3 Answers3

2

Here's a solution:

$(document).ready(function() {

  tinymce.init({
    selector: "textarea",

    toolbar: "insertfile undo redo | styleselect | bold italic | datepicker",
    setup: function(editor) {
      editor.addButton('datepicker', {
        type: 'button',
        classes: 'MyCoolBtn',
        text: 'Datepicker',
        tooltip: 'Pick a date',
        onClick: function() {
          $('#datepicker').datepicker('show');
        },
        //icon: true,
      });
    }
  });

  $("#datepicker").datepicker({
    onSelect: function(dateText, inst) {
      // insert into editor
      tinymce.activeEditor.execCommand('mceInsertContent', false, dateText);
    },

    beforeShow: function(input, inst) {
      // change position
      // see http://stackoverflow.com/questions/662220/how-to-change-the-pop-up-position-of-the-jquery-datepicker-control

      var $dpBtn = $(".mce-MyCoolBtn");
      var buttonPos = $dpBtn.position();

      inst.dpDiv.css({
        // cannot use top: and left: attrs here since they are hard-set inline anyway
        marginTop: buttonPos.top + $dpBtn.height() + 'px',
        marginLeft: buttonPos.left + 'px'
      });
    }
  });

});
#datepicker {
  display: none;
}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

<script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>

<input type="text" id="datepicker"></input>
<textarea rows="10" cols="40" id="editor"></textarea>

Note the usage of datepicker options beforeShow and onSelect and check out the comments.

Just in case JSFiddle

dekkard
  • 6,121
  • 1
  • 16
  • 26
  • not working if i make input type text as display:none; or type hidden – Manoj Dhiman Jun 22 '15 at 05:08
  • What in particular is not working? As you can see in the fiddle the date input has ```display: none``` and the datepicker shows up and pastes the chosen date into the editor, i.e. looks like it's working as expected. – dekkard Jun 22 '15 at 06:09
0

Here is a working fiddle: http://jsfiddle.net/10yz8q55/ We reposition the datepicker pop-up from the input field to any other element:

$('.datepicker').datepicker({
    beforeShow: function (input, inst) {
        setTimeout(function () {
            var button = $('#button-tinymce-datepicker'); //the element where the popup should be
            var offset = button.offset();
            inst.dpDiv.css({
                top: offset.top,
                left: offset.left
            });
        }, 0);
    }
});
ggzone
  • 3,661
  • 8
  • 36
  • 59
0

An unfortunate hack would be to add a hidden <input> to your menu as well. This would allow you to bind the datepicker to the input which makes it display near/under it.

ethorn10
  • 1,889
  • 1
  • 18
  • 29