'use strict'

var $ = window.jQuery
var DateTimeMenuItem = require('./DateTimeMenuItem')

module.exports = DynamicdatePlugin

function DynamicdatePlugin (editor) {
  var dateTimeMenuItemList = [
    new DateTimeMenuItem('dd/mm/yyyy, HH\'h\'MM\'\'\'ss'),
    new DateTimeMenuItem('dd/mm/yyyy, HH\'h\'MM'),
    new DateTimeMenuItem('dd/mm/yyyy'),
    new DateTimeMenuItem('HH\'h\'MM\'\'\'ss'),
    new DateTimeMenuItem('HH\'h\'MM'),
    new DateTimeMenuItem('dddd'),
    new DateTimeMenuItem('dd'),
    new DateTimeMenuItem('mm'),
    new DateTimeMenuItem('mmmm'),
    new DateTimeMenuItem('yy'),
    new DateTimeMenuItem('yyyy')
  ]

  editor.addMenuItem('dynamicdate', {
    context: 'insert',
    text: 'Dynamic date',
    type: 'menuitem',
    id: 'menu-dynamicdate',
    menu: dateTimeMenuItemList
  })

  $('body').on('menusController:mceMenuItemRendered', function (evt, menuId) {
    if (menuId.indexOf(DateTimeMenuItem.prototype.PREFIX_ID) === 0) {
      var $menuElement = $('#' + menuId)
      $.each(dateTimeMenuItemList, function (i, item) {
        if (item.id === $menuElement.attr('id')) {
          item.setElement($menuElement)
        }
      })
    }
  })
}

@@ -190,446 +190,36 @@
 t = 
-%5B%0A    new DateTimeMenuItem('dd/mm/yyyy, HH%5C'h%5C'MM%5C'%5C'%5C'ss'),%0A    new DateTimeMenuItem('dd/mm/yyyy, HH%5C'h%5C'MM'),%0A    new DateTimeMenuItem('dd/mm/yyyy'),%0A    new DateTimeMenuItem('HH%5C'h%5C'MM%5C'%5C'%5C'ss'),%0A    new DateTimeMenuItem('HH%5C'h%5C'MM'),%0A    new DateTimeMenuItem('dddd'),%0A    new DateTimeMenuItem('dd'),%0A    new DateTimeMenuItem('mm'),%0A    new DateTimeMenuItem('mmmm'),%0A    new DateTimeMenuItem('yy'),%0A    new DateTimeMenuItem('yyyy')%0A  %5D
+createDateTimeMenuItemList()
 %0A%0A  
@@ -759,8 +759,500 @@
 %0A  %7D)%0A%7D%0A
+%0Afunction createDateTimeMenuItemList () %7B%0A  return %5B%0A    new DateTimeMenuItem('dd/mm/yyyy, HH%5C'h%5C'MM%5C'%5C'%5C'ss'),%0A    new DateTimeMenuItem('dd/mm/yyyy, HH%5C'h%5C'MM'),%0A    new DateTimeMenuItem('dd/mm/yyyy'),%0A    new DateTimeMenuItem('HH%5C'h%5C'MM%5C'%5C'%5C'ss'),%0A    new DateTimeMenuItem('HH%5C'h%5C'MM'),%0A    new DateTimeMenuItem('dddd'),%0A    new DateTimeMenuItem('dd'),%0A    new DateTimeMenuItem('mm'),%0A    new DateTimeMenuItem('mmmm'),%0A    new DateTimeMenuItem('yy'),%0A    new DateTimeMenuItem('yyyy')%0A  %5D%0A%7D%0A

create createDateTimeMenuItemList inner function