PageRenderTime 72ms CodeModel.GetById 46ms RepoModel.GetById 1ms app.codeStats 0ms

/uPortal-webapp/src/main/webapp/WEB-INF/jsp/GoogleAnalytics/config.jsp

https://github.com/Jasig/uPortal
JavaServer Pages | 350 lines | 284 code | 56 blank | 10 comment | 17 complexity | e95a9b088958020c28e646ab30c60cfd MD5 | raw file
  1. <%--
  2. Licensed to Apereo under one or more contributor license
  3. agreements. See the NOTICE file distributed with this work
  4. for additional information regarding copyright ownership.
  5. Apereo licenses this file to you under the Apache License,
  6. Version 2.0 (the "License"); you may not use this file
  7. except in compliance with the License. You may obtain a
  8. copy of the License at the following location:
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. --%>
  17. <%@ include file="/WEB-INF/jsp/include.jsp"%>
  18. <c:set var="n"><portlet:namespace/></c:set>
  19. <div id="${n}google-analytics-config" class="google-analytics-config">
  20. <div class="property-configs">
  21. </div>
  22. <button class="addPropertyConfig">Add Host</button> <button class="saveConfig">Save</button> <button class="done">Done</button>
  23. </div>
  24. <script id="${n}propertyConfigTemplate" type="text/template">
  25. <a class="property destroy"></a>
  26. <div class="default-property-name">Default Property</div>
  27. <input name="propertyName" value="{{= name }}"/>
  28. <div>
  29. <label>Property Id:</label><input name="propertyId" value="{{= propertyId }}"/>
  30. </div>
  31. <div class="property-config">
  32. </div>
  33. <div class="property-dimensions">
  34. </div>
  35. </script>
  36. <script id="${n}settingsTemplate" type="text/template">
  37. <div>
  38. <label><a href="{{= settingsUrl }}" target="_blank" rel="noopener noreferrer">{{= settingsName }}:</a></label>
  39. <input class="settingName" name="settingName" value=""/>:
  40. <input class="settingValue" name="settingValue" value=""/>
  41. <button class="addSetting">+</button>
  42. </div>
  43. <div class="setting-data"></div>
  44. </script>
  45. <script id="${n}settingTemplate" type="text/template">
  46. <span class="view name">
  47. <label>{{= name }}</label>:
  48. </span>
  49. <span class="view value">
  50. <label>{{= value }}</label>
  51. </span>
  52. <a class="setting destroy"></a>
  53. </script>
  54. <portlet:resourceURL var="getDataUrl" id="getData" escapeXml="false" />
  55. <portlet:resourceURL var="storeDataUrl" id="storeData" escapeXml="false" />
  56. <portlet:actionURL var="configDoneUrl" name="configDone" escapeXml="false" />
  57. <script type="text/javascript">
  58. up.analytics = up.analytics || {};
  59. up.analytics.config = up.analytics.config || {};
  60. up.analytics.config.model = up.analytics.config.model || {};
  61. up.analytics.config.view = up.analytics.config.view || {};
  62. (function($, Backbone, _) {
  63. 'use strict';
  64. var data = ${up:json(data)};
  65. /**
  66. * Centralized sync method
  67. */
  68. up.analytics.sync = function(type) {
  69. return function(method, model, options) {
  70. //DOES NOTHING FOR NOW, SOMEDAY WE WILL HAVE REAL-TIME SYNC/SAVE
  71. };
  72. };
  73. up.analytics.config.model.Setting = Backbone.Model.extend({
  74. sync : up.analytics.sync("Setting")
  75. });
  76. up.analytics.config.model.SettingsList = Backbone.Collection.extend({
  77. model : up.analytics.config.model.Setting,
  78. sync : up.analytics.sync("SettingsList")
  79. });
  80. up.analytics.config.model.PropertyConfiguration = Backbone.Model.extend({
  81. sync : up.analytics.sync("PropertyConfiguration"),
  82. initialize : function(data) {
  83. this.set("config", new up.analytics.config.model.SettingsList(data.config || []));
  84. this.set("dimensionGroups", new up.analytics.config.model.SettingsList(data.dimensionGroups || []));
  85. },
  86. defaults : {
  87. name : "",
  88. propertyId : ""
  89. }
  90. });
  91. up.analytics.config.model.PropertyConfigurationList = Backbone.Collection.extend({
  92. model : up.analytics.config.model.PropertyConfiguration,
  93. sync : up.analytics.sync("PropertyConfigurationList")
  94. });
  95. up.analytics.config.model.GlobalConfiguration = Backbone.Model.extend({
  96. sync : up.analytics.sync("GlobalConfiguration"),
  97. initialize : function(data) {
  98. data = data || {};
  99. this.set("defaultConfig", new up.analytics.config.model.PropertyConfiguration(data.defaultConfig || {}));
  100. this.set("hosts", new up.analytics.config.model.PropertyConfigurationList(data.hosts || []));
  101. }
  102. });
  103. up.analytics.config.view.SettingView = Backbone.View.extend({
  104. tagName : "div",
  105. className : "setting-container",
  106. template : _.template($('#${n}settingTemplate').html()),
  107. events : {
  108. "click a.setting.destroy" : "clear"
  109. },
  110. initialize : function() {
  111. this.listenTo(this.model, 'change', this.render);
  112. this.listenTo(this.model, 'destroy', this.remove);
  113. },
  114. render : function() {
  115. $(this.el).html(this.template(this.model.toJSON()));
  116. return this;
  117. },
  118. // Remove the item, destroy the model.
  119. clear : function() {
  120. this.model.destroy();
  121. }
  122. });
  123. up.analytics.config.view.SettingsView = Backbone.View.extend({
  124. tagName : "div",
  125. className : "settings",
  126. template : _.template($("#${n}settingsTemplate").html()),
  127. initialize : function(options) {
  128. this.settingsName = options.settingsName;
  129. this.settingsUrl = options.settingsUrl;
  130. this.listenTo(this.model, 'add', this.addOne);
  131. this.listenTo(this.model, 'remove', this.remove);
  132. this.listenTo(this.model, 'reset', this.addAll);
  133. },
  134. events : {
  135. "click .addSetting" : "create"
  136. },
  137. render : function() {
  138. var modelData = this.model.toJSON();
  139. modelData["settingsName"] = this.settingsName;
  140. modelData["settingsUrl"] = this.settingsUrl;
  141. $(this.el).html(this.template(modelData));
  142. var container = document.createDocumentFragment();
  143. _.forEach(this.model.models, function(setting) {
  144. var view = new up.analytics.config.view.SettingView({
  145. model : setting
  146. });
  147. container.appendChild(view.render().el);
  148. }, this);
  149. this.$(".setting-data").html(container);
  150. return this;
  151. },
  152. // Add a single setting item to the list by creating a view for it
  153. addOne : function(setting) {
  154. var view = new up.analytics.config.view.SettingView({
  155. model : setting
  156. });
  157. this.$(".setting-data").append(view.render().el);
  158. },
  159. // Add all items in the setting collection at once.
  160. addAll : function() {
  161. this.model.each(this.addOne, this);
  162. },
  163. create : function(e) {
  164. this.newName = this.$(".settingName");
  165. this.newValue = this.$(".settingValue");
  166. if (!this.newName.val() || !this.newValue.val()) {
  167. return;
  168. }
  169. this.model.create({
  170. name : this.newName.val(),
  171. value : this.newValue.val()
  172. });
  173. this.newName.val('');
  174. this.newValue.val('');
  175. },
  176. remove : function(e) {
  177. this.render();
  178. }
  179. });
  180. up.analytics.config.view.PropertyConfigurationView = Backbone.View.extend({
  181. tagName : "div",
  182. className : "analytics-host",
  183. template : _.template($("#${n}propertyConfigTemplate").html()),
  184. initialize : function(options) {
  185. this.isDefaultConfig = (options.isDefaultConfig == true);
  186. },
  187. events : {
  188. "click a.property.destroy" : "clear",
  189. "blur input[name=propertyId]" : "updatePropertyId",
  190. "blur input[name=propertyName]" : "updatePropertyName"
  191. },
  192. render : function() {
  193. $(this.el).html(this.template(this.model.toJSON()));
  194. if (this.isDefaultConfig) {
  195. this.$("div.default-property-name").show();
  196. this.$("input[name=propertyName]").hide();
  197. }
  198. else {
  199. this.$("div.default-property-name").hide();
  200. this.$("input[name=propertyName]").show();
  201. }
  202. var configSettings = new up.analytics.config.view.SettingsView({
  203. model : this.model.get("config"),
  204. settingsName : "Settings",
  205. settingsUrl : "https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#create"
  206. });
  207. this.$(".property-config").html((configSettings.render().el));
  208. var dimensionSettings = new up.analytics.config.view.SettingsView({
  209. model : this.model.get("dimensionGroups"),
  210. settingsName : "Dimensions",
  211. settingsUrl : "https://support.google.com/analytics/answer/2709829"
  212. });
  213. this.$(".property-dimensions").html((dimensionSettings.render().el));
  214. return this;
  215. },
  216. updatePropertyId : function() {
  217. var propertyId = this.$("input[name=propertyId]").val();
  218. this.model.set("propertyId", propertyId);
  219. },
  220. updatePropertyName : function() {
  221. var propertyName = this.$("input[name=propertyName]").val();
  222. this.model.set("name", propertyName);
  223. },
  224. // Remove the item, destroy the model.
  225. clear : function() {
  226. this.model.destroy();
  227. }
  228. });
  229. up.analytics.config.view.GlobalConfigurationView = Backbone.View.extend({
  230. el : $("div#${n}google-analytics-config"),
  231. initialize : function() {
  232. this.listenTo(this.model, 'change', this.render);
  233. this.listenTo(this.model.get("hosts"), 'add', this.render);
  234. this.listenTo(this.model.get("hosts"), 'remove', this.render);
  235. this.render();
  236. },
  237. events : {
  238. "click .addPropertyConfig" : "newPropertyConfig",
  239. "click .saveConfig" : "saveConfig",
  240. "click .done" : "completeConfig"
  241. },
  242. render : function() {
  243. var container = document.createDocumentFragment();
  244. var defaultConfig = this.model.get("defaultConfig");
  245. var defaultConfigView = new up.analytics.config.view.PropertyConfigurationView({
  246. model : defaultConfig,
  247. isDefaultConfig: true
  248. });
  249. container.appendChild(defaultConfigView.render().el);
  250. var hosts = this.model.get("hosts");
  251. _.forEach(hosts.models, function(hostConfig) {
  252. var hostView = new up.analytics.config.view.PropertyConfigurationView({
  253. model : hostConfig
  254. });
  255. container.appendChild(hostView.render().el);
  256. }, this);
  257. this.$el.find(".property-configs").html(container);
  258. return this;
  259. },
  260. newPropertyConfig : function() {
  261. var instName = window.prompt("Host Name:");
  262. if (!instName)
  263. return;
  264. this.model.get("hosts").create({
  265. "name" : instName
  266. });
  267. },
  268. saveConfig : function() {
  269. $.ajax({
  270. type: "POST",
  271. url: "${storeDataUrl}",
  272. dataType: 'json',
  273. data: {config:JSON.stringify(globalConfig)},
  274. success: function (data) {
  275. //TODO force sync of globalConfig with returned data
  276. }
  277. });
  278. },
  279. completeConfig : function() {
  280. // The 'done' button requires an actionURL and a POST...
  281. var form = $('<form />', {
  282. action: '${configDoneUrl}',
  283. method: 'POST',
  284. style: 'display: none;'
  285. });
  286. form.appendTo('body').submit();
  287. }
  288. });
  289. var globalConfig = new up.analytics.config.model.GlobalConfiguration(data);
  290. new up.analytics.config.view.GlobalConfigurationView({
  291. model : globalConfig
  292. });
  293. })(up.jQuery, up.Backbone, up._);
  294. </script>