+1 vote

When you activate a category, more than 9 times out of 10, you want to enter data for this category and you expect that Efficy will automatically select the corresponding tab for you.
But Efficy doesn't. Do developpers really use the app? ;-)

Since project team cares for their customers, here is a very simple code to implement this behavior.

In the pages\dialog\Category.htm file, call this function via <body onload="storeInitialCategoriesByEditHandle()">:

function storeInitialCategoriesByEditHandle() {
  var elems = document.forms[0].elements,
  editHandle = < % GetEditHandle() % > ,
  categoriesKeys = [];

  for (var i = 0, len = elems.length; i < len; i++) {
    var e = elems[i];
    if (e.type === 'checkbox' && e.checked)
      categoriesKeys.push(e.value);
  }

  if (opener.opener.initialCategoriesIndexedByEditHandle === undefined)
    opener.opener.initialCategoriesIndexedByEditHandle = [];

  opener.opener.initialCategoriesIndexedByEditHandle[editHandle] = categoriesKeys;
}

From the start of the js/main/edit.js call this function:

function checkNewCategory() {
  if (opener.initialCategoriesIndexedByEditHandle === undefined)
    return;

  var initialCategories = opener.initialCategoriesIndexedByEditHandle[Model("editHandle")];
  if (initialCategories === undefined)
    return;

  var $catgTabs = $("a[href^=#catg-]");
  for (var i = 0, length = $catgTabs.length; i < length; i++) {
    var catgTab = $catgTabs[i],
    hash = catgTab.hash,
    tableId = hash.substring("#catg-".length);
    if (parseInt(tableId) % 1000 === 0) // Ignore "Fields" pseudo category
      continue;
    if (initialCategories.indexOf(tableId) === -1) {
      $(catgTab).trigger("click");
    }
  }

  opener.initialCategories[Model("editHandle")] = undefined;
}

Any comment is welcome. Positive vote would be appreciated :-)

asked in Efficy/ Client side by (224 points)
edited by

1 Answer

+2 votes

Hi Pierre

Here is a solution that doesn't require inline code, but handles everything in one single JS function. It makes usage of sessionStorage to store the currently activated categories before the page is reloaded.

macros/MacroEditCustom.txt

RequireConfigCustom {[
  <%UseScript('custom/js/config/require.config.js')%>
]}

js/config/require.config.js

require.overrideModule('views/editView');

js/views/editView.js

define([
  'extendObject',
  'standard/views/editView'
], function(extendObject, StdEditView){

  var module = extendObject(StdEditView, {
    start: function() {
      this._super();
      this.autoCategoryFocus();
    },
    autoCategoryFocus: function() {     
      var storageName = 'activatedCategories',
        categoryElems = document.querySelectorAll(".category[data-category]"),
        activatedCategories = Array.prototype.slice.call(categoryElems).map(function(elem) {
          return elem.dataset.category;
        });

      // Detect if the activated categories have changed since last page reload
      if (sessionStorage.getItem(storageName) !== activatedCategories.join(";")) {
        var newCatg,
          prevCatgs = sessionStorage.getItem(storageName) ? sessionStorage.getItem(storageName).split(";") : [];

        activatedCategories.forEach(function (catg) {
          if (!newCatg && prevCatgs.indexOf(catg) === -1) {
            newCatg = catg;
          };
        });

        if (newCatg && document.getElementById('catg-' + newCatg)) {          
          console.log("The first new activated categoryid is %s", newCatg); 
          $(document).foundation('tab', 'activate_tab', '#catg-' + newCatg);
        }
      }

      sessionStorage.setItem(storageName, activatedCategories.join(";"));
    }
  });

  return module;
});

enter image description here

answered by (6.9k points)
I looove the this._super()
I urgently need to add it to https://help.efficy.com/edn/projectguides/load_custom_editview_module.
I assume it's introduced in standard since Efficy 11. It's mentioned in the sample folder.
This is a true override and extend, not a separated tree of JS
It seems it has been introduced in 11.2 cf /lib/js/std/base/extendObject.js
Kristof can you correct in the guide that the extend object is only since Efficy 11.2 ? thank you
It is mentioned in the article card in the main page, but indeed not explicitly in the guide.
1,169 questions
1,427 answers
1,718 comments
325 users