Add support for polygerrit

Change-Id: I66d926265a9eeddbc3a081ad1fdb35c9a8843ce7
diff --git a/BUILD b/BUILD
index 89097bb..d903baf 100644
--- a/BUILD
+++ b/BUILD
@@ -5,6 +5,9 @@
     "PLUGIN_DEPS",
     "PLUGIN_TEST_DEPS",
 )
+load("//tools/bzl:genrule2.bzl", "genrule2")
+load("//tools/bzl:js.bzl", "polygerrit_plugin")
+
 
 gerrit_plugin(
     name = "delete-project",
@@ -16,6 +19,28 @@
         "Gerrit-SshModule: com.googlesource.gerrit.plugins.deleteproject.SshModule",
     ],
     resources = glob(["src/main/resources/**/*"]),
+    resource_jars = [":gr-delete-repo-static"],
+)
+
+genrule2(
+    name = "gr-delete-repo-static",
+    srcs = [":gr-delete-repo"],
+    outs = ["gr-delete-repo-static.jar"],
+    cmd = " && ".join([
+        "mkdir $$TMP/static",
+        "cp -r $(locations :gr-delete-repo) $$TMP/static",
+        "cd $$TMP",
+        "zip -Drq $$ROOT/$@ -g .",
+    ]),
+)
+
+polygerrit_plugin(
+    name = "gr-delete-repo",
+    srcs = glob([
+        "gr-delete-repo/*.html",
+        "gr-delete-repo/*.js",
+    ]),
+    app = "plugin.html",
 )
 
 junit_tests(
diff --git a/gr-delete-repo/gr-delete-repo.html b/gr-delete-repo/gr-delete-repo.html
new file mode 100644
index 0000000..c2a4a1b
--- /dev/null
+++ b/gr-delete-repo/gr-delete-repo.html
@@ -0,0 +1,57 @@
+<!--
+Copyright (C) 2018 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<dom-module id="gr-delete-repo">
+  <template>
+    <style include="gr-form-styles"></style>
+    <gr-repo-command
+        title="[[action.label]]"
+        tooltip="[[action.title]]"
+        disabled="[[!action.enabled]]"
+        on-command-tap="_handleCommandTap">
+    </gr-repo-command>
+    <gr-overlay id="deleteRepoOverlay" with-backdrop>
+      <gr-dialog
+          id="deleteRepoDialog"
+          confirm-label="Delete"
+          on-confirm="_handleDeleteRepo"
+          on-cancel="_handleCloseDeleteRepo">
+        <div class="header" slot="header">
+          Are you really sure you want to delete the repo: "[[repoName]]"?
+        </div>
+        <div class="main" slot="main">
+          <div class="gr-form-styles">
+            <div id="form">
+                <section>
+                  <input
+                      type="checkbox"
+                      id="forceDeleteOpenChangesCheckBox">
+                  <label for="forceDeleteOpenChangesCheckBox">Delete repo even if open changes exist?</label>
+                </section>
+                <section>
+                  <input
+                      type="checkbox"
+                      id="preserveGitRepoCheckBox">
+                  <label for="preserveGitRepoCheckBox">Preserve GIT Repository?</label>
+                </section>
+            </div>
+          </div>
+        </div>
+      </gr-dialog>
+    </gr-overlay>
+  </template>
+  <script src="gr-delete-repo.js"></script>
+</dom-module>
diff --git a/gr-delete-repo/gr-delete-repo.js b/gr-delete-repo/gr-delete-repo.js
new file mode 100644
index 0000000..0ce69c4
--- /dev/null
+++ b/gr-delete-repo/gr-delete-repo.js
@@ -0,0 +1,62 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+(function() {
+  'use strict';
+
+  Polymer({
+    is: 'gr-delete-repo',
+
+    properties: {
+      repoName: String,
+      config: Object,
+      action: Object,
+      actionId: String,
+    },
+
+    attached() {
+      this.actionId = this.plugin.getPluginName() + '~delete';
+      this.action = this.config.actions[this.actionId];
+      this.hidden = !this.action;
+    },
+
+    _handleCommandTap() {
+      this.$.deleteRepoOverlay.open();
+    },
+
+    _handleCloseDeleteRepo() {
+      this.$.deleteRepoOverlay.close();
+    },
+
+    _handleDeleteRepo() {
+      const endpoint = '/projects/' +
+          encodeURIComponent(this.repoName) + '/' +
+          this.actionId;
+
+      const json = {
+        force: this.$.forceDeleteOpenChangesCheckBox.checked,
+        preserve: this.$.preserveGitRepoCheckBox.checked
+      };
+
+      const errFn = response => {
+        this.fire('page-error', {response});
+      };
+
+      return this.plugin.restApi().send(
+          this.action.method, endpoint, json, errFn)
+            .then(r => {
+              Gerrit.Nav.navigateToRelativeUrl('/admin/repos');
+      });
+    },
+  });
+})();
diff --git a/plugin.html b/plugin.html
new file mode 100644
index 0000000..138397d
--- /dev/null
+++ b/plugin.html
@@ -0,0 +1,28 @@
+<!--
+Copyright (C) 2018 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+
+<link rel="import" href="./gr-delete-repo/gr-delete-repo.html">
+
+<dom-module id="delete-repo">
+  <script>
+    if (window.Polymer) {
+      Gerrit.install(function(plugin) {
+          plugin.registerCustomComponent(
+              'repo-command', 'gr-delete-repo');
+      });
+    }
+  </script>
+</dom-module>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/HttpModule.java b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/HttpModule.java
index a5470c9..f07ef52 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/HttpModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/HttpModule.java
@@ -24,5 +24,7 @@
   protected void configureServlets() {
     DynamicSet.bind(binder(), WebUiPlugin.class)
         .toInstance(new JavaScriptPlugin("delete-project.js"));
+    DynamicSet.bind(binder(), WebUiPlugin.class)
+        .toInstance(new JavaScriptPlugin("gr-delete-repo.html"));
   }
 }
