ru-se.com

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

webgradients-media-tab.js (10214B)


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