ru-se.com

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

mdi-tab.js (10718B)


      1 (function ($) {
      2     var wp = window.wp.media ? window.wp : parent.wp;
      3     var fetchedIcons = false;
      4 
      5     var fuzzy_match = (function () {
      6         var cache = _.memoize(function (str) {
      7             return new RegExp("^" + str.replace(/./g, function (x) {
      8                 return /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/.test(x) ? "\\" + x + "?" : x + "?";
      9             }) + "$");
     10         })
     11         return function (str, pattern) {
     12             return cache(str).test(pattern)
     13         }
     14     })();
     15 
     16 
     17     var cpMDIIconModel = Backbone.Model.extend({
     18         defaults: {
     19             buttons: {
     20                 check: !0
     21             },
     22             can: {
     23                 save: !1,
     24                 remove: !1
     25             },
     26             id: null,
     27             title: null,
     28             date: null,
     29             modified: null,
     30             mime: "fa-icon/font",
     31             dateFormatted: null,
     32             height: null,
     33             width: null,
     34             orientation: null,
     35             filesizeInBytes: null,
     36             filesizeHumanReadable: null,
     37             size: {
     38                 url: null
     39             },
     40             type: "fa-icon",
     41             icon: null
     42         }
     43     });
     44 
     45 
     46     var cpMdiIcons = Backbone.Collection.extend({
     47         model: cpMDIIconModel,
     48         initialize: function (data) {
     49             this.url = parent.ajaxurl + "?action=materialis_list_mdi"
     50         },
     51         parse: function (data) {
     52             return data;
     53         }
     54     });
     55 
     56 
     57     var cachedIconsCollection = new cpMdiIcons();
     58     var iconsViewInstance;
     59     cachedIconsCollection.fetch({
     60         success: function (icons) {
     61             if (!fetchedIcons) {
     62                 fetchedIcons = icons;
     63             }
     64         }
     65     });
     66 
     67     var iconTemplate = _.template('' +
     68         '<div class="attachment-preview js--select-attachment type-image subtype-jpeg landscape">' +
     69         '<div class="thumbnail">' +
     70         '<i class="mdi <%= mdi %>" aria-hidden="true"></i>' +
     71         '<div class="label"><%= title %></div>' +
     72         '</div>' +
     73         '<button type="button" class="button-link check" tabindex="0"><span class="media-modal-icon"></span><span class="screen-reader-text">' + materialis_customize_settings.l10n.deselect + '</span></button>' +
     74         '</div>');
     75 
     76 
     77     var cpMdiIconView = wp.media.view.Attachment.extend({
     78         tagName: "li",
     79         className: "attachment cp-mdi-image",
     80         template: iconTemplate,
     81         controller: this.controller,
     82         initialize: function () {
     83             this.render()
     84         },
     85 
     86         attributes: function () {
     87             return {
     88                 'data-search': this.model.get('mdi').replace('mdi-', '').trim(),
     89                 'aria-label': this.model.get('title'),
     90                 'title': this.model.get('title'),
     91                 'tabIndex': 0,
     92             }
     93         },
     94         events: {
     95             "click .js--select-attachment": "toggleSelectionHandler"
     96         },
     97         render: function () {
     98             var icon = this.model.get('mdi');
     99             var title = this.model.get('title');
    100             this.$el.html(this.template({
    101                 'mdi': icon,
    102                 'title': title
    103             }))
    104         },
    105 
    106         toggleSelectionHandler: function (event) {
    107             var method = 'toggle';
    108 
    109             // Catch arrow events
    110             if (37 === event.keyCode || 38 === event.keyCode || 39 === event.keyCode || 40 === event.keyCode) {
    111                 this.controller.trigger('attachment:keydown:arrow', event);
    112                 return;
    113             }
    114 
    115             // Catch enter and space events
    116             if ('keydown' === event.type && 13 !== event.keyCode && 32 !== event.keyCode) {
    117                 return;
    118             }
    119 
    120             event.preventDefault();
    121 
    122 
    123             if (event.shiftKey) {
    124                 method = 'between';
    125             } else if (event.ctrlKey || event.metaKey) {
    126                 method = 'toggle';
    127             }
    128 
    129             this.toggleSelection({
    130                 method: 'add'
    131             });
    132 
    133             $('.media-selection.one .attachment-preview.type-mdi-icon .thumbnail').html('<i class="mdi-preview-icon mdi ' + this.model.get('mdi') + '"></i>')
    134 
    135             this.controller.trigger('selection:toggle');
    136         }
    137     });
    138 
    139 
    140     var cpMdiIconsView = wp.media.View.extend({
    141         tagName: "ul",
    142         className: "attachments cp-mdi-images",
    143         attributes: {
    144             tabIndex: 0
    145         },
    146         perPage: 50,
    147         currentPage: 1,
    148         filter: '',
    149         initialize: function () {
    150 
    151             _.defaults(this.options, {
    152                 refreshSensitivity: wp.media.isTouchDevice ? 300 : 200,
    153                 refreshThreshold: 3,
    154                 AttachmentView: wp.media.view.Attachment,
    155                 sortable: false,
    156                 resize: false,
    157                 idealColumnWidth: 150
    158             });
    159 
    160             this._viewsByCid = {};
    161             this.$window = $(window);
    162             this.options.scrollElement = this.options.scrollElement || this.el;
    163             var self = this;
    164             $(this.options.scrollElement).on("scroll", function (event) {
    165                 if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
    166                     self.scrolledToBottom(event);
    167                 }
    168 
    169             });
    170 
    171             iconsViewInstance = this;
    172 
    173             this.renderPage(1);
    174         },
    175 
    176         getItems: function () {
    177             var filterValue = this.filter;
    178             return this.collection.filter(function (item) {
    179                 return fuzzy_match(item.get('title'), filterValue);
    180             })
    181         },
    182 
    183         setFilter: function (filter) {
    184             this.filter = filter;
    185         },
    186 
    187         renderPage: function (page) {
    188             var startFrom = (page - 1) * this.perPage;
    189             for (var i = 0; i < this.perPage; i++) {
    190                 var icon = this.getItems()[startFrom + i];
    191 
    192                 if (icon) {
    193                     var iconView = new cpMdiIconView({
    194                         controller: iconsViewInstance.controller,
    195                         selection: iconsViewInstance.options.selection,
    196                         model: icon
    197                     });
    198                     iconsViewInstance.views.add(iconView)
    199                 }
    200             }
    201         },
    202 
    203         startAgain: function () {
    204             this.currentPage = 1;
    205             this.$el.empty();
    206             this.renderPage(1);
    207         },
    208 
    209         scrolledToBottom: function (event) {
    210             if (this.collection.length > this.currentPage * this.perPage) {
    211                 this.currentPage += 1;
    212                 this.renderPage(this.currentPage);
    213             }
    214         }
    215     });
    216 
    217     var cpMaterialIconsSearch = wp.media.View.extend({
    218         tagName: "input",
    219         className: "search",
    220         id: "media-search-input cp-mdi-search",
    221         attributes: {
    222             type: "search",
    223             placeholder: wp.media.view.l10n.search
    224         },
    225         events: {
    226             input: "search",
    227             keyup: "search",
    228             change: "search",
    229             search: "search"
    230         },
    231         render: function () {
    232             return this;
    233         },
    234 
    235         search: _.debounce(function (event) {
    236             iconsViewInstance.setFilter(event.target.value);
    237             iconsViewInstance.startAgain(1);
    238         }, 100)
    239     });
    240 
    241     var cpMaterialIconsBrowser = wp.media.View.extend({
    242         tagName: "div",
    243         className: "cp-mdi-media attachments-browser",
    244 
    245         initialize: function () {
    246             var browserVIew = this;
    247             _.defaults(this.options, {
    248                 filters: !1,
    249                 search: true,
    250                 date: false,
    251                 display: !1,
    252                 sidebar: false,
    253                 toolbar: true,
    254                 AttachmentView: wp.media.view.Attachment.Library
    255             });
    256 
    257             var icons = cachedIconsCollection;
    258 
    259 
    260             function displayIcons(icons) {
    261                 var state = browserVIew.controller.state(),
    262                     selection = state.get('selection');
    263                 state.set('multiple', true);
    264                 selection.multiple = false;
    265                 var iconsView = new cpMdiIconsView({
    266                     controller: browserVIew.controller,
    267                     selection: selection,
    268                     collection: icons
    269                 });
    270                 browserVIew.views.add(iconsView)
    271             }
    272 
    273 
    274             if (!fetchedIcons) {
    275                 icons.fetch({
    276                     success: function (icons) {
    277                         fetchedIcons = icons;
    278                         displayIcons(icons);
    279                     }
    280                 });
    281             } else {
    282                 displayIcons(fetchedIcons);
    283             }
    284 
    285             this.createToolbar();
    286         },
    287 
    288         settings: function (view) {
    289             if (this._settings) {
    290                 this._settings.remove();
    291             }
    292             this._settings = view;
    293             this.views.add(view);
    294         },
    295         createToolbar: function () {
    296             this.toolbar = new wp.media.view.Toolbar({
    297                 controller: this.controller
    298             })
    299             this.views.add(this.toolbar);
    300             this.toolbar.set("search", new cpMaterialIconsSearch({
    301                 controller: this.controller,
    302                 browserView: this.views
    303             }));
    304         }
    305     });
    306 
    307 
    308     function extendFrameWithCPMDI(frame) {
    309 
    310         var wpMediaFrame = frame;
    311         var cpMdiFrameExtension = {
    312             browseRouter: function (routerView) {
    313                 var routes = {
    314 
    315                     "cp_material_icons": {
    316                         text: materialis_customize_settings.l10n.chooseMDILabel,
    317                         priority: 50
    318                     }
    319                 };
    320                 controller = routerView.controller;
    321                 routerView.set(routes);
    322             },
    323 
    324             bindHandlers: function () {
    325 
    326                 wpMediaFrame.prototype.bindHandlers.apply(this, arguments);
    327                 this.on('content:create:cp_material_icons', this.cpBrowseMaterialIcons, this);
    328             },
    329 
    330             createStates: function () {
    331                 wpMediaFrame.prototype.createStates.apply(this, arguments);
    332             },
    333 
    334 
    335             cpBrowseMaterialIcons: function (contentRegion) {
    336 
    337                 var state = this.state();
    338 
    339                 this.$el.removeClass('hide-toolbar');
    340 
    341                 contentRegion.view = new cpMaterialIconsBrowser({
    342                     controller: this
    343                 });
    344             }
    345 
    346         }
    347 
    348         return wpMediaFrame.extend(cpMdiFrameExtension);
    349     }
    350 
    351     wp.media.cp = wp.media.cp || {};
    352     if (!wp.media.cp.extendFrameWithMDI) {
    353         wp.media.cp.extendFrameWithMDI = extendFrameWithCPMDI;
    354     }
    355 })(jQuery);