1 define(function(require) {
7 var $ = require('libs/jquery'),
8 _ = require('libs/underscore'),
9 genericElement = require('modules/documentCanvas/canvas/genericElement'),
10 Dialog = require('views/dialog/dialog'),
11 boxTemplate = require('libs/text!./box.html'),
12 attachments = require('views/attachments/attachments'),
13 linkElement = Object.create(genericElement);
15 function youtubeParser(url) {
16 var regExp = /^.*(?:youtu.be\/|v\/|\/u\/\w\/|embed\/|\?v=|&v=|shared\?ci=)([^#&?]*).*/;
17 var match = url.match(regExp);
18 return (match && match[1].length == 11) ? match[1] : false;
21 _.extend(linkElement, {
23 genericElement.init.call(this);
24 _.bindAll(this, 'changeLink', 'deleteLink');
26 var linkText = this.wlxmlNode.getAttr('src') || '',
27 linkUrl = this.getUrl(linkText);
29 this.refreshLink(linkUrl);
31 this.box = $(_.template(boxTemplate)({text: linkText, url: linkUrl}));
32 this.box.find('.change').on('click', this.changeLink);
33 this.box.find('.delete').on('click', this.deleteLink);
35 this.addWidget(this.box);
37 onStateChange: function(changes) {
38 genericElement.onStateChange.call(this, changes);
39 if(_.isBoolean(changes.active)) {
40 this.box.toggle(changes.active);
43 onNodeAttrChange: function(event) {
44 if(event.meta.attr === 'src') {
45 var link = this.box.find('[link]');
46 link.text(event.meta.newVal);
47 var linkUrl = this.getUrl(event.meta.newVal);
48 link.attr('href', linkUrl);
49 this.refreshLink(linkUrl);
53 refreshLink: function(linkUrl) {
54 this._container().find('iframe').remove();
55 var video_id = youtubeParser(linkUrl);
57 var video_frame = '<iframe width="480" height="270" src="//www.youtube.com/embed/' + video_id +
58 '?controls=2&rel=0&showinfo=0&theme=light" frameborder="0" allowfullscreen></iframe>';
59 this._container().text('');
60 this._container().append($(video_frame));
62 this._container().text(gettext('No video'));
66 changeLink: function(e) {
68 dialog = Dialog.create({
69 title: gettext('Edit video url'),
70 executeButtonText: gettext('Apply'),
71 cancelButtonText: gettext('Cancel'),
74 label: gettext('YouTube link'),
77 initialValue: el.wlxmlNode.getAttr('src'),
80 return this.wlxmlNode.document.getLinkForUrl(text);
88 dialog.on('execute', function(event) {
89 el.wlxmlNode.document.transaction(function() {
90 el.wlxmlNode.setAttr('src', event.formData.src);
91 el.wlxmlNode.setAttr('videoid', youtubeParser(event.formData.src));
95 description: gettext('Edit video url')
96 //fragment: doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset})
99 //el.canvas.select(doc.createFragment(doc.CaretFragment, {node: el.wlxmlNode.contents()[0], offset:offset}));
104 $(".attachment-library", dialog.$el).on('click', function() {
105 attachments.select(function(v) {$("input", dialog.$el).val(v);});
110 deleteLink: function() {
113 el.wlxmlNode.document.transaction(function() {
114 el.wlxmlNode.detach();
117 description: gettext('Remove link')
122 getUrl: function(link) {
123 var pattern = /^[a-z]*:\/\//g;
124 if(!pattern.test(link) && !/^\//.test(link)) {
125 link = 'http://' + link;
127 return this.wlxmlNode.document.getUrlForLink(link);