(function (ns) { ns.Media = function () { this.construct.apply(this, arguments); }; var proto = ns.Media.prototype; proto.construct = function (options) { this.name = 'lib.form.element.Media'; this.options = $.extend({ id: undefined, browseId: undefined, deleteId: undefined, valueId: undefined, resourceBaseUrl: undefined, emptyValue: undefined, resourceType: undefined }, options); this.hidden = $('#' + this.options.id); if (1 !== this.hidden.length) { alert('Element #' + this.options.id + ' is not found!'); return; } this.browse = $('#' + this.options.browseId); if (1 !== this.browse.length) { alert('Element #' + this.options.browseId + ' is not found!'); return; } this._delete = $('#' + this.options.deleteId); if (1 !== this._delete.length) { alert('Element #' + this.options.deleteId + ' is not found!'); return; } this.value = $('#' + this.options.valueId); if (1 !== this.value.length) { alert('Element #' + this.options.valueId + ' is not found!'); return; } this.initFinder(); this.browse.on('click', $.proxy(this.onBrowseClick, this)); this._delete.on('click', $.proxy(this.onDeleteClick, this)); this.onHiddenChange(); }; proto.initFinder = function () { this.finder = new CKFinder(); this.finder.resourceType = this.options.resourceType; this.finder.selectActionFunction = $.proxy(proto.onFileSelect, this); }; proto.onBrowseClick = function () { this.finder.popup(); }; proto.onDeleteClick = function () { this.hidden.val(''); this.onHiddenChange(); }; proto.onFileSelect = function(fileUrl) { this.hidden.val(fileUrl); this.onHiddenChange(); }; proto.onHiddenChange = function () { var url = this.hidden.val(); if (url) { var file = decodeURIComponent(url.replace(this.options.resourceBaseUrl + '/', '')); var link = $(''); link.prop('href', url); link.prop('text', file); link.on('click', function () { window.open(this.href, '_blank', 'fullscreen=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=1280,height=720'); return false; }); this.value.html('').append(link); this._delete.removeClass('hidden'); } else { this.value.html(this.options.emptyValue); this._delete.addClass('hidden'); } }; })(qs.defineNS('lib.form.element'));