[IMP] simplify ckeditor image and link dialogs
authorXavier Morel <xmo@openerp.com>
Wed, 21 Aug 2013 14:38:16 +0000 (16:38 +0200)
committerXavier Morel <xmo@openerp.com>
Wed, 21 Aug 2013 14:38:16 +0000 (16:38 +0200)
& add easy linking to existing site pages and attachments

bzr revid: xmo@openerp.com-20130821143816-0hvp0m0eklh6ti1m

1  2 
addons/website/static/src/js/website.js
addons/website/views/views.xml
addons/website/website.py
addons/website_event/views/website_event.xml
addons/website_mail/views/website_mail.xml
addons/website_sale/views/website_sale.xml

          },
      });
  
+     function noop() {}
+     var alter_dialog = {
+         image: function (definition) {
+             definition.removeContents('Link');
+             definition.removeContents('advanced');
+             var upload = definition.getContents('Upload');
+             upload.add({
+                 type: 'select',
+                 label: 'Existing attachments',
+                 id: 'ir_attachment',
+                 items: [['']],
+                 /**
+                  * On dialog load, fetch all attachments (on ir.ui.view =>
+                  * previously uploaded images) and add them to the select's
+                  * options (items array & add method)
+                  */
+                 onLoad: function () {
+                     var field = this;
+                     // FIXME: fuck this garbage, also fuck openerp.Model
+                     return openerp.jsonRpc('/web/dataset/call_kw', 'call', {
+                         model: 'ir.attachment',
+                         method: 'search_read',
+                         args: [],
+                         kwargs: {
+                             fields: ['name'],
+                             domain: [['res_model', '=', 'ir.ui.view']],
+                             order: 'name',
+                         }
+                     }).then(function (results) {
+                         _(results).each(function (result) {
+                             field.add(result.name, result.id);
+                         });
+                     });
+                 },
+                 /**
+                  * The image widgets uses "txtUrl" to do most of its stuff.
+                  * Synchronize select & txtUrl by generating the correct URL
+                  * and setting it there
+                  */
+                 onChange: function () {
+                     var id = this.getValue();
+                     var url = this.getDialog().getContentElement('info', 'txtUrl');
+                     if (!id) {
+                         url.setValue('');
+                         return;
+                     }
+                     url.setValue('/website/attachment/' + id);
+                 },
+             });
+             // Override uploadButton to send its information to the select
+             // created above instead of directly to txtUrl. The select will
+             // propagate to txtUrl
+             upload.get('uploadButton').filebrowser = {
+                 onSelect: function (url) {
+                     var id = url.split('/').pop();
+                     var attachments = this.getDialog().getContentElement('Upload', 'ir_attachment');
+                     // TODO: return supplementary info to get image/attachment name?
+                     attachments.add(id, id);
+                     attachments.setValue(id);
+                 }
+             };
+             var old_show = definition.onShow;
+             definition.onShow = function () {
+                 // CKEDITOR does not *override* onShow, is smashes the existing
+                 // one instead, so override "by hand"
+                 if (old_show) {
+                     old_show.call(this);
+                 }
+                 // Assloads of code in the image plugin just go and tear into
+                 // the info tab without a care, so can't just remove the tab or
+                 // its content. Hide the tab instead, the effect is roughly the
+                 // same.
+                 this.hidePage('info');
+                 // Force the dialog to always and only display the Upload tab
+                 this.selectPage('Upload');
+                 this.on('selectPage', function (e) {
+                     setTimeout(function () {
+                         if (e.data.page !== 'Upload') {
+                             this.selectPage('Upload');
+                         }
+                     }.bind(this), 0);
+                 });
+             }
+         },
+         link: function (definition) {
+             definition.removeContents('target');
+             definition.removeContents('advanced');
+             var info = definition.getContents('info');
+             info.remove('linkType');
+             info.remove('anchorOptions');
+             info.remove('emailOptions');
+             info.get('urlOptions').children[0].widths = [ '0%', '100%' ];
+             info.get('protocol').style = 'display: none';
+             // TODO: sync edition of url to website_pages?
+             info.add({
+                 type: 'select',
+                 label: "Existing page",
+                 id: 'website_pages',
+                 items: [['']],
+                 /**
+                  * onload fetch all the pages existing in the website, then
+                  * display that.
+                  */
+                 onLoad: function () {
+                     var field = this;
+                     return openerp.jsonRpc('/web/dataset/call_kw', 'call', {
+                         model: 'website',
+                         method: 'list_pages',
+                         args: [],
+                         kwargs: {}
+                     }).then(function (results) {
+                         _(results).each(function (result) {
+                             field.add(result.name, result.url);
+                         });
+                     });
+                 },
+                 onChange: function () {
+                     var url = this.getValue();
+                     var url_field = this.getDialog().getContentElement('info', 'url');
+                     if (!url) {
+                         url_field.setValue('');
+                         return;
+                     }
+                     url_field.setValue(url);
+                 }
+             })
+         }
+     };
+     CKEDITOR.on('dialogDefinition', function (ev) {
+         (alter_dialog[ev.data.name] || noop)(ev.data.definition);
 -    })
 -});
++    });
++
 +    var all_ready = null;
 +    var dom_ready = $.Deferred();
 +    $(dom_ready.resolve);
 +
 +    /**
 +     * Returns a deferred resolved when the templates are loaded
 +     * and the Widgets can be instanciated.
 +     */
 +    website.ready = function() {
 +        if (!all_ready) {
 +            all_ready = dom_ready.then(function () {
 +                // TODO: load translations
 +                return website.load_templates(templates);
 +            });
 +        }
 +        return all_ready;
 +    };
 +
 +    dom_ready.then(function () {
 +        website.is_editable = $('html').attr('data-editable') === '1';
 +
 +        if (website.is_editable) {
 +            website.ready().then(website.init_editor);
 +        }
 +
 +        /* ----- PUBLISHING STUFF ---- */
 +        $(document).on('click', '.js_publish, .js_unpublish', function (e) {
 +            e.preventDefault();
 +            var $link = $(this).parent();
 +            $link.find('.js_publish, .js_unpublish').addClass("hidden");
 +            var $unp = $link.find(".js_unpublish");
 +            var $p = $link.find(".js_publish");
 +            $.post('/website/publish', {'id': $link.data('id'), 'object': $link.data('object')}, function (result) {
 +                if (+result) {
 +                    $p.addClass("hidden");
 +                    $unp.removeClass("hidden");
 +                } else {
 +                    $p.removeClass("hidden");
 +                    $unp.addClass("hidden");
 +                }
 +            });
 +        });
 +
 +    });
 +
 +    return website;
 +})();
  
Simple merge
Simple merge
  
      <!-- Page Shop my cart --> 
  
-     <template id="mycart" name="My cart">
 -    <template id="mycart" page="True">
++    <template id="mycart" name="My cart" page="True">
          <t t-call="website_sale.layout">
              <t t-set="title">My cart</t>
              <t t-set="shop_content">