PageRenderTime 24ms CodeModel.GetById 15ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/HotelSpaWP/wp-includes/js/customize-views.js

https://bitbucket.org/Trulsh/personal-bootstrap-projects
JavaScript | 186 lines | 113 code | 28 blank | 45 comment | 7 complexity | 122b154b05f13396529c049c1a17d8d1 MD5 | raw file
  1(function( $, wp, _ ) {
  2
  3	if ( ! wp || ! wp.customize ) { return; }
  4	var api = wp.customize;
  5
  6	/**
  7	 * wp.customize.HeaderTool.CurrentView
  8	 *
  9	 * Displays the currently selected header image, or a placeholder in lack
 10	 * thereof.
 11	 *
 12	 * Instantiate with model wp.customize.HeaderTool.currentHeader.
 13	 *
 14	 * @constructor
 15	 * @augments wp.Backbone.View
 16	 */
 17	api.HeaderTool.CurrentView = wp.Backbone.View.extend({
 18		template: wp.template('header-current'),
 19
 20		initialize: function() {
 21			this.listenTo(this.model, 'change', this.render);
 22			this.render();
 23		},
 24
 25		render: function() {
 26			this.$el.html(this.template(this.model.toJSON()));
 27			this.setButtons();
 28			return this;
 29		},
 30
 31		setButtons: function() {
 32			var elements = $('#customize-control-header_image .actions .remove');
 33			if (this.model.get('choice')) {
 34				elements.show();
 35			} else {
 36				elements.hide();
 37			}
 38		}
 39	});
 40
 41
 42	/**
 43	 * wp.customize.HeaderTool.ChoiceView
 44	 *
 45	 * Represents a choosable header image, be it user-uploaded,
 46	 * theme-suggested or a special Randomize choice.
 47	 *
 48	 * Takes a wp.customize.HeaderTool.ImageModel.
 49	 *
 50	 * Manually changes model wp.customize.HeaderTool.currentHeader via the
 51	 * `select` method.
 52	 *
 53	 * @constructor
 54	 * @augments wp.Backbone.View
 55	 */
 56	api.HeaderTool.ChoiceView = wp.Backbone.View.extend({
 57		template: wp.template('header-choice'),
 58
 59		className: 'header-view',
 60
 61		events: {
 62			'click .choice,.random': 'select',
 63			'click .close': 'removeImage'
 64		},
 65
 66		initialize: function() {
 67			var properties = [
 68				this.model.get('header').url,
 69				this.model.get('choice')
 70			];
 71
 72			this.listenTo(this.model, 'change:selected', this.toggleSelected);
 73
 74			if (_.contains(properties, api.get().header_image)) {
 75				api.HeaderTool.currentHeader.set(this.extendedModel());
 76			}
 77		},
 78
 79		render: function() {
 80			this.$el.html(this.template(this.extendedModel()));
 81
 82			this.toggleSelected();
 83			return this;
 84		},
 85
 86		toggleSelected: function() {
 87			this.$el.toggleClass('selected', this.model.get('selected'));
 88		},
 89
 90		extendedModel: function() {
 91			var c = this.model.get('collection');
 92			return _.extend(this.model.toJSON(), {
 93				type: c.type
 94			});
 95		},
 96
 97		select: function() {
 98			this.preventJump();
 99			this.model.save();
100			api.HeaderTool.currentHeader.set(this.extendedModel());
101		},
102
103		preventJump: function() {
104			var container = $('.wp-full-overlay-sidebar-content'),
105				scroll = container.scrollTop();
106
107			_.defer(function() {
108				container.scrollTop(scroll);
109			});
110		},
111
112		removeImage: function(e) {
113			e.stopPropagation();
114			this.model.destroy();
115			this.remove();
116		}
117	});
118
119
120	/**
121	 * wp.customize.HeaderTool.ChoiceListView
122	 *
123	 * A container for ChoiceViews. These choices should be of one same type:
124	 * user-uploaded headers or theme-defined ones.
125	 *
126	 * Takes a wp.customize.HeaderTool.ChoiceList.
127	 *
128	 * @constructor
129	 * @augments wp.Backbone.View
130	 */
131	api.HeaderTool.ChoiceListView = wp.Backbone.View.extend({
132		initialize: function() {
133			this.listenTo(this.collection, 'add', this.addOne);
134			this.listenTo(this.collection, 'remove', this.render);
135			this.listenTo(this.collection, 'sort', this.render);
136			this.listenTo(this.collection, 'change', this.toggleList);
137			this.render();
138		},
139
140		render: function() {
141			this.$el.empty();
142			this.collection.each(this.addOne, this);
143			this.toggleList();
144		},
145
146		addOne: function(choice) {
147			var view;
148			choice.set({ collection: this.collection });
149			view = new api.HeaderTool.ChoiceView({ model: choice });
150			this.$el.append(view.render().el);
151		},
152
153		toggleList: function() {
154			var title = this.$el.parents().prev('.customize-control-title'),
155				randomButton = this.$el.find('.random').parent();
156			if (this.collection.shouldHideTitle()) {
157				title.add(randomButton).hide();
158			} else {
159				title.add(randomButton).show();
160			}
161		}
162	});
163
164
165	/**
166	 * wp.customize.HeaderTool.CombinedList
167	 *
168	 * Aggregates wp.customize.HeaderTool.ChoiceList collections (or any
169	 * Backbone object, really) and acts as a bus to feed them events.
170	 *
171	 * @constructor
172	 * @augments wp.Backbone.View
173	 */
174	api.HeaderTool.CombinedList = wp.Backbone.View.extend({
175		initialize: function(collections) {
176			this.collections = collections;
177			this.on('all', this.propagate, this);
178		},
179		propagate: function(event, arg) {
180			_.each(this.collections, function(collection) {
181				collection.trigger(event, arg);
182			});
183		}
184	});
185
186})( jQuery, window.wp, _ );