diff --git a/src/main/resources/static/delete-project.js b/src/main/resources/static/delete-project.js
index 2200973..2291279 100644
--- a/src/main/resources/static/delete-project.js
+++ b/src/main/resources/static/delete-project.js
@@ -12,32 +12,34 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-Gerrit.install(function(self) {
-    function onDeleteProject(c) {
-      var f = c.checkbox();
-      var p = c.checkbox();
-      var b = c.button('Delete',
-        {onclick: function(){
-          c.call(
-            {force: f.checked, preserve: p.checked},
-            function(r) {
-              c.hide();
-              window.alert('The project: "'
-                + c.project
-                + '" was deleted.'),
-              Gerrit.go('/admin/projects/');
-            });
-        }});
-      c.popup(c.div(
-        c.msg('Are you really sure you want to delete the project: "'
-          + c.project
-          + '"?'),
-        c.br(),
-        c.label(f, 'Delete project even if open changes exist?'),
-        c.br(),
-        c.label(p, 'Preserve GIT Repository?'),
-        c.br(),
-        b));
-    }
-    self.onAction('project', 'delete', onDeleteProject);
-  });
+if (!window.Polymer) {
+  Gerrit.install(function(self) {
+      function onDeleteProject(c) {
+        var f = c.checkbox();
+        var p = c.checkbox();
+        var b = c.button('Delete',
+          {onclick: function(){
+            c.call(
+              {force: f.checked, preserve: p.checked},
+              function(r) {
+                c.hide();
+                window.alert('The project: "'
+                  + c.project
+                  + '" was deleted.'),
+                Gerrit.go('/admin/projects/');
+              });
+          }});
+        c.popup(c.div(
+          c.msg('Are you really sure you want to delete the project: "'
+            + c.project
+            + '"?'),
+          c.br(),
+          c.label(f, 'Delete project even if open changes exist?'),
+          c.br(),
+          c.label(p, 'Preserve GIT Repository?'),
+          c.br(),
+          b));
+      }
+      self.onAction('project', 'delete', onDeleteProject);
+    });
+}