client/js/space-editor.js
changeset 188 50b3287d06dd
child 192 47aa37d3e750
equal deleted inserted replaced
187:ba5022639c76 188:50b3287d06dd
       
     1 $(function() {
       
     2     
       
     3     var originalData = {
       
     4         show_editor: false,
       
     5         container: "preview"
       
     6     };
       
     7    
       
     8     var userData = {
       
     9         bins: [
       
    10             {
       
    11                 type: "ResourceList",
       
    12                 title: "Resource List",
       
    13                 list: [
       
    14                     {
       
    15                         title: "IRI",
       
    16                         url: "http://www.iri.centrepompidou.fr/",
       
    17                         image: "http://www.iri.centrepompidou.fr/wp-content/themes/IRI-Theme/images/logo-iri-petit_fr_fr.png",
       
    18                         description: "Institut de Recherche et d'Innovation"
       
    19                     }
       
    20                 ]
       
    21             }
       
    22         ],
       
    23         search: [
       
    24             {
       
    25                 type: "Wikipedia",
       
    26                 lang: "en"
       
    27             }
       
    28         ]
       
    29     };
       
    30     
       
    31     function getNormalizedHtml(jqsel) {
       
    32         return jqsel.html().replace(/[\r\n]/gm,' ').replace(/\s{2,}/g,' ').replace(/(^\s+|\s+$)/g,'');
       
    33     }
       
    34     
       
    35     var searchEngineTemplate = getNormalizedHtml($(".search-engine-list"));
       
    36     
       
    37     $(".search-engine-list").empty().sortable();
       
    38     
       
    39     var resourceTemplate = getNormalizedHtml($(".resource-list"));
       
    40     
       
    41     $(".resource-list").empty();
       
    42     
       
    43     var binTemplate = getNormalizedHtml($(".bin-list"));
       
    44     
       
    45     $(".bin-list").empty().sortable();
       
    46     
       
    47     function addSearchEngine(data, editMode) {
       
    48         
       
    49         $(".item").removeClass("item-editing");
       
    50         
       
    51         var element = $(searchEngineTemplate),
       
    52             allfields = element.find(".edit-value"),
       
    53             typefield = element.find(".search-engine-type .edit-value"),
       
    54             typedisplay = element.find(".search-engine-type .display-value"),
       
    55             langsection = element.find(".search-engine-language"),
       
    56             langfield = element.find(".search-engine-language .edit-value"),
       
    57             langdisplay = element.find(".search-engine-language .display-value");
       
    58         element.data("userdata",data);
       
    59         
       
    60         if (editMode) {
       
    61             element.addClass("item-editing");
       
    62         }
       
    63         
       
    64         var onChange = function() {
       
    65             
       
    66             data.type = typefield.val();
       
    67             typedisplay.text(typefield.find("[value=" + data.type + "]").text());
       
    68             
       
    69             if (data.type === "Wikipedia") {
       
    70                 data.lang = langfield.val() || "en";
       
    71                 langdisplay.text(langfield.find("[value=" + data.lang + "]").text());
       
    72                 langsection.show();
       
    73             } else {
       
    74                 delete data.lang;
       
    75                 langsection.hide();
       
    76             }
       
    77         }
       
    78         
       
    79         typefield.val(data.type);
       
    80         
       
    81         if (data.type === "Wikipedia") {
       
    82             langfield.val(data.lang || "en");
       
    83         }
       
    84         
       
    85         allfields.on("keyup paste change", onChange);
       
    86         
       
    87         onChange();
       
    88         
       
    89         element.click(function() {
       
    90             $(".item").removeClass("item-editing");
       
    91             element.addClass("item-editing");
       
    92             return false;
       
    93         });
       
    94         element.find(".remove-item").click(function() {
       
    95             element.remove();
       
    96             return false;
       
    97         });
       
    98         
       
    99         $(".search-engine-list").append(element);
       
   100     }
       
   101     
       
   102     function addBin(data, editMode) {
       
   103         
       
   104         $(".item").removeClass("item-editing");
       
   105         
       
   106         var element = $(binTemplate),
       
   107             allfields = element.find(".edit-value"),
       
   108             typefield = element.find(".bin-type .edit-value"),
       
   109             typedisplay = element.find(".bin-type .display-value"),
       
   110             langsection = element.find(".bin-language"),
       
   111             langfield = langsection.find(".edit-value"),
       
   112             langdisplay = langsection.find(".display-value"),
       
   113             ldtsection = element.find(".ldt-options"),
       
   114             ldttypefield = element.find(".bin-ldt-type .edit-value"),
       
   115             ldttypedisplay = element.find(".bin-ldt-type .display-value"),
       
   116             ldtpffield = element.find(".bin-ldt-platform .edit-value"),
       
   117             ldtpfdisplay = element.find(".bin-ldt-platform .display-value"),
       
   118             ldtprojsection = element.find(".bin-ldt-project"),
       
   119             ldtprojfield = ldtprojsection.find(".edit-value"),
       
   120             ldtprojdisplay = ldtprojsection.find(".display-value"),
       
   121             kwsection = element.find(".bin-keyword"),
       
   122             kwfield = kwsection.find(".edit-value"),
       
   123             kwdisplay = kwsection.find(".display-value"),
       
   124             ressection = element.find(".bin-resources"),
       
   125             titlefield = element.find(".bin-title .edit-value"),
       
   126             titledisplay = element.find(".bin-title .display-value"),
       
   127             reslist = element.find(".resource-list");
       
   128         
       
   129         reslist.sortable({
       
   130             stop: updateListData
       
   131         });
       
   132         
       
   133         element.data("userdata",data);
       
   134         
       
   135         if (editMode) {
       
   136             element.addClass("item-editing");
       
   137         }
       
   138         
       
   139         var onChange = function() {
       
   140             
       
   141             data.type = typefield.val();
       
   142             typedisplay.text(typefield.find("[value=" + data.type + "]").text());
       
   143             
       
   144             if (data.type === "Wikipedia") {
       
   145                 data.lang = langfield.val() || "en";
       
   146                 langdisplay.text(langfield.find("[value=" + data.lang + "]").text());
       
   147                 langsection.show();
       
   148             } else {
       
   149                 delete data.lang;
       
   150                 langsection.hide();
       
   151             }
       
   152             
       
   153             if (data.type === "Ldt") {
       
   154                 ldtsection.show();
       
   155                 data.ldt_type = ldttypefield.val() || "Results";
       
   156                 ldttypedisplay.text(ldttypefield.find("[value=" + data.ldt_type + "]").text());
       
   157                 
       
   158                 data.ldt_platform = ldtpffield.val() || ldtpffield.val("http://ldt.iri.centrepompidou.fr/").val();
       
   159                 ldtpfdisplay.text(data.ldt_platform);
       
   160                 
       
   161                 if (data.ldt_type === "Project") {
       
   162                     data.project_id = ldtprojfield.val();
       
   163                     ldtprojdisplay.text(data.ldt_project);
       
   164                     ldtprojsection.show();
       
   165                 } else {
       
   166                     delete data.ldt_project;
       
   167                     ldtprojsection.hide();
       
   168                 }
       
   169                 
       
   170             } else {
       
   171                 delete data.ldt_type;
       
   172                 delete data.project_id;
       
   173                 delete data.ldt_platform;
       
   174                 ldtsection.hide();
       
   175             }
       
   176             
       
   177             if (data.type === "Wikipedia" || data.type === "Twitter" || (data.type === "Ldt" && data.ldt_type === "Results")) {
       
   178                 data.search = kwfield.val();
       
   179                 kwdisplay.text(data.search);
       
   180                 kwsection.show();
       
   181             } else {
       
   182                 delete data.search;
       
   183                 kwsection.hide();
       
   184             }
       
   185             
       
   186             if (data.type === "ResourceList") {
       
   187                 data.title = titlefield.val() || titlefield.val("Untitled Resource List").val();
       
   188                 titledisplay.text(data.title);
       
   189                 ressection.show();
       
   190             } else {
       
   191                 ressection.hide();
       
   192             }
       
   193             
       
   194             updateListData();
       
   195             
       
   196         }
       
   197         
       
   198         var updateListData = function() {
       
   199             if (data.type === "ResourceList") {
       
   200                 data.list = Array.prototype.slice.call(
       
   201                     reslist.find(".resource").map(function(){return $(this).data("userdata");})
       
   202                 );
       
   203             } else {
       
   204                 delete data.list;
       
   205             }
       
   206         }
       
   207         
       
   208         var addResource = function(resdata, editmode) {
       
   209             
       
   210             var resel = $(resourceTemplate),
       
   211                 all_fields = resel.find(".edit-value"),
       
   212                 title_field = resel.find(".resource-title .edit-value"),
       
   213                 title_text = resel.find(".resource-title .display-value"),
       
   214                 url_field = resel.find(".resource-url .edit-value"),
       
   215                 url_text = resel.find(".resource-url .display-value"),
       
   216                 description_field = resel.find(".resource-description .edit-value"),
       
   217                 description_text = resel.find(".resource-description .display-value"),
       
   218                 image_field = resel.find(".resource-image .edit-value"),
       
   219                 image_text = resel.find(".resource-image .display-value");
       
   220                 
       
   221             resel.data("userdata",resdata);
       
   222             
       
   223             if (editmode) {
       
   224                 resel.addClass("resource-editing");
       
   225             }
       
   226             
       
   227             var onResourceChange = function() {
       
   228                 
       
   229                 resdata.title = title_field.val();
       
   230                 title_text.text(resdata.title);
       
   231                 
       
   232                 resdata.description = description_field.val();
       
   233                 description_text.text(resdata.description);
       
   234                 
       
   235                 resdata.url = url_field.val();
       
   236                 url_text.text(resdata.url);
       
   237                 
       
   238                 resdata.image = image_field.val();
       
   239                 image_text.text(resdata.image);
       
   240                 
       
   241                 updateListData();
       
   242                 
       
   243             }
       
   244             
       
   245             title_field.val( resdata.title || "" );
       
   246             url_field.val( resdata.url || "" );
       
   247             description_field.val( resdata.description || "" );
       
   248             image_field.val( resdata.image || "" );
       
   249             
       
   250             all_fields.on("keyup paste change", onResourceChange);
       
   251             
       
   252             onResourceChange();
       
   253             
       
   254             resel.find(".remove-item").click(function() {
       
   255                 resel.remove();
       
   256                 updateListData();
       
   257                 return false;
       
   258             });
       
   259             
       
   260             resel.click(function() {
       
   261                 $(".resource").removeClass("resource-editing");
       
   262                 resel.addClass("resource-editing");
       
   263                 return false;
       
   264             });
       
   265             
       
   266             reslist.append(resel);
       
   267         }
       
   268     
       
   269         if (data.list) {
       
   270             data.list.forEach(addResource);
       
   271         }
       
   272         
       
   273         typefield.val(data.type);
       
   274         
       
   275         if (data.type === "Wikipedia") {
       
   276             langfield.val(data.lang || "en");
       
   277         }
       
   278             
       
   279         if (data.type === "Ldt") {
       
   280             ldttypefield.val(data.ldt_type || "Results");
       
   281             ldtpffield.val(data.ldt_platform || "http://ldt.iri.centrepompidou.fr/");
       
   282             
       
   283             if (data.ldt_type === "Project") {
       
   284                 ldtprojfield.val(data.ldt_project);
       
   285             }
       
   286             
       
   287         }
       
   288         
       
   289         if (data.type === "Wikipedia" || data.type === "Twitter" || (data.type === "Ldt" && data.ldt_type === "Results")) {
       
   290             kwfield.val(data.search || "");
       
   291         }
       
   292         
       
   293         if (data.type === "ResourceList") {
       
   294             titlefield.val(data.title || "Untitled Resource List");
       
   295         }
       
   296         
       
   297         allfields.on("keyup paste change", onChange);
       
   298         
       
   299         onChange();
       
   300         
       
   301         element.click(function() {
       
   302             $(".item").removeClass("item-editing");
       
   303             element.addClass("item-editing");
       
   304             return false;
       
   305         });
       
   306         element.find(".remove-item").click(function() {
       
   307             element.remove();
       
   308             return false;
       
   309         });
       
   310         element.find(".add-resource").click(function() {
       
   311             addResource({"title": "New Resource"},true);
       
   312             return false;
       
   313         });
       
   314         
       
   315         $(".bin-list").append(element);
       
   316         
       
   317     }
       
   318     
       
   319     
       
   320     userData.search.forEach(function(data) {
       
   321         addSearchEngine(data,false);
       
   322     });
       
   323     userData.bins.forEach(function(data) {
       
   324         addBin(data,false);
       
   325     });
       
   326     
       
   327     $(".add-search-engine").click(function() {
       
   328         addSearchEngine({
       
   329             type:"Twitter"
       
   330         }, true);
       
   331         return false;
       
   332     });
       
   333     
       
   334     $(".add-bin").click(function() {
       
   335         addBin({
       
   336             type:"Twitter",
       
   337             search: ""
       
   338         }, true);
       
   339         return false;
       
   340     });
       
   341     
       
   342     $(".update-preview").click(function() {
       
   343         userData.search = Array.prototype.slice.call(
       
   344             $(".search-engine").map(function(){return $(this).data("userdata");})
       
   345         );
       
   346         userData.bins = Array.prototype.slice.call(
       
   347             $(".bin").map(function(){return $(this).data("userdata");})
       
   348         );
       
   349         new Rkns.Renkan(_.extend({}, originalData, userData));
       
   350         
       
   351     });
       
   352    
       
   353     new Rkns.Renkan(_.extend({}, originalData, userData));
       
   354    
       
   355 });