openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

trumbowyg.mention.js (2737B)


      1 /* ===========================================================
      2  * trumbowyg.mention.js v0.1
      3  * Mention plugin for Trumbowyg
      4  * http://alex-d.github.com/Trumbowyg
      5  * ===========================================================
      6  * Author : Viper
      7  *          Github: https://github.com/Globulopolis
      8  *          Website: http://киноархив.com
      9  */
     10 
     11 (function ($) {
     12     'use strict';
     13 
     14     var defaultOptions = {
     15         source: '',
     16         formatDropdownItem: formatDropdownItem,
     17         formatResult: formatResult
     18     };
     19 
     20     $.extend(true, $.trumbowyg, {
     21         langs: {
     22             en: {
     23                 mention: 'Mention'
     24             },
     25             fr: {
     26                 mention: 'Mentioner'
     27             },
     28             ru: {
     29                 mention: 'Упомянуть'
     30             },
     31             tr: {
     32                 mention: 'Bahset'
     33             }
     34         },
     35 
     36         plugins: {
     37             mention: {
     38                 init: function (trumbowyg) {
     39                     trumbowyg.o.plugins.mention = $.extend(true, {}, defaultOptions, trumbowyg.o.plugins.mention || {});
     40 
     41                     var btnDef = {
     42                         dropdown: buildDropdown(trumbowyg.o.plugins.mention.source, trumbowyg)
     43                     };
     44 
     45                     trumbowyg.addBtnDef('mention', btnDef);
     46                 }
     47             }
     48         }
     49     });
     50 
     51     /**
     52      * Build dropdown list
     53      *
     54      * @param   {Array}   items      Items
     55      * @param   {object}  trumbowyg  Editor
     56      *
     57      * @return  {Array}
     58      */
     59     function buildDropdown(items, trumbowyg) {
     60         var dropdown = [];
     61 
     62         // Check if source is an array
     63         if (items.constructor === Array) {
     64             $.each(items, function (i, item) {
     65                 var btn = 'mention-' + i,
     66                     btnDef = {
     67                         hasIcon: false,
     68                         text: trumbowyg.o.plugins.mention.formatDropdownItem(item),
     69                         fn: function () {
     70                             trumbowyg.execCmd('insertHTML', trumbowyg.o.plugins.mention.formatResult(item));
     71 
     72                             return true;
     73                         }
     74                     };
     75 
     76                 trumbowyg.addBtnDef(btn, btnDef);
     77                 dropdown.push(btn);
     78             });
     79         }
     80 
     81         return dropdown;
     82     }
     83 
     84     /**
     85      * Format item in dropdown.
     86      *
     87      * @param   {object}  item  Item object.
     88      *
     89      * @return  {string}
     90      */
     91     function formatDropdownItem(item) {
     92         return item.login;
     93     }
     94 
     95     /**
     96      * Format result pasted in editor.
     97      *
     98      * @param   {object}  item  Item object.
     99      *
    100      * @return  {string}
    101      */
    102     function formatResult(item) {
    103         return '@' + item.login + ' ';
    104     }
    105 })(jQuery);